如何从Markdown文件(*.md)中提取R代码?

9

我尝试使用以下R命令生成一个R文件:

library(knitr)
purl("/Readme.md")

但是我得到了一个空的 R 文件。
2个回答

6
正如mnel所建议的那样,您的问题可能在于'/'。因此,请先确保您在正确的目录中: ```html

正如 mnel 所建议的那样,你的问题可能在于 '/ '。 确保你在正确的目录下:

```
getwd()

然后检查您的文件是否存在:

file.exists("Readme.md")

那么这个应该可以工作:
knitr::purl("Readme.md")
##Or
knitr::purl("./Readme.md")

这个代码是否真的提取了 R 代码呢?当我尝试使用 knitr::purl("https://gist.githubusercontent.com/brshallo/01496b68adef88a71de1fd44f3712b10/raw/3c769cbc1399846607aca48750c9ebbfc1bc4ecc/grouped-nested-t-test.md") 时,它只返回一个空文件...?我以为 purl 只能用于 RMD 文件而不是 md 文件? - Bryan Shalloway

0

快速函数,用于从 .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
#> 

函数的功能还比较粗糙,假设三个反引号只用于创建代码块(例如,如果它们出现在引用的字符串中,可能会导致代码错误)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接