How to Knitr Markdown Straight Out of Your Workspace Using Rstudio

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

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)

Using knitr markdown in RStudio calling two different versions of R (2.15.3 and 3.0.2)

Use engine tag:

```{r,engine='Rscript', engine.path='PATH_TO/R/R-3.0.2/bin/Rscript'}
version
```

```{r,engine='Rscript', engine.path='PATH_TO/R/R-2.15.3/bin/Rscript'}
version
```

EDIT add a picture of the knitr preview:

Sample Image

Clearing workspace and sourcing script in one go in rstudio

So, I found a non-ideal but will-do solution myself. Using the RStudio shortcut manager, you can define a shortcut to clean the workspace. I bound this to CTRL+SHIFT+X, such that it is close to the shortcut for sourcing the script, CTRL+SHIFT+S. However, clearing the workspace has a warning pop-up, so eventually, the routine becomes CTRL+SHIFT+X - ENTER - CTRL+SHIFT+S.

Making knitr run a r script: do I use read_chunk or source?

read_chunk() only reads the source code (for future references); it does not evaluate code like source(). The purpose of read_chunk() was explained in this page as well as the manual.

How to use objects from global environment in Rstudio Markdown

For better or worse, this omission is intentional. Relying on objects created outside the document makes your document less reproducible--that is, if your document needs data in the global environment, you can't just give someone (or yourself in two years) the document and data files and let them recreate it themselves.

For this reason, and in order to perform the render in the background, RStudio actually creates a separate R session to render the document. That background R session cannot see any of the environments in the interactive R session you see in RStudio.

The best way around this problem is to take the code you used to create the contents of your global environment and move it inside your document (you can use echo = FALSE if you don't want it to show up in the document). This makes your document self-contained and reproducible.

If you can't do that, there are a few approaches you can take to use the data in the global environment directly:

  1. Instead of using the Knit HTML button, type rmarkdown::render("your_doc.Rmd") at the R console. This will knit in the current session instead of a background session. Alternatively:

  2. Save your global environment to an .Rdata file prior to rendering (use R's save function), and load it in your document.

how to make knitr use the same R session in RStudio?

In case you use .Rnw files type directly in the console (given the working directory is where your file resides):

knitr::knit("filename.Rnw")
# or
knitr::knit2pdf("filename.Rnw")

The later also converts filename.tex to filename.pdf

For classical .Rmd files you can use the same

knitr::knit("filename.Rmd") 
# or
knitr::knit2html("filname.Rmd")

For the newer workflow using rmarkdown package use

rmarkdown::render("filename.Rmd")

This works because by default the knit and render functions have the envir argument set to envir=parent.frame(), which is usually the global environment of your R-Session.

Calling knit or render by clicking the knit button in R-Studio on the other hand calls these functions with argument envir=new.env().

custom RStudio exit strategy for a given project

You can just open a .Rproj with a text editor. It looks like this:

Version: 1.0

RestoreWorkspace: Yes
SaveWorkspace: Yes
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX

I changed the values for RestoreWorkspace and SaveWorkspace to accomplish what you want. Documentation on this is a bit spotty, to say the least, but the values which are in there by default are self-explanatory.

Knitr:spin - how to add text without manually adding #' every line?

In RStudio v 1.1.28, starting a line with #' causes the next line to start with #' when I hit enter in a *.R file on my machine (Ubuntu Linux 16.04LTS).

So as long as you start a text chunk with it, it will continue. But for previously existing R scripts, it looks like you would have to use find -> replace, or write a function to modify the required file, this worked for me in a very simple test.

comment_replace <- function(in_file, out_file = in_file){
in_text <- scan(file = in_file, what = character(), sep = "\n")
out_text <- gsub("^# ", "#' ", in_text)
cat(out_text, sep = "\n", file = out_file)
}

I would note, that this function does not check for preexisting #', you would want to build that in. I modified it so that it shouldn't replace them too much by adding a space in the regular expression.



Related Topics



Leave a reply



Submit