Abstract base class for all R Markdown AST nodes
Functions for extracting information for Rmd nodes.
Usage
rmd_node()
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, ...)
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_set_label(x, value, ...)
rmd_node_set_options(x, value, ...)
rmd_node_set_attr(x, attr, value, ...)
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 a character vector of node textual content, nodes without content returnNA
.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.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.
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] NA
#> [2] NA
#> [3] "library(tidyverse) \nlibrary(datasauRus)"
#> [4] NA
#> [5] "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] NA
#> [7] "(Type your answer to Exercise 1 here. This exercise does not require any R code.)"
#> [8] NA
#> [9] "Plot `y` vs. `x` for the `dino` dataset. Then, calculate the correlation coefficient between x and y for this dataset."
#> [10] NA
#> [11] "(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.)\n\nFirst let's plot the data in the dino dataset:"
#> [12] "dino_data <- datasaurus_dozen %>%\n filter(dataset == \"dino\")\n\nggplot(data = dino_data, mapping = aes(x = x, y = y)) +\n geom_point()"
#> [13] "And next calculate the correlation between `x` and `y` in this dataset:"
#> [14] "dino_data %>%\n summarize(r = cor(x, y))"
#> [15] NA
#> [16] "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] NA
#> [18] "(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.)\n\nBlah blah blah..."
#> [19] ""
#> [20] "I'm some text, you should replace me with more meaningful text..."
#> [21] ""
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] ""
#>
# Assignment examples
chunk = rmd_chunk("r", "example", code = "1 + 1")
rmd_node_label(chunk) # "example"
#> [1] "example"
rmd_node_label(chunk) = "new_name"
rmd_node_label(chunk) # "new_name"
#> [1] "new_name"
# Setting options
rmd_node_options(chunk) = list(eval = FALSE, echo = TRUE)
rmd_node_options(chunk) # List with eval=FALSE, echo=TRUE
#> $eval
#> [1] FALSE
#>
#> $echo
#> [1] TRUE
#>
# Setting attributes
rmd_node_attr(chunk, "engine") = "python"
rmd_node_attr(chunk, "engine") # "python"
#> [1] "python"
# Pipeable versions for chaining operations
chunk = rmd_chunk("r", "example", code = "1 + 1") |>
rmd_node_set_label("new_label") |>
rmd_node_set_options(list(eval = FALSE, echo = TRUE))
rmd_node_label(chunk) # "new_label"
#> [1] "new_label"
rmd_node_options(chunk) # List with eval=FALSE, echo=TRUE
#> $eval
#> [1] FALSE
#>
#> $echo
#> [1] TRUE
#>
# Set engine via attribute
chunk = rmd_chunk("r", "example", code = "x = 1") |>
rmd_node_set_attr("engine", "python")
rmd_node_engine(chunk) # "python"
#> [1] "python"