I am searching for a way to produce a standalone HTML document from a chunk in Rmarkdown.
My example is this:
I have an Rmarkdown document for my analysis.
In one chunk of code, I produce interactive plotly plots that are quit long to load when I open the HTML.
I am looking for an option that would create another HTML document for this particular chunk and put a link to it in its place in the master HTML document.
I am sure I could manage to do something like that with a bit of tweaking with another script, but I would like to know if there is not a simpler option first.
Thanks
Yes, you can nest documents using knitr child documents. Put the chunk you want to isolate in its own Rmd (say, child.Rmd), then use this syntax to insert it in the master document:
```{r child='child.Rmd'}
```
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'm creating a tutorial that involves telling the reader what to put into a file we'll call utils.R. The user would get the tutorial as an HTML file. Throughout the tutorial utils.R changes and the Rmd document uses the code in utils.R as it exists at that stage of the tutorial. During the rendering, I'd like for the code chunks to use source("utils.R") as it exists at that stage of the tutorial. I'm looking for a way to either...
1. Write the contents of a code chunk to a file. For example...
```{r utils_1}
summary(cars)
median(cars$speed)
```
Is there a way to write the code in utils_1 to a file?
2. Create a nicely formatted code chunk from a text string (I know how to write that to a file). For example...
z <- "summary(cars)\nmedian(cars$speed)"
write(z, "utils.R")
Will generate utils.R, but is there a way to turn z into a properly formatted code chunk.
I could create multiple versions of utils.R and use echo=F to hide that I'm loading that behind the scenes, but that seems like a pain.
Not sure if this is what are you looking for but you can use child option to generate them from another file. I use it for automated reports as it helps to keep the main Rmd a bit simpler
```{r child=utils.R}
```
I often place the child code in the YAML though, and call it (matter of tastes I guess...):
---
params:
utils: "utils.R"
---
```{r child=params$utils}
```
I am using knit()and markdownToHTML() to automatically generate reports.
The issue is that I am not outputting plots when using these commands. However, when I use RStudio's Knit HTML button, the plots get generated. When I then use my own knit/markdown function, it suddenly outputs the plot. When I switch to another document and knit that one, the old plot appears.
Example:
```{r figA, result='asis', echo=TRUE, dpi=300, out.width="600px",
fig=TRUE, fig.align='center', fig.path="figure/"}
plot(1:10)
```
Using commands:
knit(rmd, md, quiet=TRUE)
markdownToHTML(md, html, stylesheet=style)
So I guess there are 2 questions, depending on how you want to approach it:
What magic is going on in Rstudio's Knit HTML?
How can I produce/include without depending on RStudio's Knit HTML button?
The only issue I see here is that this doesn't work when you have the chunk options {...} spanning two lines. If it's all on one line, it works fine. Am I missing something?
See how this is not allowed under knitr in the documentation:
Chunk options must be written in one line; no line breaks are allowed inside chunk options;
RStudio must handle linebreaks in a non-standard way.
This is really embarrassing, I really thought I read the documentation carefully:
include: (TRUE; logical) whether to include the chunk output in the
final output document; if include=FALSE, nothing will be written into
the output document, but the code is still evaluated and plot files
are generated if there are any plots in the chunk, so you can manually
insert figures; note this is the only chunk option that is not cached,
i.e., changing it will not invalidate the cache
Simply adding {..., include=TRUE} did the trick. I would say it would be a pretty sensible default though.
I am looking to render a 2 column report as a stand-alone HTML file using R and Markdown only. I am very new to markdown within R, so I need some help with the layout.
The image below displays the layout of what I would like to render using RMarkdown.
The HTML is on the left hand side and some data along the right hand side.
The raw HTML and the example dataframe can be found here:
Note: I used the pander package to create the table using the following command:
pandoc.table(df, style="rmarkdown")
Although this is not a perfect solution, it is a place to get started: Yihui recently added HTML templates to knitr, and docco is a example two-column page: http://cran.r-project.org/web/packages/knitr/vignettes/docco-classic.html .
You can see the template file used for that output here: https://github.com/yihui/knitr/blob/master/inst/misc/docco-template.html.
Alternatively, you can try placing inline HTML right in your R Markdown chunks, but this is terribly hacky and you might feel like a bad person for doing it. We use results='asis' so that the cated HTML is rendered properly, and out.extra='' to ensure that the HTML used to generate the figures is generated right away, rather than the Markdown language for image inclusion.
```{r two-column, results='asis', echo=FALSE, out.extra=''}
library(knitr)
cat("<table class='container'><tr>")
cat("<td>")
plot( rnorm(10) )
cat("</td>")
cat("<td>")
kable( rnorm(10), format="html" )
cat("</td>")
cat("</tr></table>")
```
Calling knit on that should produce a 2 column layout for that particular chunk (although without any nice styling for the table; you might add that in yourself with some CSS)
I work in R and deliver PDF file with Sweave, sweave(.rnw document). I need to put into my text a JPEG image. I didn't find any function on the web and have no ideas about how I could do that.
You include it just like you include an image in any other LaTeX document:
http://amath.colorado.edu/documentation/LaTeX/reference/figures.html#pdf
\includegraphics{myimage.jpg}
Put it in a LaTeX block not an R code block. As the link points out, you'll need to compile with pdflatex.