Learn R Programming

ezknitr (version 0.3.0)

ezknitr_core: Knit Rmd or spin R files without the typical pain of working directories

Description

ezknitr is an extension of knitr that adds flexibility in several ways. One common source of frustration with knitr is that it assumes the directory where the source file lives should be the working directory, which is often not true. ezknitr addresses this problem by giving you complete control over where all the inputs and outputs are, and adds several other convenient features. The two main functions are ezknit and ezspin, which are wrappers around knitr's knit and spin, used to make rendering markdown/HTML documents easier.

Usage

ezspin(file, wd, out_dir, fig_dir, out_suffix, params = list(),
  verbose = FALSE, chunk_opts = list(tidy = FALSE), keep_rmd = FALSE,
  keep_md = TRUE, keep_html = TRUE)

ezknit(file, wd, out_dir, fig_dir, out_suffix, params = list(),
  verbose = FALSE, chunk_opts = list(tidy = FALSE), keep_md = TRUE,
  keep_html = TRUE)

Value

  • The path to the output directory (invisibly).

Detailed Arguments

All paths given in the arguments can be either absolute or relative. The wd argument is very important and is set to the current working directory by default. The path of the input file and the path of the output directory are both relative to wd (unless they are absolute paths). Moreover, any code in the R script that reads or writes files will use wd as the working directory. The fig_dir argument is relative to the output directory, since the figures accompanying a markdown file should be placed in the same directory. It is recommended to either leave fig_dir as default or set it to a different name but not to a different directory. Because of the way knitr works, there are a few known minor issues if fig_dir is set to a different directory.

Difference between ezknit and ezspin

ezknit is a wrapper around knitr::knit while ezspin is a wrapper around ezspin. The two functions are very similar. knit is the more popular and well-known function. It is used to render a markdown/HTML document from an Rmarkdown source. spin takes an R script as its input, produces an Rmarkdown document from the R script, and then calls knit on it.

Details

If you have a very simple project with a flat directory structure, then knitr works great. But even something as simple as trying to knit a document that reads a file from a different directory or placing the output rendered files in a different folder cannot be easily done with knitr. ezknitr improves basic knitr functionality in a few ways. You get to decide:
  • What the working directory of the source file is
  • Where the output files will go
  • Where the figures used in the markdown will go
  • Any parameters to pass to the source file

See Also

open_output_dir setup_ezknit_test setup_ezspin_test set_default_params knit spin

Examples

Run this code
tmp <- setup_ezknit_test()
   ezknit("R/ezknit_test.Rmd", wd = "ezknitr_test")
   ezknit("R/ezknit_test.Rmd", wd = "ezknitr_test",
          out_dir = "output", fig_dir = "coolplots",
          params = list(numPoints = 50))
   open_output_dir()
   unlink(tmp, recursive = TRUE, force = TRUE)
 
   tmp <- setup_ezspin_test()
   ezspin("R/ezspin_test.R", wd = "ezknitr_test")
   ezspin("R/ezspin_test.R", wd = "ezknitr_test",
          out_dir = "output", fig_dir = "coolplots",
          params = list(numPoints = 50), keep_rmd = TRUE)
   open_output_dir()
   unlink(tmp, recursive = TRUE, force = TRUE)

Run the code above in your browser using DataLab