Skip to content

Extract R code from R Markdown HTML file #1811

@stevecondylios

Description

@stevecondylios

There appears to be no fast and easy way to extract the R code from HTML files generated via R Markdown.

Example

Max and Davis's applied-ml workshop is a good example.

We can easily get the R code for 'Part_1.html', since we have access to the original .Rmd file, and can hence call

knitr::purl("Part_1.Rmd")
readLines("Part_1.R") %>% paste0(collapse="\n\n") %>% cat
# Displays R code...

But we cannot so easily get the R code for parts 2 through 5, as the originating .Rmd is not available.

Possible solution

html_to_r() extracts the R code from R Markdown generated HTML files.

I provide an implementation in a PR.

Using in the applied-ml example

We can now easily retrieve the R code from the .html files, like so

# from inside applied-ml
dir() %>% grep("Part_{1}.*html", ., value = T) %>% sapply(., html_to_r) -> a
dir() %>% grep("Part_{1}.*html", ., value = T) %>% mapply(html_to_r, inc_out=F, .) -> b

# Randomly inspect the second file with / without output to ensure it worked as expected
a[[2]] %>% cat # with output
b[[2]] %>% cat # without output

This can be merged if relevant or disregarded if not relevant.

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureFeature requests

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions