I wish to use libraries across multiple .Rmd files in an r notebook without having to reload the library each time.
An example: I have loaded the library kableExtra in the index.Rmd file but when I call it in another .Rmd file such as ExSum.Rmd I would get this error:
Error in Kable....: could not find funciton "kable" Calls:...
If I load the kableExtra library again this problem goes away. Is there a workaround?
R Markdown files are intended to be standalone, so you shouldn't do this. There are two workarounds that come close:
If you process your .Rmd files within the R console by running code like rmarkdown::render("file.Rmd") then any packages attached in the session will be available to the code in the .Rmd file.
You can put all the setup code (e.g. library(kableExtra)) into a file (called setup.R, for example), and source it into each document in the first code chunk using source('setup.R'). Every file will run the same setup, but you only need to type it once.
The second is the better approach.
Related
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’m using knitr::rmarkdown (but knitr::knitr does the same) as my VignetteEngine. I then build my package vignette using devtools::build_vignettes().
This works, but only creates HTML and R output files in inst/doc. What I want is a Markdown output file, since only that can be displayed directly within the Github project pages (for HTML files, Github shows the source and for Rmd files it shows a rendered output but — obviously — without executing the R blocks).
I’ve tried finding out how to specify the output for custom VignetteEngines and I think that it should be possible (after all, other packages use this to build PDF vignettes, at a minimum) but I cannot find a way of doing this via devtools::build_vignettes. Is there no way around building the vignette manually (i.e. via knitr::knit or similar mechanisms that ignore the VignetteBuilder directives)?
I can’t find relevant information in the documentation/source either.
The only output formats for vignettes are HTML and PDF (and LaTeX, but it is converted to PDF, not displayed). Markdown isn't supported.
You can have arbitrary documentation files in your package (by convention you put them in inst/doc), but they aren't considered to be vignettes, so they won't be automatically built, functions like browseVignettes() will ignore them, etc.
To convert an Rmd file to md, just run knitr::knit on it.
I'm using a RMD file to create a package vignette. My. rmd file is stored in
.../path_to_package/vignettes/vignette.rmd
When creating a PDF the last line in the R Markdown console window is "Output created: /tmp/....".
How is it possible to create the PDF directly in the vignettes folder and not in a /tmp/.. folder?
I'm using Ubuntu 14.04 LTS and R 3.3.0, rmarkdown 0.9.6 and knitr 1.13.
Regards,
Johannes
rmarkdown::render does indeed output to the same directory as the input folder by default, but you can override that by supplying an output_dir argument to it (and an output_file one if you'd like to specify a different filename to the input file too).
I'm not sure why the Knit button in RStudio is doing something different for you—in my case it also outputs to the same folder as the source (even when I haven't specified a working folder and my home directory is the default), and RStudio doesn't show the function it's calling when you click the button, so it's a little hard to be sure. I'd stick to using rmarkdown::render() with the specified arguments for now.
Instead use
devtools::build_vignettes()
It will automatically put the files where they're supposed to go. Also check out Hadley Wickham's guide. It rocks!
I am doing report generation in R with knitr.
So basically I have a dataset, do some preprocessing and then call knitr to output an html report.
This means the entire workflow consists of several R code files and some .Rhtml templates which are needed later on for report generation.
I would like to wrap all of this into a R package.
Having just .r files I would just run package.skeleton() and have a start..
But, how do I deal with the .Rhtml files. What is the proper way to deal with these when building a R package?
Thanks,
Ben Bolkers answer:
put them in a directory within an inst directory and use system.file() to retrieve them.
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.