'How to extract R code from a Markdown file (*.md)?
I tried the following R commands in order to generate an R file:
library(knitr)
purl("/Readme.md")
But I get an R empty file.
Solution 1:[1]
As mnel suggests, your problem is probably with '/'. So first make sure you are in the correct directory:
getwd()
Then check your file exists:
file.exists("Readme.md")
Then this should work:
knitr::purl("Readme.md")
##Or
knitr::purl("./Readme.md")
Solution 2:[2]
Quick function for parsing only the code chunks out from a .md file and then writing them to a local temp file:
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
}
Let's use this function on the "README.md" file from dplyr's github page.
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
#>
Function is rough and subject to the assumption that three backticks are only used to make code chunks (e.g. if they show-up in a quoted character string or something, will break code).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Waldir Leoncio |
| Solution 2 | Bryan Shalloway |
