r-Rstudio可以运行markdown生成的编织html文件中嵌入的代码吗



是否需要进一步阐述的问题是。1.写入rmd文件2.Rmd被编织成html。3.Html与代码一起保存。我需要Rstudio来读取Html文件,识别其中的代码并运行它。有办法吗?

假设:

---
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Blah Blah
```{r cars}
summary(cars)
```
```{r pressure, echo=FALSE}
plot(pressure)
```
```{r}
xdf <- rbind.data.frame(mtcars, mtcars)
library(tidyverse) # rly bad place to put this but it's just a demo
just6 <- filter(xdf, cyl == 6)
```
```{r}
ggplot(mtcars, aes(wt, mpg)) + geom_point()
```
```{r}
count(just6, gear)
```

和一个名为"forso.Rmd"的文件,该文件被编织成"forso.html":

library(rvest)
pg <- read_html("forso.html")
html_nodes(pg, "pre.r") %>% 
html_text() %>% 
styler::style_text() %>%
write_lines("my-recovered-r-code.R") %>% 
cat(sep="n")
## summary(cars)
## xdf <- rbind.data.frame(mtcars, mtcars)
## 
## library(tidyverse) # rly bad place to put this but it's just a demo
## just6 <- filter(xdf, cyl == 6)
## ggplot(mtcars, aes(wt, mpg)) + geom_point()
## count(just6, gear)

上面的"##"只是输出的一个假象。

注意您将无法取回include=FALSEecho=FALSE代码(如上所示(。

另请注意使用styler是100%可选的。

相关内容

最新更新