Skip to content

Instantly share code, notes, and snippets.

@zkamvar
Created July 21, 2020 17:20
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save zkamvar/6e14e424c89eb393d3ace78b30c274e0 to your computer and use it in GitHub Desktop.
# 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