Functions for extracting information for Rmd nodes.
Usage
rmd_node_label(x)
rmd_node_label(x) <- value
# Default S3 method
rmd_node_label(x) <- value
# S3 method for class 'rmd_chunk'
rmd_node_label(x) <- value
rmd_node_type(x)
rmd_node_length(x)
rmd_node_content(x)
rmd_node_attr(x, attr)
rmd_node_engine(x)
rmd_node_options(x, yaml_style = TRUE)
rmd_node_code(x)
rmd_node_options(x) <- value
# Default S3 method
rmd_node_options(x) <- value
# S3 method for class 'rmd_chunk'
rmd_node_options(x) <- value
rmd_node_attr(x, attr) <- value
# Default S3 method
rmd_node_attr(x, attr) <- value
# S3 method for class 'rmd_node'
rmd_node_attr(x, attr) <- value
rmd_node_content(x) <- value
# Default S3 method
rmd_node_content(x) <- value
# S3 method for class 'rmd_chunk'
rmd_node_content(x) <- value
# S3 method for class 'rmd_raw_chunk'
rmd_node_content(x) <- value
# S3 method for class 'rmd_markdown'
rmd_node_content(x) <- value
# S3 method for class 'rmd_code_block'
rmd_node_content(x) <- value
# S3 method for class 'rmd_code_block_literal'
rmd_node_content(x) <- value
rmd_node_set_label(x, value)
rmd_node_set_options(x, ...)
rmd_node_set_content(x, value)
rmd_node_set_attr(x, attr, value)
Arguments
- x
An rmd object, e.g.
rmd_ast
orrmd_tibble
.- value
The new value to assign (for assignment functions).
- attr
Attribute name to extract or set.
- yaml_style
logical, if
TRUE
(default) return option names in YAML style (with hyphens), ifFALSE
return normalized style (with dots)- ...
For
rmd_node_set_options()
, named arguments that will be converted to a list of options to assign.
Value
rmd_node_label()
- returns a character vector of node labels, nodes without labels returnNA
.rmd_node_label<-()
- assigns new labels to chunk nodes. For the setter, returns the modified object.rmd_node_type()
- returns a character vector of node types.rmd_node_length()
- returns an integer vector of node lengths (i.e. lines of code, lines of text, etc.), nodes without a length returnNA
.rmd_node_content()
- returns the raw character vector(s) of node textual content (lines/code), nodes without content returnNULL
.rmd_node_attr()
- returns the value of a given node attribute (S7 property), returnsNULL
if the attribute does not exist.rmd_node_engine()
- returns a character vector of chunk engines,NA
for all other node types.rmd_node_options()
- returns a list of chunk node options (named list),NULL
for all other node types. Option names are returned in YAML style (with hyphens) by default, or normalized style (with dots) ifyaml_style = FALSE
.rmd_node_options<-()
- assigns new options to chunk nodes by merging with existing options. Takes a named list of options. For the setter, returns the modified object.rmd_node_attr<-()
- assigns new attribute values to nodes. For the setter, returns the modified object.rmd_node_code()
- returns a list of chunk node code (character vector),NULL
for all other node types.rmd_node_set_label()
- pipeable version ofrmd_node_label<-()
for setting node labels.rmd_node_set_options()
- pipeable version ofrmd_node_options<-()
for setting chunk options.rmd_node_set_attr()
- pipeable version ofrmd_node_attr<-()
for setting node attributes.rmd_node_content<-()
- assigns new content to nodes. For the setter, returns the modified object.rmd_node_set_content()
- pipeable version ofrmd_node_content<-()
for setting node content.
Examples
rmd = parse_rmd(system.file("examples/hw01.Rmd", package="parsermd"))
rmd_node_label(rmd)
#> [1] NA NA "load-packages" NA
#> [5] NA NA NA NA
#> [9] NA NA NA "plot-dino"
#> [13] NA "cor-dino" NA NA
#> [17] NA NA "plot-star" NA
#> [21] "cor-star"
rmd_node_type(rmd)
#> [1] "rmd_yaml" "rmd_heading" "rmd_chunk" "rmd_heading" "rmd_markdown"
#> [6] "rmd_heading" "rmd_markdown" "rmd_heading" "rmd_markdown" "rmd_heading"
#> [11] "rmd_markdown" "rmd_chunk" "rmd_markdown" "rmd_chunk" "rmd_heading"
#> [16] "rmd_markdown" "rmd_heading" "rmd_markdown" "rmd_chunk" "rmd_markdown"
#> [21] "rmd_chunk"
rmd_node_content(rmd)
#> [[1]]
#> NULL
#>
#> [[2]]
#> NULL
#>
#> [[3]]
#> [1] "library(tidyverse) " "library(datasauRus)"
#>
#> [[4]]
#> NULL
#>
#> [[5]]
#> [1] "Based on the help file, how many rows and how many columns does the `datasaurus_dozen` file have? What are the variables included in the data frame? Add your responses to your lab report. When you're done, commit your changes with the commit message \"Added answer for Ex 1\", and push."
#>
#> [[6]]
#> NULL
#>
#> [[7]]
#> [1] "(Type your answer to Exercise 1 here. This exercise does not require any R code.)"
#>
#> [[8]]
#> NULL
#>
#> [[9]]
#> [1] "Plot `y` vs. `x` for the `dino` dataset. Then, calculate the correlation coefficient between x and y for this dataset."
#>
#> [[10]]
#> NULL
#>
#> [[11]]
#> [1] "(The answers for this Exercise are given for you below. But you should clean up some of the narrative so that it only includes what you want to turn in.)"
#> [2] ""
#> [3] "First let's plot the data in the dino dataset:"
#>
#> [[12]]
#> [1] "dino_data <- datasaurus_dozen %>%"
#> [2] " filter(dataset == \"dino\")"
#> [3] ""
#> [4] "ggplot(data = dino_data, mapping = aes(x = x, y = y)) +"
#> [5] " geom_point()"
#>
#> [[13]]
#> [1] "And next calculate the correlation between `x` and `y` in this dataset:"
#>
#> [[14]]
#> [1] "dino_data %>%" " summarize(r = cor(x, y))"
#>
#> [[15]]
#> NULL
#>
#> [[16]]
#> [1] "Plot `y` vs. `x` for the `star` dataset. You can (and should) reuse code we introduced above, just replace the dataset name with the desired dataset. Then, calculate the correlation coefficient between x and y for this dataset. How does this value compare to the r of dino?"
#>
#> [[17]]
#> NULL
#>
#> [[18]]
#> [1] "(Add code and narrative as needed. Note that the R chunks are labeled with `plot-star` and `cor-star` to provide spaces to place the code for plotting and calculating the correlation coefficient. To finish, clean up the narrative by removing these instructions.)"
#> [2] ""
#> [3] "Blah blah blah..."
#>
#> [[19]]
#> [1] ""
#>
#> [[20]]
#> [1] "I'm some text, you should replace me with more meaningful text..."
#>
#> [[21]]
#> [1] ""
#>
rmd_node_attr(rmd, "level")
#> [[1]]
#> NULL
#>
#> [[2]]
#> [1] 3
#>
#> [[3]]
#> NULL
#>
#> [[4]]
#> [1] 3
#>
#> [[5]]
#> NULL
#>
#> [[6]]
#> [1] 4
#>
#> [[7]]
#> NULL
#>
#> [[8]]
#> [1] 3
#>
#> [[9]]
#> NULL
#>
#> [[10]]
#> [1] 4
#>
#> [[11]]
#> NULL
#>
#> [[12]]
#> NULL
#>
#> [[13]]
#> NULL
#>
#> [[14]]
#> NULL
#>
#> [[15]]
#> [1] 3
#>
#> [[16]]
#> NULL
#>
#> [[17]]
#> [1] 4
#>
#> [[18]]
#> NULL
#>
#> [[19]]
#> NULL
#>
#> [[20]]
#> NULL
#>
#> [[21]]
#> NULL
#>
rmd_node_engine(rmd)
#> [1] NA NA "r" NA NA NA NA NA NA NA NA "r" NA "r" NA NA NA NA "r"
#> [20] NA "r"
rmd_node_options(rmd)
#> [[1]]
#> NULL
#>
#> [[2]]
#> NULL
#>
#> [[3]]
#> [[3]]$message
#> [1] FALSE
#>
#>
#> [[4]]
#> NULL
#>
#> [[5]]
#> NULL
#>
#> [[6]]
#> NULL
#>
#> [[7]]
#> NULL
#>
#> [[8]]
#> NULL
#>
#> [[9]]
#> NULL
#>
#> [[10]]
#> NULL
#>
#> [[11]]
#> NULL
#>
#> [[12]]
#> [[12]]$`fig-height`
#> [1] 3
#>
#> [[12]]$`fig-width`
#> [1] 6
#>
#>
#> [[13]]
#> NULL
#>
#> [[14]]
#> named list()
#>
#> [[15]]
#> NULL
#>
#> [[16]]
#> NULL
#>
#> [[17]]
#> NULL
#>
#> [[18]]
#> NULL
#>
#> [[19]]
#> named list()
#>
#> [[20]]
#> NULL
#>
#> [[21]]
#> named list()
#>
rmd_node_code(rmd)
#> [[1]]
#> NULL
#>
#> [[2]]
#> NULL
#>
#> [[3]]
#> [1] "library(tidyverse) " "library(datasauRus)"
#>
#> [[4]]
#> NULL
#>
#> [[5]]
#> NULL
#>
#> [[6]]
#> NULL
#>
#> [[7]]
#> NULL
#>
#> [[8]]
#> NULL
#>
#> [[9]]
#> NULL
#>
#> [[10]]
#> NULL
#>
#> [[11]]
#> NULL
#>
#> [[12]]
#> [1] "dino_data <- datasaurus_dozen %>%"
#> [2] " filter(dataset == \"dino\")"
#> [3] ""
#> [4] "ggplot(data = dino_data, mapping = aes(x = x, y = y)) +"
#> [5] " geom_point()"
#>
#> [[13]]
#> NULL
#>
#> [[14]]
#> [1] "dino_data %>%" " summarize(r = cor(x, y))"
#>
#> [[15]]
#> NULL
#>
#> [[16]]
#> NULL
#>
#> [[17]]
#> NULL
#>
#> [[18]]
#> NULL
#>
#> [[19]]
#> [1] ""
#>
#> [[20]]
#> NULL
#>
#> [[21]]
#> [1] ""
#>
chunk = rmd_chunk("r", "example", code = "1 + 1")
rmd_node_label(chunk)
#> [1] "example"
rmd_node_label(chunk) = "new_name"
rmd_node_label(chunk)
#> [1] "new_name"
rmd_node_options(chunk) = list(eval = FALSE, echo = TRUE)
rmd_node_options(chunk)
#> $eval
#> [1] FALSE
#>
#> $echo
#> [1] TRUE
#>
rmd_node_attr(chunk, "engine") = "python"
rmd_node_attr(chunk, "engine")
#> [1] "python"
rmd_node_content(chunk) = c("x = 2", "y = 3")
rmd_node_content(chunk)
#> [1] "x = 2" "y = 3"
chunk = rmd_chunk("r", "example", code = "1 + 1") |>
rmd_node_set_label("new_label") |>
rmd_node_set_options(eval = FALSE, echo = TRUE) |>
rmd_node_set_content(c("a = 1", "b = 2"))
rmd_node_label(chunk)
#> [1] "new_label"
rmd_node_options(chunk)
#> $eval
#> [1] FALSE
#>
#> $echo
#> [1] TRUE
#>
rmd_node_options(chunk, yaml_style = FALSE) # get in normalized style
#> $eval
#> [1] FALSE
#>
#> $echo
#> [1] TRUE
#>
rmd_node_content(chunk)
#> [1] "a = 1" "b = 2"
chunk = rmd_chunk("r", "example", code = "x = 1") |>
rmd_node_set_attr("engine", "python")
rmd_node_engine(chunk)
#> [1] "python"