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.
Related
I'm trying to embed a static image of a targets workflow in an rmarkdown document. I tried to do this by using tar_mermaid, defining a target that writes the workflow in mermaid format mm <- tar_mermaid(); writeLines(mm, "target_mermaid.js") but the help for tar_mermaid says
You can visualize the graph by copying
the text into a public online mermaid.js editor or a mermaid GitHub code chunk
I am looking for a programmatic way to either (1) embed the Javascript output in an (R)markdown file, or (2) render it (as SVG, PNG, whatever).
I thought as a shortcut that I could cut-and-paste into a markdown code chunk delimited by ```mermaid, or use cat(readLines("target_mermaid.js"), sep = "\n") in a chunk with results = "asis" but I guess that only works in Github markdown (I'm using Pandoc to render to HTML) ... ?
The visNetwork package has a visSave() function which can save to HTML (not quite what I wanted but better than what I've managed so far), and a visExport() function (which saves to PNG etc. but only by clicking in a web browser). Furthermore, targets wraps the visNetwork functions in a way that is (so far) hard for me to unravel (i.e., it doesn't return a visNetwork object, but automatically returns a widget ...)
For the time being I can go to https://mermaid.live, paste in the mermaid code, and export the PNG manually but I really want to do it programmatically (i.e. as part of my workflow, without manual steps involved).
I am not quite sure about the answer. But I have an idea. And I will delete if it is not adequate:
If you want execute mermaid code to get for example an html output then you could do this with quarto. I am not sure if this is possible with rmarkdown:
See https://quarto.org/docs/authoring/diagrams.htmlS
---
title: "Untitled"
format: html
editor: visual
---
## Quarto
Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see <https://quarto.org>.
## Running Code
```{mermaid}
flowchart LR
A[Hard edge] --> B(Round edge)
B --> C{Decision}
C --> D[Result one]
C --> E[Result two]
```
output:
#landau's suggestion to look here almost works, if I'm willing to use Quarto instead of Rmarkdown (GH Markdown is not an option). The cat() trick was the main thing I was missing. The .qmd file below gets most of the way there but has the following (cosmetic) issues:
I don't know how to suppress the tidyverse startup messages, because targets is running the visualization code in a separate R instance that the user has (AFAIK) little control of;
the default size of the graph is ugly.
Any further advice would be welcome ...
---
title: "targets/quarto/mermaid example"
---
```{r}
suppressPackageStartupMessages(library("tidyverse"))
library("targets")
```
```{r, results = "asis", echo = FALSE}
cat(c("```{mermaid}", tar_mermaid(), "```"), sep = "\n")
```
Beginning of document:
Zooming out:
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!
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.
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
I have a .Rpres file in RStudio. I would like to include code, but not have it run (I am only showing the code to explain how it works). Is it possible to accomplish this (and ensure that it will not produce errors, because it is not running)?
Have you tried eval=FALSE in the knitr code chunk options? e.g.:
```{r eval=FALSE}
print("Don't run me")
```
{r, eval=F, echo=T} will include the R source code in the output file while it is not evaluated
Posting for anyone who may come across this like I have. I've found that for small examples (if you don't want to use chunks), you can also just use back ticks like you would with regular markdown inline, but just don't add the "r" at the beginning:
`plot(cars)`
Will print the code itself, but will not print the plot.