Skip to content

Instantly share code, notes, and snippets.

@zkamvar
Created Jul 21, 2020
Embed
What would you like to do?
# test file
sample_chunk <- "
```{r hello, testing=as.logical(1), another=1 + 1}
x <- 1 + 1 # this evaluates to two
```
the value of x is `r x`"
file = "sample.Rmd"
writeLines(sample_chunk, con = file)
# delete temp Rmd file
on.exit(unlink(file))
library(magrittr)
library(tinkr) # ropenscilabs/tinkr
library(xml2)
# Grab the file and all code blocks
tink <- tinkr::to_xml(file)
code <- tink$body %>% xml_find_all(".//*[self::d1:code_block or self::d1:code]")
print(code)
## {xml_nodeset (2)}
## [1] <code_block xml:space="preserve" language="r" name="hello" testing="as.logical(1)" another="1 ...
## [2] <code xml:space="preserve">r x</code>
xml_text(code)
## [1] "x <- 1 + 1 # this evaluates to two\n" "r x"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment