Title: | Tools for RMarkdown |
---|---|
Description: | Tools for RMarkdown |
Authors: | Sebastian Kranz |
Maintainer: | Sebastian Kranz <[email protected]> |
License: | GPL >= 2.0 |
Version: | 0.2.7 |
Built: | 2024-11-20 05:51:18 UTC |
Source: | https://github.com/skranz/rmdtools |
Adapt header and footer of if blocks for output format and parse already the if condition for faster runtime evaluation
adapt.hf.blocks(txt, block.df = NULL, out.type = "html", only.types = c("if", "note"), ...)
adapt.hf.blocks(txt, block.df = NULL, out.type = "html", only.types = c("if", "note"), ...)
Adapt header and footer of if blocks for output format and parse already the if condition for faster runtime evaluation
adapt.if.blocks(txt, block.df = NULL, out.type = "html", only.types = "if", ...)
adapt.if.blocks(txt, block.df = NULL, out.type = "html", only.types = "if", ...)
Add code button with div to an ui
add.code.ui(ui = NULL, code, show_code, code.highlight = TRUE)
add.code.ui(ui = NULL, code, show_code, code.highlight = TRUE)
Create a Rmd chunk header line from a list of arguments
args.to.chunk.header(args = list(), type = "r", label = args$label)
args.to.chunk.header(args = list(), type = "r", label = args$label)
Scan all used block and whisker parameters in an .rmd file and create a template for a list
cat.rmd.params(file = NULL, text = readLines(file, warn = FALSE), use.blocks = TRUE, use.whiskers = TRUE)
cat.rmd.params(file = NULL, text = readLines(file, warn = FALSE), use.blocks = TRUE, use.whiskers = TRUE)
cat.whisker params
cat.whisker.params(file = NULL, text = readLines(file, warn = FALSE))
cat.whisker.params(file = NULL, text = readLines(file, warn = FALSE))
Translates a chunk header to a list of its option
chunk.opt.string.to.list(str, keep.name = FALSE)
chunk.opt.string.to.list(str, keep.name = FALSE)
str |
the chunk header as written in the rmd file |
keep.name |
shall the chunk name be kept? |
Main function to compile rmd to html
compile.rmd(file = NULL, text = readLines(file, warn = FALSE), envir = list(), if.blocks = c("ph", "render", "ignore")[1], blocks = c("ph", "render", "ignore")[1], whiskers = c("ph", "render", "render.as.text", "ignore")[1], chunks = c("ph", "knit", "render", "ignore")[1], start.line = "<!-- START -->", end.line = "<!-- END -->", set.utf8 = TRUE, out.type = "html", fragment.only = FALSE, whiskers.call.list = NULL, blocks.call.list = NULL, add.info = TRUE, use.commonmark = FALSE)
compile.rmd(file = NULL, text = readLines(file, warn = FALSE), envir = list(), if.blocks = c("ph", "render", "ignore")[1], blocks = c("ph", "render", "ignore")[1], whiskers = c("ph", "render", "render.as.text", "ignore")[1], chunks = c("ph", "knit", "render", "ignore")[1], start.line = "<!-- START -->", end.line = "<!-- END -->", set.utf8 = TRUE, out.type = "html", fragment.only = FALSE, whiskers.call.list = NULL, blocks.call.list = NULL, add.info = TRUE, use.commonmark = FALSE)
Render a knitr chunk in the same way as a whisker (taking into some chunk options, like results="asis")
eval.chunk.like.whisker(code, call = NULL, options = NULL, env = parent.frame())
eval.chunk.like.whisker(code, call = NULL, options = NULL, env = parent.frame())
Render all knitr chunks in the same way as a whisker (taking into some chunk options, like results="asis")
eval.chunks.in.text(rmd, envir = parent.frame())
eval.chunks.in.text(rmd, envir = parent.frame())
Evaluate a placeholder and return its value
eval.placeholder(ph, envir = parent.frame(), chunks = "knit", dir = getwd(), out.type = "html", cr = NULL, on.error = c("null", "error", "stop")[1], use.commonmark = TRUE, ...)
eval.placeholder(ph, envir = parent.frame(), chunks = "knit", dir = getwd(), out.type = "html", cr = NULL, on.error = c("null", "error", "stop")[1], use.commonmark = TRUE, ...)
Evaluate placeholders in compiled rmd
eval.placeholders(cr = NULL, envir = parent.frame(), ph = cr$ph, type = NULL, on.error = c("null", "error", "stop")[1], out.type = first.none.null(cr$out.type, "html"), ...)
eval.placeholders(cr = NULL, envir = parent.frame(), ph = cr$ph, type = NULL, on.error = c("null", "error", "stop")[1], out.type = first.none.null(cr$out.type, "html"), ...)
compile all given types of rmd blocks
eval.rmd.blocks.in.text(txt, to = "html", envir = parent.frame(), call.list = NULL, block.df = NULL, only.types = NULL, ignore.types = "if", use.del.rows.na = FALSE, replace.funs = NULL)
eval.rmd.blocks.in.text(txt, to = "html", envir = parent.frame(), call.list = NULL, block.df = NULL, only.types = NULL, ignore.types = "if", use.del.rows.na = FALSE, replace.funs = NULL)
evaluate whiskers and replace them in the text
eval.whiskers.in.text(str, envir = parent.frame(), signif = getOption("whiskerSignifDigits"), round = getOption("whiskerRoundDigits"), add.params = TRUE, whiskers.call.list = NULL)
eval.whiskers.in.text(str, envir = parent.frame(), signif = getOption("whiskerSignifDigits"), round = getOption("whiskerRoundDigits"), add.params = TRUE, whiskers.call.list = NULL)
#. type arguments
find.dot.blocks(txt, dot.levels = NULL, dot.start = "#. ")
find.dot.blocks(txt, dot.levels = NULL, dot.start = "#. ")
Find all rmd blocks that start with a line '#< ...' and end with a line '#>'
find.rmd.blocks(txt)
find.rmd.blocks(txt)
txt |
the rmd code, separated into lines |
A data.frame with the columns start, end, type, arg.str or NULL if no block was found
Find the start and end lines of all rmd chunks
find.rmd.chunks(rmd, add.code = FALSE)
find.rmd.chunks(rmd, add.code = FALSE)
rmd |
the rmd code as character vector, one element per line |
find blocks, chunks and dot blocks and add nesting info
find.rmd.nested(txt, dot.levels = NULL)
find.rmd.nested(txt, dot.levels = NULL)
txt |
the rmd source as character vector, each line one element |
dot.levels |
a list that describes the level of dot block types |
a data.frame
Find the parent index of each row given a vector levels that describes the nestedness
get.levels.parents(levels, is.parent.type = NULL)
get.levels.parents(levels, is.parent.type = NULL)
levels |
integer vector of levels must start with lowest level and increase by 1 or decrease to an integer number |
is.parent.type |
NULL or a optional boolean vector of those rows that are of a type for which children shall be found. |
a vector of parent indices or 0 for most outer levels
Find the parent index of each row given a vector levels that describes the nestedness
get.levels.parents.by.types(levels, types, parent.types = setdiff(unique(types), c(NA)))
get.levels.parents.by.types(levels, types, parent.types = setdiff(unique(types), c(NA)))
levels |
integer vector of levels must start with lowest level and increase by 1 or decrease to an integer number |
types |
character vector of types |
parent.types |
the parent.types that shall be characterized |
a matrix of parent type indices with length(levels) rows and length(parent.types) columns. If there is no parent type, we enter a 0.
Find the levels given ordered start and end positions of possible nested blocks
get.start.end.levels(start, end, start.level = 1L)
get.start.end.levels(start, end, start.level = 1L)
start |
vector of start positions of the blocks |
end |
vector of end positions of the blocks |
start.level |
the initial level |
a vector of levels of each blocks, moore deeply nested blocks have higher levels
Gets for each element the index of its type
get.types.inds(types)
get.types.inds(types)
types |
a character vector of types |
A button that toogles whether content in a div is displayed or not
hideShowButton(id, label, content = NULL, div.id = NULL, shown = FALSE, ...)
hideShowButton(id, label, content = NULL, div.id = NULL, shown = FALSE, ...)
own function to print a compact html table from a data frame with option to select row
html.table(df, sel.row = NULL, col.names = TRUE, row.names = FALSE, border = TRUE, bg.color = c("#dddddd", "#ffffff"), font.size = "80%", round.digits = 8, signif.digits = 8, col.tooltips = NULL, ...)
html.table(df, sel.row = NULL, col.names = TRUE, row.names = FALSE, border = TRUE, bg.color = c("#dddddd", "#ffffff"), font.size = "80%", round.digits = 8, signif.digits = 8, col.tooltips = NULL, ...)
Variant of htmltools::htmlTemplate
html.template(file = NULL, html = NULL, envir = parent.frame(), document_ = "auto")
html.template(file = NULL, html = NULL, envir = parent.frame(), document_ = "auto")
A more convenient yaml importer
import.yaml(file = NULL, text = NULL, verbose = FALSE, keep.quotes = FALSE, quote.char = "__QUOTE__", catch.error = TRUE, check.by.row = FALSE, utf8 = TRUE)
import.yaml(file = NULL, text = NULL, verbose = FALSE, keep.quotes = FALSE, quote.char = "__QUOTE__", catch.error = TRUE, check.by.row = FALSE, utf8 = TRUE)
Inline dependencies with local file references
inline.dependencies(deps, mustWork = TRUE)
inline.dependencies(deps, mustWork = TRUE)
Inline a dependency with local file references
inline.dependency(dependency, mustWork = TRUE)
inline.dependency(dependency, mustWork = TRUE)
Does not create /figure subfolder in current wd
knit.chunk(text, envir = parent.frame(), fragment.only = TRUE, quiet = TRUE, encoding = getOption("encoding"), html.table = TRUE, out.type = "html", knit.dir = getwd(), use.commonmark = TRUE, deps.action = c("add", "ignore")[1], args = NULL, eval_mode = c("knit", "sculpt", "eval")[1], show_code = c("no", "note", "open_note", "note_after", "open_note_after", "before", "after")[1], code.highlight = use.commonmark, ...)
knit.chunk(text, envir = parent.frame(), fragment.only = TRUE, quiet = TRUE, encoding = getOption("encoding"), html.table = TRUE, out.type = "html", knit.dir = getwd(), use.commonmark = TRUE, deps.action = c("add", "ignore")[1], args = NULL, eval_mode = c("knit", "sculpt", "eval")[1], show_code = c("no", "note", "open_note", "note_after", "open_note_after", "before", "after")[1], code.highlight = use.commonmark, ...)
Knits the rmd txt
knit.rmd(text, envir = parent.frame(), fragment.only = TRUE, quiet = TRUE, encoding = getOption("encoding"), html.table = TRUE, out.type = "html", use.commonmark = FALSE)
knit.rmd(text, envir = parent.frame(), fragment.only = TRUE, quiet = TRUE, encoding = getOption("encoding"), html.table = TRUE, out.type = "html", use.commonmark = FALSE)
Does not create /figure subfolder in current wd
knit.rmd.in.temp(text, envir = parent.frame(), ...)
knit.rmd.in.temp(text, envir = parent.frame(), ...)
Parse a n rmd chunk and store info in a list
make.chunk.info(txt, id = NULL)
make.chunk.info(txt, id = NULL)
Make a info for a placeholder object
make.placeholder.info(txt, type, form)
make.placeholder.info(txt, type, form)
Parse a whisker and create meta info
make.whisker.info(txt, add.variables = FALSE)
make.whisker.info(txt, add.variables = FALSE)
Recursively encode strings in list as UTF-8
mark_utf8(x)
mark_utf8(x)
Own markdown to html converter that interfaces commonmark
md2html(text, fragment.only = TRUE, options = c("use_xhtml", "mathjax", if (include.images) "base64_images" else NULL, "highlight_code"), include.images = TRUE, smart = FALSE, use.commonmark = FALSE, ...)
md2html(text, fragment.only = TRUE, options = c("use_xhtml", "mathjax", if (include.images) "base64_images" else NULL, "highlight_code"), include.images = TRUE, smart = FALSE, use.commonmark = FALSE, ...)
text |
the variable containing the markdown text |
fragment.only |
only a fragment or should html headers be added |
options |
options to markdownToHTML. These are the default options without smartypants |
smart |
smart punctuation (relevant for commonmark) |
use.commonmark |
use commonmark instead of markdownToHTML (no mathjax and included images)... |
The created HTML as a string
Normalize an id to letters that are allowed
normalize.id(str, allowed = c(letters, LETTERS, 0:9, "_"), subst = "_")
normalize.id(str, allowed = c(letters, LETTERS, 0:9, "_"), subst = "_")
Parse an arg.str as list
parse.arg.str(arg.str)
parse.arg.str(arg.str)
Parse the name of an rmd block
parse.block.args(header, arg.str = NULL, add.type = TRUE, type = "", allow.unquoted.title = FALSE)
parse.block.args(header, arg.str = NULL, add.type = TRUE, type = "", allow.unquoted.title = FALSE)
Parse the name of a knitr chunk and its arguments
parse.chunk.args(header, arg.str = NULL)
parse.chunk.args(header, arg.str = NULL)
Take a vector of chunk header lines and returns the chunk names
parse.chunk.names(header)
parse.chunk.names(header)
header |
the chunk headers |
Parse a hashdot yaml string
parse.hashdot.yaml(txt, hashdot = "#. ", ...)
parse.hashdot.yaml(txt, hashdot = "#. ", ...)
Unlike replace whiskers, returns a string vector if whiskers evaluate as vectors Cannot deal with nested whiskers
paste.whiskers(str, values = parent.frame(), eval = TRUE, signif.digits = NULL, error.val = "`Error`", empty.val = NULL, whisker.start = "{{", whisker.end = "}}", sep = "", collapse = NULL, return.list = FALSE)
paste.whiskers(str, values = parent.frame(), eval = TRUE, signif.digits = NULL, error.val = "`Error`", empty.val = NULL, whisker.start = "{{", whisker.end = "}}", sep = "", collapse = NULL, return.list = FALSE)
Prints list read from a yaml file
## S3 method for class 'yaml' print(obj)
## S3 method for class 'yaml' print(obj)
Create n random strings of length nchar each
random.string(n = 1, nchar = 14)
random.string(n = 1, nchar = 14)
Read a text file and convert to UTF-8
read.as.utf8(file, sep.lines = TRUE)
read.as.utf8(file, sep.lines = TRUE)
Reads a yaml file and returns as a list
read.yaml(file = NULL, verbose = FALSE, keep.quotes = TRUE, quote.char = "__QUOTE__", text = NULL, catch.error = TRUE, check.by.row = FALSE, space.after.colon = FALSE, utf8 = TRUE)
read.yaml(file = NULL, verbose = FALSE, keep.quotes = TRUE, quote.char = "__QUOTE__", text = NULL, catch.error = TRUE, check.by.row = FALSE, space.after.colon = FALSE, utf8 = TRUE)
Read a text file that was saved in UTF-8 format
readUtf8(file)
readUtf8(file)
Remove quotes from strings
remove.quotes(str, quotes = c("'", "\""))
remove.quotes(str, quotes = c("'", "\""))
Removes the rmd chunks with the given chunk names from rmd code
remove.rmd.chunks(rmd, chunk.names)
remove.rmd.chunks(rmd, chunk.names)
rmd |
the rmd code as character vector, one element per line |
chunk.names |
the list of rmd chunks that shall be removed |
Render a compiled rmd
render.compiled.rmd(cr = NULL, txt = cr$body, envir = parent.frame(), fragment.only = FALSE, chunks = c("knit", "eval")[1], out.type = if (is.null(cr$out.type)) "html" else cr$out.type, use.print = "none", overwrite.values = FALSE, on.error = "error", use.commonmark = TRUE)
render.compiled.rmd(cr = NULL, txt = cr$body, envir = parent.frame(), fragment.only = FALSE, chunks = c("knit", "eval")[1], out.type = if (is.null(cr$out.type)) "html" else cr$out.type, use.print = "none", overwrite.values = FALSE, on.error = "error", use.commonmark = TRUE)
Render a list of dependencies as a list of singleton head tags TO DO: Some dependencies may use local file names... Those dependencies must be fully inlined...
render.deps.as.singletons.tags(deps, inline.local.files = TRUE)
render.deps.as.singletons.tags(deps, inline.local.files = TRUE)
Does not create /figure subfolder in current wd
render.rmd.in.temp(text, envir = parent.frame(), quiet = TRUE, ...)
render.rmd.in.temp(text, envir = parent.frame(), quiet = TRUE, ...)
Render a value in a format specified by out.type
render.value(val, out.type = "html", ...)
render.value(val, out.type = "html", ...)
extract #< if blocks from a rmd txt
replace.if.blocks(txt, envir = parent.frame(), call.list = NULL, block.df = NULL, warn.if.na = TRUE, del.rows.na = FALSE, if.df = NULL)
replace.if.blocks(txt, envir = parent.frame(), call.list = NULL, block.df = NULL, warn.if.na = TRUE, del.rows.na = FALSE, if.df = NULL)
replace whiskers using a list of values, with several options
replace.whiskers(str, values = parent.frame(), eval = TRUE, signif.digits = NULL, vector.return.first = use.print == "none", pos = NULL, error.val = "`Error`", empty.val = NULL, use.whisker.render = FALSE, whisker.start = "{{", whisker.end = "}}", use.print = c("none", "knit", "whisker")[1])
replace.whiskers(str, values = parent.frame(), eval = TRUE, signif.digits = NULL, vector.return.first = use.print == "none", pos = NULL, error.val = "`Error`", empty.val = NULL, use.whisker.render = FALSE, whisker.start = "{{", whisker.end = "}}", use.print = c("none", "knit", "whisker")[1])
May be useful if blocks evaluate whiskers themselves or after rmd to html transformation
reverse.whisker.placeholders(txt, ph = cr$ph, cr = NULL)
reverse.whisker.placeholders(txt, ph = cr$ph, cr = NULL)
Extract rmd txt between start.line and end.line tag
rmd.between.start.end.lines(txt, start.line = "<!-- START -->", end.line = "<!-- END -->", return.start.end = FALSE)
rmd.between.start.end.lines(txt, start.line = "<!-- START -->", end.line = "<!-- END -->", return.start.end = FALSE)
TODO: Need to deal with nested blocks: replace from inner to outer
rmd.blocks.to.placeholders(txt, block.df = NULL, whisker.prefix = "{{", whisker.postfix = "}}", del.rows.na = FALSE, ignore.types = NULL, only.types = NULL, add.info = TRUE, ...)
rmd.blocks.to.placeholders(txt, block.df = NULL, whisker.prefix = "{{", whisker.postfix = "}}", del.rows.na = FALSE, ignore.types = NULL, only.types = NULL, add.info = TRUE, ...)
Replace chunks with placeholderss of the form id
rmd.chunks.to.placeholders(txt, whisker.prefix = "{{", whisker.postfix = "}}", del.rows.na = FALSE, add.info = TRUE, id = NULL)
rmd.chunks.to.placeholders(txt, whisker.prefix = "{{", whisker.postfix = "}}", del.rows.na = FALSE, add.info = TRUE, id = NULL)
Replace whiskers with placeholders of the form id
rmd.whiskers.to.placeholders(txt, whisker.prefix = "{{", whisker.postfix = "}}", add.info = TRUE)
rmd.whiskers.to.placeholders(txt, whisker.prefix = "{{", whisker.postfix = "}}", add.info = TRUE)
Like paste0 but returns an empty vector if some string is empty
sc(..., sep = "", collapse = NULL)
sc(..., sep = "", collapse = NULL)
A simple html page
simple.html.page(head, body)
simple.html.page(head, body)
Splits a text vector into different blocks by a start line token
## S3 method for class 'by.line.start' split(txt, start.with = NULL, block.lines = NULL, add.start = TRUE, merge.lines = TRUE)
## S3 method for class 'by.line.start' split(txt, start.with = NULL, block.lines = NULL, add.start = TRUE, merge.lines = TRUE)
a data frame
knitr data.frame printer as nice HTML table with several options
table.knit_print.data.frame(x, table.max.rows = 100, round.digits = 8, signif.digits = 8, html.data.frame = TRUE, show.col.tooltips = TRUE, col.tooltips = NULL, output = "html", options = NULL, ...)
table.knit_print.data.frame(x, table.max.rows = 100, round.digits = 8, signif.digits = 8, html.data.frame = TRUE, show.col.tooltips = TRUE, col.tooltips = NULL, output = "html", options = NULL, ...)
View an extended rmd file
view.html(file = NULL, text = if (!is.null(file)) readLines(file, warn = FALSE) else NULL, ui = NULL, browser = rstudio::viewer, dir = getwd())
view.html(file = NULL, text = if (!is.null(file)) readLines(file, warn = FALSE) else NULL, ui = NULL, browser = rstudio::viewer, dir = getwd())
View an extended rmd file
view.rmd(file = NULL, text = readLines(file, warn = FALSE), envir = list(), start.line = "<!-- START -->", end.line = "<!-- END -->", set.utf8 = TRUE, knit = !chunks.like.whisker, chunks.like.whisker = FALSE, out.type = "shiny", launch.browser = rstudio::viewer, use.commonmark = FALSE)
view.rmd(file = NULL, text = readLines(file, warn = FALSE), envir = list(), start.line = "<!-- START -->", end.line = "<!-- END -->", set.utf8 = TRUE, knit = !chunks.like.whisker, chunks.like.whisker = FALSE, out.type = "shiny", launch.browser = rstudio::viewer, use.commonmark = FALSE)
Print a whisker object
whisker_print(x, ...)
whisker_print(x, ...)
Need to implement different methods
## Default S3 method: whisker_print(x, ...)
## Default S3 method: whisker_print(x, ...)
Write text file in UTF-8 format
writeUtf8(x, file, bom = F)
writeUtf8(x, file, bom = F)