Create multiple html files R markdown - r

How do I create multiple HTML files in a single R markdown file with the Knit HTML option in RStudio? I want to create a separate HTML file with graphs for different levels of a variable in a data frame. I know a similar question has been posted in How to best generate multiple HTML files from RMarkdown based on one dataset?, but I do not manage to get this running.
Could someone give me a simple example, for instance to create 3 HTML files of the mtcars dataset for each amount of cylinders.
data(mtcars)
for(cyl in unique(mtcars$cyl)){
plot.data <- mtcars[mtcars$cyl==cyl,]
plot(plot.data$mpg,plot.data$disp,main=paste(cyl,"cylinders"))
}
This would be of great help for me!
Thanks in advance,
Gijs

I managed to do the job!
The answer to the post R Knitr PDF: Is there a posssibility to automatically save PDF reports (generated from .Rmd) through a loop? really helped me.
The render function was the solution.
I thought I had to run this type of markdown just through the Knit HTML button, but then only one HTML file is created. The render function can be used in a for loop in R, calling a template.Rmd file to create multiple HTML files.

Related

Source RMarkdown chunks

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!

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

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.

Is there a way to get an R Markdown document from a recently knitted document (html, pdf or Word)?

So I just recently started to learn how to use RStudio for my Bio class. For my latest submission, I knitted my lab, which was made as an R Markdown, into an HTML file. For some reason, I might have deleted my RMD file, which was also needed in the submission. Is there any way to convert my HTML (PDF or even Word) file back into an R Markdown file?
Thank you so much!
-SuperEli
No, one rmarkdown file generates a unique word/html/pdf
however, different rmarkdown files may generate same output results.

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()

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