我尝试了以下 R 命令以生成 R 文件:
library(knitr)
purl("/Readme.md")
但我得到一个 R 空文件。
正如 mnel 所暗示的,您的问题可能与'/'有关。所以首先确保你在正确的目录中:
getwd()
然后检查您的文件是否存在:
file.exists("Readme.md")
那么这应该工作:
knitr::purl("Readme.md")
##Or
knitr::purl("./Readme.md")
用于仅从 .md 文件中解析代码块然后将它们写入本地临时文件的快速函数:
library(dplyr)
library(stringr)
subset_even <- function(x) x[!seq_along(x) %% 2]
extract_code_md <- function(file_path){
lines <- readr::read_file(file_path) %>%
stringr::str_split("```.*", simplify = TRUE) %>%
subset_even() %>%
stringr::str_flatten("\n## new chunk \n")
file_output <- tempfile(fileext = ".R")
writeLines(lines, file_output)
file_output
}
让我们在 dplyr 的 github 页面上的“README.md”文件中使用这个函数。
readr::read_file(extract_code_md('https://raw.githubusercontent.com/tidyverse/dplyr/main/README.md')) %>%
writeLines()
#>
#> # The easiest way to get dplyr is to install the whole tidyverse:
#> install.packages("tidyverse")
#>
#> # Alternatively, install just dplyr:
#> install.packages("dplyr")
#>
#> ## new chunk
#>
#> # install.packages("devtools")
#> devtools::install_github("tidyverse/dplyr")
#>
#> ## new chunk
#>
#> library(dplyr)
#>
#> starwars %>%
#> filter(species == "Droid")
#> #> # A tibble: 6 <d7> 14
#> #> name height mass hair_color skin_color eye_color birth_year sex gender
#> #> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr>
#> #> 1 C-3PO 167 75 <NA> gold yellow 112 none masculi<U+0085>
#> #> 2 R2-D2 96 32 <NA> white, blue red 33 none masculi<U+0085>
#> #> 3 R5-D4 97 32 <NA> white, red red NA none masculi<U+0085>
#> #> 4 IG-88 200 140 none metal red 15 none masculi<U+0085>
#> #> 5 R4-P17 96 NA none silver, red red, blue NA none feminine
#> #> # <U+0085> with 1 more row, and 5 more variables: homeworld <chr>, species <chr>,
#> #> # films <list>, vehicles <list>, starships <list>
#>
#> starwars %>%
#> select(name, ends_with("color"))
#> #> # A tibble: 87 <d7> 4
#> #> name hair_color skin_color eye_color
#> #> <chr> <chr> <chr> <chr>
#> #> 1 Luke Skywalker blond fair blue
#> #> 2 C-3PO <NA> gold yellow
#> #> 3 R2-D2 <NA> white, blue red
#> #> 4 Darth Vader none white yellow
#> #> 5 Leia Organa brown light brown
#> #> # <U+0085> with 82 more rows
#>
#> starwars %>%
#> mutate(name, bmi = mass / ((height / 100) ^ 2)) %>%
#> select(name:mass, bmi)
#> #> # A tibble: 87 <d7> 4
#> #> name height mass bmi
#> #> <chr> <int> <dbl> <dbl>
#> #> 1 Luke Skywalker 172 77 26.0
#> #> 2 C-3PO 167 75 26.9
#> #> 3 R2-D2 96 32 34.7
#> #> 4 Darth Vader 202 136 33.3
#> #> 5 Leia Organa 150 49 21.8
#> #> # <U+0085> with 82 more rows
#>
#> starwars %>%
#> arrange(desc(mass))
#> #> # A tibble: 87 <d7> 14
#> #> name height mass hair_color skin_color eye_color birth_year sex gender
#> #> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr>
#> #> 1 Jabba De<U+0085> 175 1358 <NA> green-tan<U+0085> orange 600 herm<U+0085> mascu<U+0085>
#> #> 2 Grievous 216 159 none brown, wh<U+0085> green, y<U+0085> NA male mascu<U+0085>
#> #> 3 IG-88 200 140 none metal red 15 none mascu<U+0085>
#> #> 4 Darth Va<U+0085> 202 136 none white yellow 41.9 male mascu<U+0085>
#> #> 5 Tarfful 234 136 brown brown blue NA male mascu<U+0085>
#> #> # <U+0085> with 82 more rows, and 5 more variables: homeworld <chr>, species <chr>,
#> #> # films <list>, vehicles <list>, starships <list>
#>
#> starwars %>%
#> group_by(species) %>%
#> summarise(
#> n = n(),
#> mass = mean(mass, na.rm = TRUE)
#> ) %>%
#> filter(
#> n > 1,
#> mass > 50
#> )
#> #> # A tibble: 8 <d7> 3
#> #> species n mass
#> #> <chr> <int> <dbl>
#> #> 1 Droid 6 69.8
#> #> 2 Gungan 3 74
#> #> 3 Human 35 82.8
#> #> 4 Kaminoan 2 88
#> #> 5 Mirialan 2 53.1
#> #> # <U+0085> with 3 more rows
#>
功能很粗糙,并且假设三个反引号仅用于制作代码块(例如,如果它们出现在带引号的字符串或其他内容中,则会破坏代码)。