I am running into a problem when trying to include a web-based image within a R Markdown PDF document.
Minimal Example:
---
title: "Random"
output: pdf_document
---
![Benjamin Bannekat](https://octodex.github.com/images/bannekat.png)
Knitting the above results in the error:
! Package pdftex.def Error: File `https://octodex.github.com/images/bannekat.pn
g' not found.
However, if I use the following code, the image shows up:
---
title: "Random"
output:
html_document: default
html_notebook: default
---
![Benjamin Bannekat](https://octodex.github.com/images/bannekat.png)
The same code works fine when output is HTML.
How can I make the image show up in a PDF document?
If you are knitting to PDF, the file has to run through LaTeX. LaTeX has no built-in capacity to display files hosted on the web, as highlighted in this question.
The best workaround for this is to download the web image to your local directory, and then specify this as a file path.
The following code downloads the image using the download.file function, and then displays in using include_graphics. The benefit of include graphics is that it allows you to specify the width of the image in the output document, which I set to 40 pixels.
---
title: "Random"
output: pdf_document
---
```{r results = 'asis', out.width="40px"}
download.file(url = "https://octodex.github.com/images/bannekat.png",
destfile = "image.png",
mode = 'wb')
knitr::include_graphics(path = "image.png")
```
Find out more reasons why include_graphics should be used to include graphics in R Markdown documents. Check out my other answer here for more details why.
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 am working within RStudio 1.1.383 using rmarkdown. I am trying to render to Latex a string that is created within a knitr chunk so that it appears as latex code within the document.
So far I have experimented with the 'results=' options in the chunk header and find that results='asis' enables it to be rendered once the document is knitted, but I have not been able to find a way to enable the result to be rendered using the preview feature that allows you to run a single chunk and see the results within the .Rmd editor.
Any help on this matter would be appreciated.
A minimal example is included below that should be copied into a .Rmd file before rendering.
Thanks in advance,
Michael
---
title: "Minimal Example"
output: html_document
---
```{r}
str <- "$$\\alpha \\cdot \\beta = \\delta$$"
# this doesn't show in the preview 'run chunk feature'
cat(str, "\n")
# neither does this
writeLines(str)
```
Is it possible to use pandoc-crossref in bookdown?
I tried to change the yaml header to:
output:
bookdown::tufte_book2:
toc: yes
highlight: tango
pandoc_args: -F /usr/local/bin/pandoc-crossref
Which should pass the filter to pandoc, but I get the error:
pandoc: Error running filter pandoc-crossref:
Could not find executable ' pandoc-crossref'.
The above error does not make sense, as I entered the correct path. What kind of env is bookdown using, which is precluding the access to the filter file?
Here is an example
---
output: bookdown::html_document2
---
# Section name {#id}
```{r pressure, echo=FALSE, fig.cap='test plot'}
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. To cross-reference the figure, use `\#ref(fig:pressure)` to produce Figure \#ref(fig:pressure). All this is found within the section \#ref(id).
Which produces...
See https://bookdown.org/yihui/bookdown/figures.html for the official documentation.
I had a similar issue while I was trying to put numbers to equations in Word files (SO question). I got the same error Could not find executable 'pandoc-crossref'.
My RStudio installation (on Windows) did not come with pandoc-crossref. Here is what I did:
Downloaded pandoc-crossref from here.
Find the path where RStudio saved the file pandoc.exe:
rmarkdown::find_pandoc()
Put the pandoc-crossref.exe in the folder I got in (2).
I'm getting started with rmarkdown and knitr. In the sample document provided by RStudio, I can successfully use render() to generate an HTML file that views fine in Chrome. However, when I click on the knit button, it generates a .markdown file and then returns the following error without rendering the preview:
Error generating HTML preview for ~/path/to/file/report.rmarkdown system error 2 (The system cannot find the file specified)
I think it's getting hung up at the pandoc stage. Is it possible that RStudio is looking for pandoc in the wrong place? Pandoc was already installed at C:\Program Files (x86)\Pandoc\pandoc.exe, but RStudio installed its own instance at C:\Program Files\RStudio\bin\pandoc\pandoc.exe, so maybe it's looking in the wrong place and/or confusing settings from one with the other?
Any help would be greatly appreciated. Thanks!
And just in case, here's the RMarkdown template I'm starting with:
---
title: "Monthly Report"
author: "Kris Shaffer"
date: "February 17, 2017"
output: html_document
---
```{r setup, include=FALSE}
library(knitr)
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.
R version 3.3.2
RStudio 1.0.136
rmarkdown 1.3
knitr 1.15.1
pandoc 1.17.2 (both installations)
Turns out it was as simple as a file extension. I changed the filename from report.rmarkdown to report.Rmd. Knit works fine now.
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