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

前端 未结 3 1167
长情又很酷
长情又很酷 2020-12-24 07:54

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

3条回答
  •  醉酒成梦
    2020-12-24 08:20

    In case it helps anyone else, I've found using read_chunk() to read a script without evaluating it can be useful in two ways. First, if you are working with the 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, earlier code chunks in the main document already loaded data.

    Second, 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 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.

提交回复
热议问题