How to access R markdown stored in a package - r

I want to create a package that produces reports using knitr that uses predefined templates.
At the moment I have a project directory that has this structure
R/createReport.r
R/reportTemplate.rmd
inside createReport.r I want to be able do something like the following;
require(knitr)
render('reportTemplate.rmd', output.file='someplace')
However I have no idea how to get the render function to locate my template file. Any help greatly appreciated!

You could store the template in yourpackage/inst/templates/sometemplate.Rmd and then access it with:
system.file("templates/sometemplate.Rmd", package="yourpackage")

Related

Rmarkdown: Is there a function to create .Rmd documents?

When using Rstudio, one can use File -> New File -> R markdown... to create a new R markdown file from a template, with YAML headers and some examples already buffered. Is there a function to replicate this without an IDE (in this case, saving the template Rstudio would buffer in a new file)?
I'd rather start my .Rmd file from template rather than from scratch, but not using an IDE.
rmarkdown::draft seems to do what you want. It creates a new file, based on a template. You can provide a link to a custom template. See the help page for details.

How to add CSS external files to be used by my R package function?

I'm making a personal package and would like to load a css file in one of the functions in this package in order to have a "default style". What is the optimal way of doing this?
I cannot find any documnetation that helps me understand how to work with external data like a css file, mostly what I find is about exporting external datasets to be loaded separately, etc.
I found the problem. I was using the inst/extdata folder to export my style.css file, but was trying to read it directly in the code. Thanks to #MrFlick docpage, I found out I had to use the system.file() function in order to use the exported file.

Create multiple html files R markdown

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.

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.

Is there a way to knitr markdown straight out of your workspace using RStudio?

I wonder whether I can use knitr markdown to just create a report on the fly with objects stemming from my current workspace. Reproducibility is not the issue here. I also read this very fine thread here.
But still I get an error message complaining that the particular object could not be found.
1) Suppose I open a fresh markdown document and save it.
2) write a chunk that refers to some lm object in my workspace. call summary(mylmobject)
3) knitr it.
Unfortunately the report is generated but the regression output cannot be shown because the object could not be found. Note, it works in general if i just save the object to .Rdata and then load it directly from the markdown file.
Is there a way to use objects in R markdown that are in the current workspace?
This would be really nice to show non R people some output while still working.
RStudio opens a new R session to knit() your R Markdown file, so the objects in your current working space will not be available to that session (they are two separate sessions). Two solutions:
file a feature request to RStudio, asking them to support knitting in the current R session instead of forcibly starting a new session;
knit manually by yourself: library(knitr); knit('your_file.Rmd') (or knit2html() if you want HTML output in one step, or rmarkdown::render() if you are using R Markdown v2)
Might be easier to save you data from your other session using:
save.image("C:/Users/Desktop/example_candelete.RData")
and then load it into your MD file:
load("C:/Users/Desktop/example_candelete.RData")
The Markdownreports package is exactly designed for parsing a markdown document on the fly.
As Julien Colomb commented, I've found the best thing to do in this situation is to save the large objects and then load them explicitly while I'm tailoring the markdown. This is a must if your data is coming through an ODBC and you don't want to run the entire queries repeatedly as you tinker with fonts and themes.

Resources