I am quite excited about the upcoming RStudio notebooks (available in the preview version of RStudio. For a short overview click here). However, I am encountering some difficulties with including images.
In Rmarkdown, I can include images like this:
---
title: "This works"
output: html_document
---
```{r echo=FALSE, out.width='10%'}
library(knitr)
knitr::include_graphics('https://www.rstudio.com/wp-content/uploads/2014/06/RStudio-Ball.png')
```
However, when I want to do the same in a notebook (note the change from html_document to html_notebook), I no longer get an image:
---
title: "This does not work"
output: html_notebook
---
```{r echo=FALSE, out.width='10%'}
knitr::include_graphics('https://www.rstudio.com/wp-content/uploads/2014/06/RStudio-Ball.png')
```
(When I use an image in the same folder as my notebook, I simply get the name of that image, just as is the case with the link to the external image).
My question: is there a way to display the image also in the notebook (update:) inside a rmarkdown code-chunk?
Please note: I want to use r-code to include the image. I do not want to include images with standard markdown (![image description](path/to/image)), which works in both the notebook and the regular rmarkdown document. I also do not want to use html. My hope would be that using r-code to include the image would render the image in the notebook.
Edit: One difference between regular Rmarkdown files and notebooks is that notebooks are "previewed" and not knit:
Thanks for taking the time to try out the notebooks. There is a roundabout way you can do this today; just make a plot and draw the image on it.
download.file("https://www.rstudio.com/wp-content/uploads/2014/06/RStudio-Ball.png", "RStudio-Ball.png")
library("png")
ball <- readPNG("RStudio-Ball.png", native = TRUE)
plot(0:1, 0:1, type = "n", ann = FALSE, axes = FALSE)
rasterImage(ball, 0, 0, 1, 1)
It's a bit of a hack, though, so we just added support for knitr::include_graphics. It'll be in tomorrow's daily (0.99.1272 or later).
Related
Similar questions have been asked before about inserting an image from a url (for example here and here ). However, I can't seem to get those solutions to work for me.
I am trying to create an R package vignette and a github.io vignette page. Initially I was saving my images to my machine and when building the vignette I was calling them locally via:
knitr::include_graphics("file/path/image.png")
However, when I use devtools::build_site() to build the github.io site, the images are understandably missing from the vignette. I have uploaded all the images to my GitHub repo and I was wondering if there is any way to display to these images when building the GitHub.io site?
I have tried the following but neither have worked. I just get a blank space where the image should be.
<center><src="https://github.com/tidyverse/ggplot2/blob/main/icons/coord_cartesian.png"></center>
```{r out.width="100%"}
url <- "https://github.com/tidyverse/ggplot2/blob/main/icons/coord_cartesian.png"
knitr::include_graphics(url)
```
My YAML looks like this:
---
title: "myPackage"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{vivid}
%\VignetteEncoding{UTF-8}
%\VignetteEngine{knitr::rmarkdown}
editor_options:
chunk_output_type: console
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "vig/"
)
options(rmarkdown.html_vignette.check_title = FALSE)
```
I figured out the solution to my problem from this. I needed to use the "raw" absolute link from the image. So in the example above, I should have used:
```{r out.width="100%"}
url <- "https://raw.githubusercontent.com/tidyverse/ggplot2/main/icons/coord_cartesian.png"
knitr::include_graphics(url)
```
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:
When using officedown::rdocx_document in R Mardown, the Office Word output will include the plots as a .jpg image although my desire is to automatically include the plots of my R markdown in format .emf (Enhanched Metafile).
I know that with the function ggsave() you can adapt the device to .emf like in this example:
plot(mtcars$mpg)
ggsave("plot.emf", width = 10, height = 6, scale = 1, device = {function(filename, ...) devEMF::emf(file = filename, ...)})
Is there a way to do a workaround ggsave to change the device output of the plots in a RMarkdown (.Rmd) using officedown::rdocx_document as the output?
Here the answer to my own question after some research.
If you don't want to ggsave() and then knitr::include_graphics() every plot of your .Rmd in .emf (Enchanced Metafile) because there are a lot of them, then consider changing the graphic device of your .Rmd so that when it is exported to Word through the amazing package officedown (shout out to David Gohel) all the plots would be inserted in .emf extension that is more suitable for print publications than .png o .svg. Here the code workaround (is a simple change in your setup chunk but really makes the difference):
```{r setup, include=FALSE}
pacman::p_load("devEMF")
knitr::opts_chunk$set(echo = FALSE,
warning = FALSE,
message = FALSE,
dev = "emf",
fig.ext = "emf")
```
I hope it helps the community that uses officedown for Office Word Reports.
I'd like to output an accessible version of a presentation I'm making with R Markdown ioslides. At this point, I've been struggling to find much documentation on alt-text for dynamically generated images - not pointers to images - there's lots of information available online on that front.
Seems like the easiest thing to do might be to output my document as a word_document instead of a presentation, and ensure that figures have captions, using fig.cap argument in the chunk header.
However, I'd like the captions to be available only as alt-text - so that this document doesn't look strange to folks who aren't using screen reader tech (I'd like to make my alt-text nice and descriptive, but for folks who can see ok, this will seem odd, because the graph will do the talking instead).
I've included a reprex below. How can I still include the fig.cap as alt-text in the output word document, but not have it show up underneath the graph?
Also, if you are aware of any good resources on making RMarkdown docs accessible, I am all ears!!
Thanks for your help.
---
title: 'test doc'
author: "Nova"
date: "April 2020"
output:
word_document:
fig_height: 5
fig_width: 10
height: 500
widescreen: yes
width: 500
---
```{r setup and comments, include=FALSE}
knitr::opts_chunk$set(echo = FALSE,
message = F,
warning = F,
fig.height = 5,
fig.width = 10,
fig.cap = NA)
library(ggplot2)
```
## cars
```{r ggplot, include=T, fig.cap = "this is some alternative text about this figure"}
ggplot(cars)
```
I'd go back to some kind of HTML output, because it's simpler.
If you want ioslides output, you can use fig.cap="Some alt text" in the chunk option to set the alt text for the figure. Unfortunately, this also generates a visible caption, but you can suppress that. For one slide, just insert inline CSS:
<style>
.caption {
display:none;
}
</style>
somewhere on the slide before the figure. To do it for the whole presentation, put that text in a .css file and include it with YAML like
output:
ioslides_presentation:
css: style.css
It's probably also possible to change the template for this output style, but that looks like a lot more work.
I am practicing data analysis using R programming. I want my files to be on github. I am unable to figure out why github is not showing the output of .rmd files.
Here is the link to the file in my github repository Data Analysis Practice
I want the output including plots to be shown on github.
Instead of:
output: html_document
make it:
output: rmarkdown::github_document
(assuming you have the latest rmarkdown installed which you should if you're using RStudio — which I suspect you are — and keep it updated or update packages regularly).
It will create Exploring_one_variable.md and render the images as files.
When you browse to that markdown file, the images will render.
An alternative is to use:
output:
html_document:
keep_md: true
and it will render to both Exploring_one_variable.md and Exploring_one_variable.html so you'll have the best of both worlds without the local github-esque preview that the former provides.
You can get fancier and put something like the following as the first code section in the Rmd:
```{r, echo = FALSE}
knitr::opts_chunk$set(
fig.path = "README_figs/README-"
)
```
which will put the figures in a directory of your choosing.
You can see this in action here as the README.md was generated with knitr from the README.Rmd in the same directory.
There is now output: github_document