Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

rstudio

knitr

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.

like image 346
Matt Bannert Avatar asked Jun 22 '12 11:06

Matt Bannert


People also ask

How do you use the knitr in RStudio?

If you are using RStudio, then the “Knit” button (Ctrl+Shift+K) will render the document and display a preview of it. Note that both methods use the same mechanism; RStudio's “Knit” button calls rmarkdown::render() under the hood.

How do you display Markdown in RStudio?

To open a new file, click File > New File > R Markdown in the RStudio menu bar. A window will pop up that helps you build the YAML frontmatter for the . Rmd file. Use the radio buttons to select the specific type of output that you wish to build.

How can you compile the R Markdown document using knitr package?

The usual way to compile an R Markdown document is to click the Knit button as shown in Figure 2.1, and the corresponding keyboard shortcut is Ctrl + Shift + K ( Cmd + Shift + K on macOS). Under the hood, RStudio calls the function rmarkdown::render() to render the document in a new R session.

How do I retrieve data from R Markdown?

You can click in the upper left menu File > Import Dataset > From Excel and select the file to import it. Then you can copy the code that appears in the R console with the code required for import the data in xlsx and then copy it in a R Markdown code chunk.


1 Answers

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:

  1. file a feature request to RStudio, asking them to support knitting in the current R session instead of forcibly starting a new session;
  2. 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)
like image 107
Yihui Xie Avatar answered Oct 14 '22 04:10

Yihui Xie