Mermaid diagram in Quarto/Rmarkdown: narrow and blurry - r

I'm trying to generate a quarto document in pdf (and word later). I'm meeting an issue with the mermaid graph.
Please find bellow a sample .qmd file to illustrate the issue.
So first it's supposed to support the {mermaid} tag but when i do that, I can't "run" the cell in rstudio, but also my document doesn't actually generated and the "background jobs" in rstudio gets stuck on "output file: testmermaid.knit.md"
So I use DiagrammeR::mermaid to encapsulate this, then the document is indeed generated but the size is too small and if I resize the figure generated then it becomes all blurry.
I made many attempts but none of them worked so if someone would have a simple solution where the diagram would go Xcm from the left to Ycm to the right, and be generated correctly in pdf and word?
EDIT:
After #Shafee's comment, I've noticed I didn't have the quarto package installed, I have now installed the latest R package quarto 1.0.36 and reinstalled RStudio (2022.07.1 build 554) for the sake of it, but the problem is unchanged.
I've also noticed my personal computer (same version of RStudio and quarto) doesn't have these issues (still can't run a mermaid chunk in Rstudio IDE though)
EDIT2:
after a full reinstall on the local hard drive (instead of windows network drive) I get this:
output file: testmermaid.knit.md
ERROR: Couldn't find open server
my version is now:
quarto::quarto_version()
1 ‘1.1.189’
---
title: "TestMermaid"
format: pdf
---
```{r libimport}
library(quarto)
library(DiagrammeR)
knitr::opts_chunk$set(echo = F)
```
## Quarto
Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see <https://quarto.org>.
Attempt #1
```{mermaid attempt1}
flowchart LR
A[Hard edge] --> B(Round edge)
B --> C{Decision}
C --> D[Result one]
C --> E[Result two]
```
Attempt #2
```{r attempt2}
mermaid("
sequenceDiagram
participant ParticipantA
participant ParticipantB
participant ParticipantC
ParticipantA->>ParticipantB: I want something
ParticipantB->>ParticipantC: he want something
ParticipantC->>ParticipantB: here is something
ParticipantB->>ParticipantA: he got something for you
", height = '100%', width = '100%')
```

Your code executes as intended once the library(quarto) call is removed from line 7.
As per the documentation for Quarto figures. The size can be increase through the use of chunk options.
```{mermaid attempt1}
%%| fig-width: 6.5
%%| fig-cap: Fig 1. Test figure
flowchart LR
A[Hard edge] --> B(Round edge)
B --> C{Decision}
C --> D[Result one]
C --> E[Result two]
```

Related

static image of targets workflow, programatically

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:

How to make options(width = 60) persist across code chunks in R Markdown used as notebook in RStudio?

I have an R Markdown file that I want to use as a notebook inside RStudio by interactively executing individual (!) code chunks and seeing their results, e.g., prints, tables, plots, displayed under the code chunk. In particular, I am not interested in knitting the entire R Markdown file at this point. The .Rmd file contains nothing but the following code chunks A, B, and C.
```{r A}
options("width") # => 96 on my system
```
```{r B}
options(width = 60)
options("width") # => 60
```
```{r C}
options("width") # => 96 but I expected 60
```
When I open a fresh instance of RStudio, open the .Rmd file, clear the knitr cache using RStudio's "Clear Knitr Cache..." Knit-menu item, and run the chunks, one by one, in the listed order using RStudio's chunk-specific "Run Current Chunk" buttons, I get 96, the default width on my system, for chunk A, 60 for chunk B, and 96 for chunk C.
Why does the new global width set in chunk B not persist into chunk C?
I am using RStudio version 1.4.1717 with rmarkdown version 2.10 and knitr version 1.34 on macOS Catalina.
A similar question about options(digits = N) was asked here and the single answer was accepted by the asker. However, the example given in the answer fails to demonstrate persistence of R's global options across chunks as the relevant code chunks include their own options(digits = ...) calls. When I tested the example from that answer on my system using an additional code chunk without options(digits = ...) call, the "digits" value set in the previous chunk did not persist.
This only happens when using the document as a Notebook in RStudio. If you knit the whole document, you should see the expected behaviour.
I suspect the reason is that RStudio sets the option depending on the size of your edit window. I don't see any option in it that would let you change this behaviour, but you might be able to find one if you look at the .rs.rnb.* objects in the tools:rstudio environment. That's initially loaded in position 2 in the search() list.
I'm not sure what you are knitting to, but I had similar issues trying to keep the text when knitting to a markdown file from wrapping with a hard cut-off of 60, and setting the options(width = XXX) never seemed to work for me.
Instead, in the YAML header you can tell pandoc not to wrap, but rather preserve the text as you have typed it in the .Rmd.
output:
md_document:
pandoc_args: ["--wrap=preserve"]
Again, not sure if you are knitting to an .md file, but this might be useful.

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!

Rmarkdown and rstudio - reveal js print pdf.css error

After I have created a revealjs-presentation in my Rstudio-project, i proceed to open it. This works fine except that I get an error in my browser (Chrome) that pdf.css doesn't exist. Which it doesn't, because the presentation is looking for it in a sub-folder in my project directory. Now, I can add pdf.css according to the path that fails and the presentation works...until I re-run it and the added directory is deleted (I guess the knit process delete the aformentioned sub-folder?)
This is a minor problem but there seems to be something buggy going on here, anyone got any idea what I could do (besides re-installing everything)?
Example at the bottom
---
title: "Untitled"
output: revealjs::revealjs_presentation
---
## R Markdown
This is an R Markdown presentation. 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.
## Slide with Bullets
- Bullet 1
- Bullet 2
- Bullet 3
## Slide with R Code and Output
```{r}
summary(cars)
```
## Slide with Plot
```{r, echo=FALSE}
plot(cars)
```
And this is the folder-tree that's missing (relpath from a given Rstudio project):
Failed to load resource: net::ERR_FILE_NOT_FOUND
/TestRevealJs_files/reveal.js-3.3.0/css/print/paper.css
Where TestRevealJs is the name of the project.

Ending a document in r Markdown and continuing with code

I am writing a report using r markdown. However, after this report is produced I would like to continue to analyse the data without having to open up a new editor window.
I was wondering if there is a simple command I could use to express the end of the document?
Thanks.
It appears that you are not necessarily interested in ending your markdown document but in hiding your results. The code below will enable you to continue the analysis in the same window and to exclude it from appearing in the core document when compiled.
```{r results='hide', message=FALSE, warning=FALSE}
# Stuff that you want to do
```
For a more detailed explanation, you may want to have a look at the Chunk Options in the knitr documentation.

Resources