Code works interactively but not while knitting - r

I have an RMarkdown document with some R code in it. The code works fine when I'm running it interactively in the console. However, when I try to knit the document (using the "knit" button in RStudio), R throws error messages that some objects cannot be found.

When you compile a document (using the "knit" button in RStudio), knitting takes place in a separate R session. Therefore, your document must be self-contained:
Objects from the Global Environment of your interactive sessions are not available when knitting. You must create/load all objects from within your document.
You must load all packages used from within your document.
Also note that while knitting, your working directory is changed to the directory containing your document (by default). Therefore, relative paths (for example, paths to data sets you want to read) might not point to the expected files.

Related

Execute all R chunks of an external Rmd file from an R script

I have a .Rmd file called f1.Rmd (containing a mix of text and R chunks) and an R script called f2.R.
I would like to insert a set of R instructions in f2.R that would execute all the R chunks contained in f1.Rmd, in such a way that all variables created in f1.Rmd would be created in my current R session if I source f2.R
(similarly to what happens when clicking on "Run" -> "Run all chunks below from the Rstudio menu").
If you render the f1.Rmd file from your current environment, this should happen.
You can use rmarkdown::render() from the console or a .R script. This will create all the variables in your current environment. It will also have the side effect of making the document.
When you use the knit button in RStudio, this launches the render in a new r session as a background process.
See also the envir option for render.
See also this answer for other options. knitr: run all chunks in an Rmarkdown document
It also depends what your .Rmd is doing.

How to use libraries across an r notebook?

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.

How to stop RStudio from rendering nb.html on save of an R Markdown document

RStudio renders mynotebook.nb.html file every time R Markdown document mynotebook.Rmd is saved. This process does not involve running code in chunks, so it is much faster than knitting a notebook into mynotebook.html. However, for large .Rmd documents, saving nb.html files can take a long time, and, unfortunately, it is required to wait for it to finish before one can start using the notebook again and run code in chunks.
Is there a way to configure RStudio to not to create nb.html files on save of an R Markdown document?
I've found out you can delete relevant output entry from the top section of your file. In my case it looks like:
---
title: "Document"
output:
html_notebook: default
---
Which causes creation of .nb.html on every save. If you remove the output tag, file is no longer created automatically. You can still knit to any output file from the Knit menu on the top (or press Ctrl+Shift+K for default. This will run all the chunks again, which may take a while.
You may want to consult this guide book for more information on how YAML tags work. I'm just beginning with them!
Another reason (and solution) might be, that you clicked incidentally on the "Knit on Save" button located just under the Save-to-Disk-Symbol in RStudio. This was in my case the problem. Usually you should be able to save a rmd-file without triggering the knitting process.
So - given this scenario - just uncheck the "Knit on Save" button.

knitr: object cannot be found when converting markdown file into html

Hi I am using R studio and the "knitHTML" button to convert my Rmd file into a html file. However, even thought the code runs fine, when using knitHTML it cannot find any of my objects previously created:
## Error: object 'cbt_2010' not found
however if I type cbt_2010 at the terminal - it is there. Bascially knit cannot find any of the objects in the workspace.
what am I doing wrong? it just seems any data produced in each chunk is lost in memory when using knit!
As already mentioned by #BenBolker , you can use knit2html( Note that it is different from the Rstudio button, Rstudio use its own function to process document) from knitr:
x <- 10
writeLines(c("# hello markdown",
"```{r hello-random, echo=TRUE}",
"rnorm(x)", "```"), ## note the use of x
"test.Rmd")
library(knitr)
knit2html("test.Rmd")
Maybe I am missing something in your question, but if you create object 'cbt_2010' in your .Rmd file, knitr will have that object to work with.
Stated differently, you can find it when you type object 'cbt_2010' at the console because you created that object and it is available to R. You need to do the same in the .Rmd file.

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