Tayeb
Tayeb

Reputation: 11

Code / Process for running rmarkdown in base R

All my codes developed in base R and I don't want to use RStudio, however I want to use rmarkdown feature in base R which is available in Rstudio.

I have downloaded rmarkdown package in base r, but not able to derive a code to publish my work

All the output of my codes written in R should be view able through web browser.

Upvotes: 1

Views: 927

Answers (1)

onlyphantom
onlyphantom

Reputation: 9603

First make sure you're using .Rmd as your file extension. If not, rename it to a .Rmd extension. Make sure you have Pandoc installed on your OS.

Next, add the following to the top of the file:

---
title: "Your notebook title"
output: html_document
---

output: could take any value. You can pass in the value of ioslides_presentation for example if you want but it looks like html_document fits the criteria of what you want pretty well.

Once you have that, write your code in any editor (or the R console if you prefer). Use the code chunks and markdown text formatting as you normally would:

```{r}
plot(1:10)
```

In my base R Console, this is how mynotebook.Rmd looks like: enter image description here

Finally, use the render() function from rmarkdown. You can either attach it and run render():

library(rmarkdown)
render("mynotebook.Rmd")

Or, run rmarkdown::render("mynotebook.Rmd").

Notice that the use of RStudio is not required at all since Pandoc is the document converter performing this task. For the so inclined, this is what its documentation has to say:

When you run render, R Markdown feeds the .Rmd file to knitr, which executes all of the code chunks and creates a new markdown (.md) document which includes the code and it's output.

The markdown file generated by knitr is then processed by pandoc which is responsible for creating the finished format.

This may sound complicated, but R Markdown makes it extremely simple by encapsulating all of the above processing into a single render function.

Upvotes: 2

Related Questions