23

I am running R version 2.15.3 with RStudio version 0.97.312. I have one script that reads my data from various sources and creates several data.tables. I then have another r script which uses the data.tables created in the first script. I wanted to turn the second script into a R markdown script so that the results of analysis can be outputted as a report.

I do not know the purpose of read_chunk, as opposed to source. My read_chunk is not working, but source is working. With either instance I do not get to see the objects in my workspace panel of RStudio.

Please explain the difference between read_chunk and source? Why would I use one or the other? Why will my .Rmd script not work

Here is ridiculously simplified sample

It does not work. I get the following message

Error: object 'z' not found

Two simple files...

test of source to rmd.R

x <- 1:10
y <- 3:4
z <- x*y  

testing source.Rmd

Can I run another script from Rmd
========================================================

Testing if I can run "test of source to rmd.R"

```{r first part}
require(knitr)
read_chunk("test of source to rmd.R")
a <- z-1000
a
```

The above worked only if I replaced "read_chunk" with "source". I 
can use the vectors outside of the code chunk as in inline usage. 
So here I will tell you that the first number is `r a[1]`. The most 
interesting thing is that I cannot see the variables in RStudio 
workspace but it must be there somewhere.
Kalin
  • 1,691
  • 2
  • 16
  • 22
Farrel
  • 10,244
  • 19
  • 61
  • 99

3 Answers3

14

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.

Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
  • 5
    I read "Code Externalization". Maybe because I am not a "real" programmer I did not at first appreciate the difference between reading and evaluating. Why would one want to read previous code but not evaluate it? For what I want to do I can see that I clearly have to use `source`. – Farrel Mar 20 '13 at 12:09
  • 2
    I have also learned that each knitr run is a separate session and cannot use objects that were present in my RStudio session. So while I am writing (drafting, developing) my .Rmd script I need to start by simply pulling in a .Rdata image file that was produced by the original source .r file. If I were to repeatedly run my .Rmd during development to see how I am doing I would have to wait the 10-20 seconds each time the `source` line executes. Once I had everything working I could revert to using `source` so that future runs pulled in the latest data. – Farrel Mar 20 '13 at 12:12
  • 1
    1. store the code chunks for future references; 2. if you want speed, use the chunk option `cache=TRUE`. The two links in my answer are my best attempt of explanation. If you have read them and still do not understand `read_chunk()`, I would recommend you to leave this behind -- you probably do not really need it. – Yihui Xie Mar 20 '13 at 19:23
  • 1
    How about child documents? does it serve a different purpose? it sounds quite similar "The idea is like the command \input{} or \include{} in LaTeX to manage a large document in smaller parts." from ``http://yihui.name/knitr/demo/child/`` versus "You do not have to put the R code in the input document; with knitr, you can separate your input document with the R script" from ``http://yihui.name/knitr/demo/externalization/`` I would appreciate a quick word on basic differences, if any. +1 – PatrickT Dec 19 '14 at 08:30
  • @Yihui `source` doesn't seem to work -- I get yelled at for including `<` characters in my code for html sake. What do I do instead? – theforestecologist Apr 24 '17 at 20:40
1

There isn't an option to run a chunk interactively from within knitr AFAIK. However, this can be done easily enough with something like:

#' Run a previously loaded chunk interactively
#'
#' Takes labeled code loaded with load_chunk and runs it in the /global/ envir (unless otherwise specified)
#'
#' @param chunkName The name of the chunk as a character string
#' @param envir The environment in which the chunk is to be evaluated 
run_chunk <- function(chunkName,envir=.GlobalEnv) {
    chunkName <- unlist(lapply(as.list(substitute(.(chunkName)))[-1], as.character))    
    eval(parse(text=knitr:::knit_code$get(chunkName)),envir=envir) 
} 
NULL
russellpierce
  • 4,583
  • 2
  • 32
  • 44
1

In case it helps anyone else, I've found using read_chunk() to read a script without evaluating can be useful in two ways. First, you might have a script with many chunks and want control over which ones run where (e.g., a plot or a table in a specific place). I use source when I want to run everything in a script (for example, at the start of a document to load a standard set of packages or custom functions). I've started using read_chunk early in the document to load scripts and then selectively run the chunks I want where I need them.

Second, if you are working with an R script directly or interactively, you might want a long preamble of code that loads packages, data, etc. Such a preamble, however, could be unnecessary and slow if, for example, prior code chunks in the main document already loaded data.

Omar Wasow
  • 1,870
  • 24
  • 24