LazyScript | R Documentation |
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,
update = FALSE
)
remove_rmd_chunk(rmd_path, match_labels = NULL)
remove_qmd_chunk(qmd_path, match_labels = NULL)
lift_inchunk_label(md_path, inchunk_rm = FALSE)
update_script_to_rmd(script_path, rmd_path = NULL, saveAll = 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 for |
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. |
update |
Logical. If both |
match_labels |
Vector of characters. The name of the chunk where contents to be removed.
If NULL, remove contents for all chunks except |
qmd_path |
String. The path to the Quarto file. |
md_path |
String. The path to the rmarkdown or quarto file. |
inchunk_rm |
Logical. Whether to remove the in-chunk label line once it is moved to the chunk header |
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.
A LazyScript object
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.
LazyScript %run% chunk_name
: 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.
remove_rmd_chunk()
: Remove code chunk content in rmd files.
remove_qmd_chunk()
: Remove code chunk content in qmd files.
lift_inchunk_label()
: Put in-chunk labels to chunk headers
update_script_to_rmd()
: Wrapper of copy_script_to_rmd
for match_chunk = TRUE, update = FALSE
Yangzhuoran Yang
knitr::read_chunk
, insert_purl_section
, insert_run_operator
## Not run:
test_script <- read_script("test.R")
test_script %run% "hello"
copy_script_to_rmd("test.R", "test.Rmd")
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.