I have a markdown script with several code chunks and providing this script together with my package.
I want to include unit testing for these code chunks or any way to make sure, my markdown script is always running.
Has anyone tried something before or can recommend a way of testing the markdown script?
I use a function called runAllChunks to run R code from an RMD file. I stole the function from knitr: run all chunks in an Rmarkdown document. That might be helpful in your situation.
Related
Please consider the following.
For me, the beauty of writing nearly all analyses in a RMarkdown file instead of in R scripts it that RMarkdown offers the possibility to write a report of the analysis while performing/coding it.
Sometimes, specific code snippets are re-used for different outputs. For example: a table created in a RMarkdown code chunk could be used in a Shiny app as well. Currently, I copy/paste the respective code from the RMarkdown into the Shiny app code.
However, when this table would be created in a R script, we can use source("table_script.R"). In this way no copy/paste is needed and both the RMarkdown and the Shiny app can make efficient use of this table. However, this (writing separate source-able R scripts) is exactly what I try to avoid when writing an RMarkdown, because otherwise the code chunks in the RMarkdown would have little other use than sourcing a couple of R scripts.
Question
Is there any way to source() (named) RMarkdown chunks?
Thanks in advance!
I have 2 scripts. One is an R script and the other an rmarkdown script.
I'm using the following code in the R script to run the markdown script:
rmarkdown::render("my_md_file_path_and_name.Rmd"))
I want to have the .html file it creates output into a folder of my choosing. At the moment it outputs into the same folder where the markdown script is stored.
Is this possible? I've done a lot of googling and although there's a lot of talk on this, i can't find anything which actually works. I'm not very familiar with markdown, so possibly there's a working solution i've read, but didn't fully understand how to code it into my script.
You can use output_file argument.
rmarkdown::render("my_md_file_path_and_name.Rmd",
output_file = '/file/path/out.html')
I have long complicated functions included in my code.
When I try to knit the Markdown file to HTML document, it takes a very long time and still nothing happens.
I tried to use cache=TRUE and updating my R/RStudio but it still doesn't work.
Does anyone have any idea what else I could try? Thanks
I am familiar with the situation. I am using Markdown to show some graphs with notes. When compiling Markdown all the code is executed. Also the computational expensive machine learning. To speed up my process I save the outcomes of my model to dataframes with the save function. The file type I use is .Rdata. In the Markdown document I use load to load the dataframes in the Markdown environment.
I am using RMarkdown to write a journal article. For various reasons I'd prefer to have the R analysis script in a separate Jupyter notebook. Is there a nice way to call R code from MyAnalysis.ipynb in MyArticle.Rmd?
I know I can use knitr syntax to have the .Rmd file read and execute chunks of R code from a .R file like so. And that you can use knitr::purl to call code chunks from one rmarkdown doc in another like so.
But I would like to be able to "purl" the code from the .ipynb file. Is there any way to do this?
I've been moving some of the code for a report I'm writing to child .rmd files. I want to run these chunks by clicking on the green arrow (top right):
But this doesn't work in RStudio, is this a feature or a bug?
This has not been implemented in RStudio yet, and probably won't be for some time.
However, you can write your R code in a separate file, reference it in R Markdown chunks, and execute those chunks interactively in RStudio. The way to do this is with knitr's code externalization feature. You can read about how to use it in R Markdown notebooks here:
https://rmarkdown.rstudio.com/r_notebooks.html#executing_code (scroll down a bit to Executing External Chunks)
More on code externalization with knitr here:
https://yihui.name/knitr/demo/externalization/