简体   繁体   中英

Resources for knitr chunks

I've been trying to find this on the web but haven't had any luck. I'm working on creating a report with R using knitr and was wondering if anyone knows of a good resource for all options involving <<>>= . I've seen some examples like <<setup, include=FALSE, cache=FALSE>>= but I don't know what these mean and would like to know what else I can do.

To let you close the question - everything is here: http://yihui.name/knitr/options/#chunk_options , I will just paste the most important (in my opinion) options below:

Code Evaluation

  • eval : (TRUE; logical) whether to evaluate the code chunk; it can also be a numeric vector to select which R expression(s) to evaluate, eg eval=c(1, 3, 4) or eval=-(4:5)

Text Results

  • echo : (TRUE; logical or numeric) whether to include R source code in the output file; besides TRUE/FALSE which completely turns on/off the source code, we can also use a numeric vector to select which R expression(s) to echo in a chunk, eg echo=2:3 means only echo the 2nd and 3rd expressions, and echo=-4 means to exclude the 4th expression
  • results : ('markup'; character) takes these possible values
    • markup : mark up the results using the output hook, eg put results in a special LaTeX
    • asis : output as-is, ie, write raw results from R into the output document
    • hold : hold all the output pieces and push them to the end of a chunk
    • hide hide results; this option only applies to normal R output (not warnings, messages or errors)
  • collapse : (FALSE; logical; applies to Markdown output only) whether to, if possible, collapse all the source and output blocks from one code chunk into a single block (by default, they are written to separate <pre></pre> blocks)
  • warning : (TRUE; logical) whether to preserve warnings (produced by warning()) in the output like we run R code in a terminal (if FALSE, all warnings will be printed in the console instead of the output document); it can also take numeric values as indices to select a subset of warnings to include in the output
  • error : (TRUE; logical) whether to preserve errors (from stop()); by default, the evaluation will not stop even in case of errors!! if we want R to stop on errors, we need to set this option to FALSE message: (TRUE; logical) whether to preserve messages emitted by message() (similar to warning)
  • include : (TRUE; logical) whether to include the chunk output in the final output document; if include=FALSE, nothing will be written into the output document, but the code is still evaluated and plot files are generated if there are any plots in the chunk, so you can manually insert figures; note this is the only chunk option that is not cached, ie, changing it will not invalidate the cache.

Cache

  • cache : (FALSE; logical) whether to cache a code chunk; when evaluating code chunks, the cached chunks are skipped, but the objects created in these chunks are (lazy-) loaded from previously saved databases (.rdb and .rdx) files, and these files are saved when a chunk is evaluated for the first time, or when cached files are not found (eg you may have removed them by hand)

Plots

  • fig.path : ('figure/'; character) prefix to be used for figure filenames (fig.path and chunk labels are concatenated to make filenames); it may contain a directory like figure/prefix- (will be created if it does not exist); this path is relative to the current working directory
  • fig.width , fig.height : (both are 7; numeric) width and height of the plot, to be used in the graphics device (in inches) and have to be numeric
  • dev : ('pdf' for LaTeX output and 'png' for HTML/markdown; character) the function name which will be used as a graphical device to record plots

An examplary chunk:

```{r global_options, include = FALSE}
knitr::opts_chunk$set(fig.width = 9, fig.height = 4, fig.path = "Figs/", dev = "svg",
                      echo = FALSE, warning = FALSE, message = FALSE,
                      cache = FALSE, tidy = FALSE, size = "small")
```

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM