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 http://pkg.yangzhuoranyang.com/lazytype/ 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)
script_path | String. The path to the script. |
---|---|
library | Logical. Whether the library chunk (if there is one in the script) is ran when read in the script |
LazyScript | A LazyScript object. |
chunk_name | String. The label of the chunk in the script that you want to run. |
rmd_path | String. The path to the rmarkdown file. The default is the file currently opened in the Rstudio editor. |
saveAll | Logical. Whether save all open file in the editor. If FALSE, unsaved changes may be lost. |
match_chunk | 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.
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") }