Skip to contents

Wraps knitr::purl() to create an .R script from a .Rmd file. It can also auto-archive the .Rmd file to a [dir]/archive sub-directory. This is useful for turning first-attempt exploratory data analysis into production scripts once the validation code is complete.

Usage

convert_to_script(filename, dir = NULL, archive = FALSE)

Arguments

filename

string, the file name of both the .Rmd file that will be read and the file name of the .R file to be written (do not include .Rmd or .R extension)

dir

string, the directory where the .Rmd file is and the .R file will be written, default is NULL which means the current working directory will be used

archive

logical, whether to auto-archive the .Rmd file; default is FALSE

Value

nothing

Details

  • The resulting script will take the same name as the .Rmd file but with a different extension (.R)

  • If [dir]/archive does not already exist, it will be created

Examples

# get test notebook from the sdtmval/inst/extdata dir and copy it to temp dir
test_file_dir <- system.file("extdata", package = "sdtmval")
filename <- "test_notebook"
temp_path <- tempdir()
file.copy(from = file.path(test_file_dir, paste0(filename, ".Rmd")),
          to = file.path(temp_path, paste0(filename, ".Rmd")))
#> [1] TRUE

# create the script and archive the .Rmd file
convert_to_script(dir = temp_path, filename = filename, archive = TRUE)
#> 
#> 
#> processing file: /tmp/Rtmpe2JGvm/test_notebook.Rmd
#> 1/3                  
#> 2/3 [unnamed-chunk-1]
#> 3/3                  
#> output file: /tmp/Rtmpe2JGvm/test_notebook.R
#> [1] TRUE