Source RMarkdown chunks - r

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!

Related

R markdown file takes forever to knit to 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.

With RMarkdown is there a way to use knitr syntax to call external R code from a Jupyter notebook?

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?

Input .tex in Rmarkdown

I'm using Rmarkdown/Bookdown to write a paper/PDF document, which is an amazing tool #Yihui, thanks! Now I'm trying to include a table I have already put in LaTeX into the document by reading in this external .tex file. However, when knitting in RStudio with a \include{some-file.tex} or input{some-file.tex} in the body of the .Rmd outside of a chunk a LaTeX Error: Can be used only in preamble. is produced and the process stopped. I haven't found a way how to directly input through knit or otherwise into a chunk as well.
I found this question here: Rmarkdown v2, embed Latex document, although while the question is similar, there is no answer which would reflect how to input/include .tex-files into an .Rmd.
Why would I want this? Sometimes LaTeX tables offer more layout options than building directly in R, like for tables only with text rather than R-computed numbers. Also, when running models on a cluster, exporting results directly into .tex ready for compilation saves a lot of computation compared to have to open all these heavy .RData files just for getting the results into a PDF. Similarly, having sometimes multiple types of reports with different audiences, having the full R code in one main .Rmd file and integrating only the necessary results in other files reduces complexity by not having to redo all steps in each file newly. This way, I can keep one report with the full picture and do not have to check if I included every little change in various documents simultaneously.
So finally the question is how to get prepared .tex-Files into a .Rmd-document?
Thanks for your answers!

Testing code chunks of a markdown script

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.

How to create R documentation file (.Rd) in latex?

Is there a simple way to create R documentation file for simple R functions?
I know I can edit a .Rd file in R-studio and preview it in HTML file. But how to put it into latex to edit and preview? Is there some latex package producing R documentation format?
There is the Rd2latex function in the tools package that will convert from the .Rd format to LaTeX format. This will let you preview the documentation in LaTeX. However this does not allow converting edits to the LaTeX document back to the .Rd document.
Look at Sweave, maybe it helpful for you.
Sweave is a tool that allows to embed the R code for complete data analyses in latex documents.
The purpose is to create dynamic reports, which can be updated automatically if data or analysis change. Instead of inserting a prefabricated graph or table into the report, the master document contains the R code necessary to obtain it. When run through R, all data analysis output (tables, graphs, etc.) is created on the fly and inserted into a final latex document.
The report can be automatically updated if data or analysis change, which allows for truly reproducible research.
Check out printr http://yihui.name/printr/ . It should do what you need if you are using knitr.
The problem with Rd2latex is that i haven't figured out which style file I need to use, otherwise it works fine.
When you generate the latex code with the Rd2latex function, make sure that you copy the Rd.sty file from the R directory, paste it and somewhere that latex can see it and use \usepackage{Rd}.
Try the knitr package, an easy way to generate flexible and fast dynamic reports with R for LaTex.

Resources