Code / Process for running rmarkdown in base R - 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.

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:
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.

Related

Shiny Server missing charts generated by R markdown file

I have an R markdown file which I'd like to be available on my corporate Shiny Server.
According to R Markdown: The Definitive Guide I can add runtime: shiny to the YAML metadata at the top of the Rmd file to turn it into a Shiny document. I have done this and it works. If I click "Run Document" in RStudio it will run the Rmd and I will see the report with no problems.
My project is located in the ShinyApps directory where the Shiny Server is looking for apps to serve. When I hit the URL for this project I get the report without any charts. I just get broken image icons where the charts should be. (I am using RStudio Server so it is the exact same files being accessed by RStudio and Shiny Server).
R version 3.4.3, Shiny Server version 1.5.6.875
UPDATE: I have reproduced the behaviour with the simplest possible example. I created a new RStudio project - just a plain project - called TEST located in my ShinyApps directory. Then I created a new R Markdown file, which I called TEST.Rmd. This file is pre-populated with example RMarkdown using the cars and pressure built-in datasets. I changed the YAML header to include runtime: shiny. The RStudio "knit" button is replaced by the "Run Document" button, as expected, and clicking this runs the document and works as expected. Attempting to view the page via the Shiny Server has the same issue whereby the plot is not included; a broken image icon takes its place.
UPDATE 2: As requested, here is the Markdown file. It is literally the sample file generated by RStudio with the addition of runtime: shiny in the YAML header.
---
title: "Test RMarkdown"
author: "Michael Henry"
date: "4/6/2020"
output: html_document
runtime: shiny
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r cars}
summary(cars)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
UPDATE 3: So I went hunting around the server looking for a log file for Shiny Server. I do not have any administrator privileges so I've never looked for this before, but I found a log file which included this: /lib64/libpango-1.0.so.0: undefined symbol: g_log_structured_standard. It turns out there is a bug in RHEL which has a fix available so I have put a request in with my administrator to apply the fix. I will report back after this has been applied as to whether it resolved my issue.
UPDATE 4: It turns out my RHEL server is up-to-date; it already has the version of glib2 suggested by the bugfix. The fact that I am still getting this error is therefore something that my administrator is going to escalate to Red Hat.
UPDATE 5: The Red Hat support suggested there was another glib2 so file lying around and it turned out that this was the case. Removing this file resolved the issue!
So, I think you might need to add some extra code to make the markdown application run as you expect in the Shiny Server environment
try wrapping the charts and plots with the following:
renderPlot({})
This gives the server an order to both run the plot and then make it available to the Shiny document. Shiny is a weird thing to straight up R programmers.
You are basically computing and creating things in an R environment and handing them off to a Javascript/HTML one which needs to know in its own language what to do. That renderPlot({}) tells the JS component how to promote the R output to your page.
You can see from this section of the reference document you linked to what the format is and how it is promoted to the page by Shiny.
If you are still stuck after doing this, reach back out and I will try to help trouble shoot!
Searching for the error message: /lib64/libpango-1.0.so.0: undefined symbol: g_log_structured_standard led to a known issue with RHEL whereby there was an older version of glib2 on the system that did not contain the symbol g_log_structured_standard. In my case the server was up-to-date and had the correct version of glib2, but there was another version of this library lying around which was causing the issue.
The moral of the story: search for the log files early and follow the leads contained therein!

Compile Bookdown to Markdown?

Is there any way to take a Bookdown project, and build it as Markdown instead of HTML or TeX?
I ask because I need to post-process the final Markdown output from Bookdown, in order to extract R and Python notebooks for download.
In more detail, I am using Bookdown to build a textbook that embeds notebooks to download, where the notebooks contain subsets of the code and text in the bookdown .Rmd files. For example, a single chapter could contain more than one notebook.
In order to do this, I put start and end comment markers in the RMarkdown input text to identify the section that will be a notebook, and then post-process the generated Markdown files to extract the notebook section. As in something like:
<!--- notebook: first_section.Rmd
-->
Some explanation, maybe using Bookdown extra markup such as #a_citation.
```{r}
a <- 1
a
```
<!--- end of notebook
-->
More markdown.
```{r}
# More code not in notebook.
b <- 2
```
Obviously I could use the input RMarkdown pages, but this would be ugly, because all the extended Bookdown markup such as citations, cross-references and so on, would appear in raw and ugly form in the generated notebook. So I'd really like to be able to get the final output Markdown, after merging, resolving of citations and cross references. Is there any way of doing that?
My question is similar to this as-yet unanswered question, but adds my motivation for an official solution to this problem.
With the latest version of bookdown on CRAN, you can use the output format bookdown::markdown_document2, e.g.,
output:
bookdown::markdown_document2:
base_format: rmarkdown::md_document
variant: gfm

How can I convert a Rmd document to a jupyter notebook

I would like to convert a rmarkdown .Rmd document to a jupyter notebook .ipynb.
I found that converting from jupyter to rmd is easy using as described in reference page but for some reason (...) the Rstudio team did not do the other way.
For instance I would like to convert
---
title: "Untitled"
author: "statquant"
date: "03/09/2019"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r cars}
summary(cars)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
After checking, the silver bullet seems to be jupytext
It allows you to convert from or to markdown, rmarkdown, python, ipynb, etc.
This can actually allow you a pretty neat workflow
write a simple R script, script.R, that you can spin into a Rmd document
use knitr::spin('script.R', knit = FALSE) to transform it to Rmd
use jupytext --to notebook script.Rmd to create script.ipynb
share or execute the notebook
sos-rmarkdown provides yet another Rmarkdown to Jupyter notebook converter. The unique features include its support for inline expressions using markdown-kernel, use of multiple kernels in one notebook (using a SoS kernel) to accommodate code blocks in multiple languages, and the ability to execute generated notebook using sos-papermill. It also uses cell meta data to control the display of input and output of code blocks in Jupyter Lab and exported HTML reports.
To use this tool, you can install sos-rmarkdown from pip or conda-forge, then run the converter with command
sos convert input.Rmd output.ipynb
or use option --execute to execute the converted notebook
sos convert input.Rmd output.ipynb --execute
Disclaimer: I am the author of sos-rmarkdown.
Here is another way.
The detailed answer (to convert .rmd to .ipynb) is described here: https://gist.github.com/ramnathv/10012123
TL;DR
Use a 3rd-party Python package notedown with sed command as follows:
1) Install a 3rd-party python package which does the conversion for us
$ pip install notedown
2) Use the installed package to convert from your *.Rmd file (or *.md) to *.ipynb and run the terminal command:
$ notedown example.Rmd | sed '/%%r/d' > example.ipynb

render SQL in Rmarkdown notebook, not just when knit

If I have an R block in an Rmarkdown notebook, the code is visible in the rendered nb.html file. However, if I have a SQL block, I can't find a way to make the code visible in the rendered file.
However, if I generate an html_document (standard knitr) instead of an html_notebook, the rendered HTML does include the SQL code (and output).
Is there a way to get the notebook output to do something closer to what the knit output does?
I've also encountered this recently, Harlan, and began by asking if there's a way to get notebook output more like knit output.
I then wondered if there was a way to get knit output more like notebook output and realised that there was.
After asking the folks at RStudio, I learnt that you can add the following line to the YAML options in order to generate a html_document that, when rendered, enables the RMD to be downloaded. Specifically, use:
output:
html_document:
code_download: true
I now use that in all of my code and it works well. I hope it helps you, too.

Compile R Script to markdown

Using RStudio's "Compile Notebook to html" feature, I noticed that a temporary .md file was created in the process, but deleted automatically. At one point I was lucky enough to see its content, and it is exactly what I need: the code chunks alternate with output chunks, all perfectly formatted.
So my question is: how do I generate such an .md file directly form an R script?
You could also run knitr::spin() directly from the R console.
If you run "Knit to HTML", a markdown file is temporarily created and deleted. To keep the markdown file, add the following to the YAML code at the top of the RMD file.
---
output:
html_document:
keep_md: true
---
Here is a helpful reference: https://bookdown.org/yihui/rmarkdown/html-document.html#keeping-markdown

Resources