Created
July 21, 2020 17:20
-
-
Save zkamvar/6e14e424c89eb393d3ace78b30c274e0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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