0

I write my R scripts in RStudio. I have also started using the Rmarkdown notebook feature of RStudio. As part of my workflow, I would like to have R scripts that include some R code evaluated when I source the code and when I render a notebook, and other R code that is evaluated only when I render the notebook, but not when sourced. For example a typical script could have the following format:

# R script

# import data (here I create data for this example)-------------------------
  dat <- data.frame(y = 1:10, x =11:20) )

# CHECK data ----------------------------------------------------------------

#~ hist(dat$x)
#' The data needs transformation

# Transform data ------------------------------------------------------------
  dat$z.x <- scale(dat$x)

where the #~ could act as a comment for sourcing in R and a rmarkdown command to execute R code, similar to the #' which evaluates markdown when the rmarkdown::render() function is used (The #~ does not work -- I made this up to illustrate).

The idea is to have the code used to explore, manipulate, and check the data that would be rendered into a notebook to document the data exploration and the decisions made in the code, including graphics and tables. This same file could also be used to manipulate(e.g. `source) the working data frame from the data sources. Sourcing the file would just result in munging the data, without generating the graphics or tables.

Is there a way to do this already in the framework I described?

wmmurrah
  • 213
  • 1
  • 6
  • [#' doesn't evaluate anything](http://stackoverflow.com/questions/25231385/what-exactly-is-the-roxygen-hash#comment39364815_25231385) – rawr Sep 12 '14 at 16:42
  • The code after `#'` is evaluated as markdown when `rmarkdown::render()` is used. I changed the question to make this clear. – wmmurrah Sep 12 '14 at 16:53

0 Answers0