The LazyScript serie provides functions that can copy/run code between script and rmd files. LazyScript is a R6 class, but the functions here provides means for user to use LazyScript instead of handling the R6 system. See functions below for detailed usage. See for more documentations.

read_script(script_path, library = TRUE)

LazyScript %run% chunk_name

copy_script_to_rmd(script_path, rmd_path = NULL, saveAll = TRUE,
  match_chunk = TRUE)



String. The path to the script.


Logical. Whether the library chunk (if there is one in the script) is ran when read in the script


A LazyScript object.


String. The label of the chunk in the script that you want to run.


String. The path to the rmarkdown file. The default is the file currently opened in the Rstudio editor.


Logical. Whether save all open file in the editor. If FALSE, unsaved changes may be lost.


Logical. If TRUE, the function tries to find chunks in the script matching the chunks in the rmd file and make the copy to the corresponding chunks If FALSE, the chunks are appended to the end of the rmd file.


A LazyScript object


The LazyScript serie generally depends on the prul header ## ---- to seperate the sections in the script. See insert_purl_section for addin to insert this header quickly. It is also the head that knitr::read_chunk uses to regonise chunks.


  • read_script: Read in script and store them in a LazyScript object. Similar to knitr::read_chunk but in the context of script. Use purl header ## ---- to set labels in the script.

  • %run%: Run the chunk in the LazyScript object based on the specified label. Addin insert_run_operator can be used to insert it. Note: use print explicitly in the script to print result in the console.

  • copy_script_to_rmd: Copy the code in the script to the rmarkdown file based on the chunk label (when match_chunk = TRUE), or append the code to the end.

See also

knitr::read_chunk, insert_purl_section, insert_run_operator


if (FALSE) { test_script <- read_script("test.R") test_script %run% "hello" copy_script_to_rmd("test.R", "test.Rmd") }