Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract R code from a Markdown file (*.md)?

Tags:

r

knitr

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.

like image 518
user2042032 Avatar asked Sep 19 '13 01:09

user2042032


People also ask

How do I extract codes in RStudio?

Extract function RStudio comes with a tool that can help you build functions. To use it, highlight the lines of code in your R script that you want to turn into a function. Then click Code > Extract Function in the menu bar.

How do I export an R Markdown file?

To transform your markdown file into an HTML, PDF, or Word document, click the “Knit” icon that appears above your file in the scripts editor. A drop down menu will let you select the type of output that you want. When you click the button, rmarkdown will duplicate your text in the new file format.

How do I open an R Markdown file in R?

To open a new file, click File > New File > R Markdown in the RStudio menu bar. A window will pop up that helps you build the YAML frontmatter for the . Rmd file. Use the radio buttons to select the specific type of output that you wish to build.


1 Answers

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")
like image 55
csgillespie Avatar answered Sep 22 '22 22:09

csgillespie