Is there any way to open multiple R markdown files and knit them at the same time? - r

I have several RMD files in one folder and I need to knit them one by one everyday to get html for each of them. Is there any way or function that I can open them at the same time and knit them by running only few lines code or one function?

did you have a look at this chapter: https://bookdown.org/yihui/rmarkdown/parameterized-reports.html. I think that is exactly what you will need.

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.

How to export mulitple PDFs from R

I'm fairly new to R and am running into some issues exporting multiple graphs from R into a PDF file. The graphs I've created are:
plot(mirt.nom.cohen.pf.fa2,type="info")
plot(mirt.nom.cohen.pf.fa2,type="infotrace")
plot(mirt.nom.cohen.pf.fa2,type="trace")
What would be the most efficient way to export these to a pdf?
Any guidance would be greatly appreciated.
Thank you!
Darko
Think of it this way - to save an R graph as a pdf file, you need to:
Open the pdf file;
Place the graph in that file;
Close the pdf file.
What this means in your case is that you have to do something like this:
pdf("plot1.pdf")
plot(mirt.nom.cohen.pf.fa2,type="info")
dev.off()
pdf("plot2.pdf")
plot(mirt.nom.cohen.pf.fa2,type="infotrace")
dev.off()
pdf("plot3.pdf")
plot(mirt.nom.cohen.pf.fa2,type="trace")
dev.off()
The 3 pdf files you created will be stored in R's working directory, which you can find with the command:
getwd()
Once you know where R saved the pdf files, you have to locate that directory on your computer and then you can manually select and open the files. Each file will be single-page and contain one graph.
As already suggested here, you can also create a single, multi-page pdf file which will contain all 3 graphs (one graph per page):
pdf("plot.pdf")
plot(mirt.nom.cohen.pf.fa2,type="info")
plot(mirt.nom.cohen.pf.fa2,type="infotrace")
plot(mirt.nom.cohen.pf.fa2,type="trace")
dev.off()
This file will also be stored in R's working directory.
If you look at the help of the pdf function in R, you'll discover that you can use it with various options (e.g., width and height):
help(pdf)
do you want one to a page, or all on the same page?
to simply get them to a pdf file, you open the pdf device, indicate the file name, and then everything until you close it will go there.
pdf("plots.pdf")
your statements
dev.off()

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!

How to remove .tex after running knit and texi2pdf

I have an R data frame that I run through knitr using the following code:
knit('reportTemplate.Rnw', 'file.tex') # creates a .tex file from the .Rnw one
texi2pdf('file.tex') # creates a .pdf file from the .tex one
Inside my R script, I want to remove 'file.tex' from my computer folder afterwards. How do I achieve this? It is important that I do this within my .R file, since those lines are actually inside a loop that generates 1000 different reports from that template.
There are a family of functions in R that allow the user to interact with the computer's file system. Run ?files to see functions that make it possible to, for instance, create, rename and remove files.
As noted by Josh O'Brien, in a comment to the OP, in this specific case, the command to be used is file.remove('file.tex').

practically getting started with Sweave

my question(s) might be less general than the title suggests. I am running R on Mac OS X with a MySQL database to store the data. I have been working with the Komodo / Sciviews-R for some time. Recently I had the need for auto-generated reports and looked into Sweave. I guess StatET / Eclipse appears to be the "standard" solution for Sweavers.
1) Is it reasonable to switch from Komodo to StatET Eclipse? I tried StatET before but chose Komodo over StatET because I liked the calltip / autosuggest and the more convenient config from Komodo so much.
2) What´s a reasonable workflow to generate Sweave files? Usually I develop my R code first and then care about the report later. I just learned today that there is one file in Sweave that contains R code and Latex code at once and that from this file the .tex document is created. While the example files look handily and can't really imagine how to enter my 250 + lines of R code to a file and mixed it up with Latex.
Is it possible to just enter the qplot() and ggplot() statements to a such a document and source the functionality like database connection and intermediate results somehow?
Or is it just a matter of being used to the mix of Latex and R code?
Thx for any suggestions, hints, links and back-to-the-roots-shout-outs…
You've asked several questions, so here's several answers;
Is StatEt/Eclipse the right way to do Sweave ?
Not nessarily (note: I'm an avid StatEt/Eclipse user, and use it for both pure R and Sweave/R and love it, I haven't used Komodo / sciviews-R). You should be able to run the sweave command from any R command line which will generate a .tex file. You can then turn the .tex file into something readable (like pdf) from any tex environment.
What's a good Sweave workflow ?
When I have wanted to turn an r script into a sweave report I generaly start with an empty sweave template and copy/paste my entire R script into a sweave R block just after the title, i.e;
<<label=myEntireRScript, echo=false, include=false>>
#Insert code here
myTable<-dataframe(...)
myPlot<-qplot(....)
#
Then I go through and find the parts I want to report. For instance, if i want to put a table into the report, I'll cut the R block and put an xtable block in, and the same for variables and plots.
<<label=myEntireRScript, echo=false, include=false>>=
#Insert code here
#
Put any text I want before my table here, maybe with a \Sexpr{print(variable)} named variable
<<label=myTable, result=Tex>>=
myTable<-dataframe(...)
print(xtable(mytable,...),...)
#
Any text I want before my figure
<label=myplot, result=figure>>=
myPlot<-qplot(....)
print(qplot)
#
You may want to look at these related SO posts. The rest of my post relates to your question 2.
When creating reports with Sweave, I usually keep most of the R code and the report text separate. If the R code is fast to run, then I prefer I will include something like the following at the start of the .Rnw file:
<<>>
source('/path/to/script.r')
#
On the other hand, if the R code takes a long time, I will often include something like the following at the end of the R script:
Sweave('/path/to/report.Rnw'); system('pdflatex report.tex')
That way, I can re-generate the report quickly, without needing to run all the R code again. Then, the only work R has to do in the Sweave file is print tables, make graphs and maybe extract a few figures.
Like nullglob, I prefer to keep the R and Sweave files separate, but I prefer to save the workspace with save.image() rather than to source() the file. This avoids running the R calculations with each .Rnw file compiling (and I always end up tinkering with the typesetting more than I'd like).
My general work flow is to do each paper/project in it's own folder with it's own R file(s). When the calculation side is "done", I save.image() to store all the workspace variables as-is.
Then, in the .Rnw file in the same directory I set the working directory with setwd() and load all variables with load(".Rdata"). Of course, you can change the name you use for your workspace, but I do one workspace per folder and keep the default name. Oh, and if you tinker with the R file, be sure save the workspace image and watch out for variables that linger in the workspace and .Rnw file, but are no longer part of the R file... this is where the save.image() approach can cause some headaches.
I am on a Mac and I suggest TextMate if you're mildly geeky and emacs/ess if you're really geeky. I use vim and command line R, but emacs/ess works best for most. If you're in this for the long haul, I doubt you'll regret learning emacs/ess for R, Sweave, and LaTeX.

Resources