R Markdown Presentation not loading/ rendering interactive Plotly chart - r

I am using Plotly with R to create a chart that will be rendered in a R Markdown Presentation With Ioslides, but instead of showing the demo chart from the website like the following:
It is rendering the steps like this:
My code is pretty simple:
---
title: "R Markdown Presentation & Plotly"
author: "Eduardo Almeida"
date: "February 19, 2017"
output: ioslides_presentation
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
## Interactive plot with Plotly
```{r}
library(plotly)
p <- plot_ly(economics, x = ~date, y = ~unemploy / pop)
```

As Karthik Arumugham pointed out you need to display the plot, either by entering p or not assigning plot_ly to variable but calling it directly.
I'd suggest to explicitly state the missing variables (type='scatter', mode='markers') instead of suppressing the output messages. In addition you could add {r, warning=F} to get rid of the
Error: attempt to use zero-length variable name
message.
---
title: "R Markdown Presentation & Plotly"
author: "Eduardo Almeida"
date: "February 19, 2017"
output: ioslides_presentation
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
## Interactive plot with Plotly
```{r, warning=F}
suppressPackageStartupMessages({library(plotly)})
library(plotly)
plot_ly(economics, x = ~date, y = ~unemploy / pop, type='scatter', mode='markers')
```

Related

Multiple figures side-by-side not closing TeX's figure block with plotly content

I would like to align several plotly figures in two columns with only one caption in an Rmd producing PDF output. I have tried to use plotly's subplot within the code block which was not successful, as I want each sub-figure to have its own legend. I am now trying to work with the chunk options fig.show='hold' and fig.ncol=2 and it appears that the tex-File that is knitted does not close the corresponding figure environment, causing the resulting PDF to contain a literal \begin{figure}. This problem apparently does not occur with base R figures, as per the reproducible example below.
Is there any chance that anyone here has had a similar issue before or some other remedies to prevent this from happening?
Reproducible example
Rmd-file:
---
title: "Untitled"
author: "Bob the Builder"
date: "11/15/2021"
output:
pdf_document:
latex_engine: xelatex
keep_tex: true
---
## Base R
```{r echo=FALSE, message=FALSE, results=FALSE, fig.show='hold', fig.ncol=2, fig.cap = "Plot caption goes here.", out.width="0.46\\textwidth"}
plot(iris$Sepal.Length, iris$Petal.Length)
plot(cars)
```
## Plotly
```{r echo=FALSE, message=FALSE, results=FALSE, fig.show='hold', fig.ncol=2, fig.cap = "Plot caption goes here.", out.width="0.46\\textwidth"}
plotly::plot_ly(iris, x = ~Sepal.Length, y = ~Petal.Length, type = 'scatter', mode = 'markers')
plotly::plot_ly(cars, x = ~speed, y = ~dist, type = 'scatter', mode = 'markers')
```
TeX file contains the following:
\hypertarget{base-r}{%
\subsection{Base R}\label{base-r}}
\begin{figure}
\includegraphics[width=0.46\textwidth]{test_files/figure-latex/unnamed-chunk-1-1} \includegraphics[width=0.46\textwidth]{test_files/figure-latex/unnamed-chunk-1-2} \caption{Plot caption goes here.}\label{fig:unnamed-chunk-1}
\end{figure}
\hypertarget{plotly}{%
\subsection{Plotly}\label{plotly}}
\textbackslash begin\{figure\}
\includegraphics[width=0.46\textwidth]{test_files/figure-latex/unnamed-chunk-2-1}
\includegraphics[width=0.46\textwidth]{test_files/figure-latex/unnamed-chunk-2-2}
with a missing \end{figure} and \caption in the plotly-Section.

Rayshader in Rmarkdown file?

When I render a Rayshader graphic, it pops open Xquartz on my mac, no problem, but what if I wanted to include it in my Rmarkdown document, it just shows the code, no graphic? I understand this is a heavy graphic intensive render, but looking for any tips. thanks, below is my code:
---
title: "rayshader"
author: "Daniel"
date: "6/16/2020"
output:
html_document:
self_contained: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r cars}
library(rayshader)
#Here, I load a map with the raster package.
loadzip = tempfile()
download.file("https://tylermw.com/data/dem_01.tif.zip", loadzip)
localtif = raster::raster(unzip(loadzip, "dem_01.tif"))
unlink(loadzip)
#And convert it to a matrix:
elmat = raster_to_matrix(localtif)
elmat %>%
sphere_shade(texture = "desert") %>%
add_water(detect_water(elmat), color = "desert") %>%
add_shadow(ray_shade(elmat, zscale = 3), 0.5) %>%
add_shadow(ambient_shade(elmat), 0) %>%
plot_3d(elmat, zscale = 10, fov = 0, theta = 135, zoom = 0.75, phi = 45, windowsize = c(1000, 800))
```
From the package owner:
To embed the plot into an RMarkdown document, you need to call rgl::rglwidget() after bringing up your plot. If you're embedding multiple plots, you will also have to close to previous plot using rgl::rgl.close() before plotting the next one.
reference
Worked for me.
Try adding this to the end of your code:
Sys.sleep(0.2)
render_snapshot()
I am able to generate interactive .HTML files of rayshader's 3D renders through the following approach in an R Markdown, using RStudio:
title: "title"
author: "author"
date: "date"
output: html_document
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(rayshader)
library(MetBrewer)
library(tidyverse)
library(rgl)
knitr::knit_hooks$set(webgl = hook_webgl)
The key to embedding the 3D render is on the hook_webgl() function in knitr. The issue is discussed in more length in this thread:
including a interactive 3D figure with knitr
, including a reference to the alternative writeWebGL() function.

Render ggplotly() in R Markdown github_document

Interactive graphs created with ggplotly() go along nicely with html_document output in R Markdown, see eg. RMarkdown and ggplotly. For github_document output, however, the knitted HTML preview file does not show ggplotly() graphs.
I adopted the code from the linked SO post and only changed the output format in the header. Does anyone know how to render plotly graphs correctly with this kind of output? Or at least, if that is even possible?
---
title: "RmarkdownExample"
author: "fdetsch"
date: "April 16, 2020"
output: github_document
---
Here is the graph I generated.
```{r setup, message = FALSE, echo = FALSE, warning=FALSE}
# Require
library(plotly)
# Create
dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
# Plot
g <- qplot(carat, price, data=dsamp, colour=clarity)
# Call
ggplotly(g)
```
For output: github_document, I found a workaround that renders ggplotly() graphs nicely using iframes. The trick is to export the plotly widget as HTML and subsequently embed it as iframe. In my opinion, the advantage over output: html_document with keep_md enabled is that the online .md file simply prints a link to the intermediary HTML file instead of the full widget code, making it much tidier.
---
title: "Render `ggplotly()` graphs in `github_document`"
author: "fdetsch"
date: "`r Sys.Date()`"
output: github_document
---
Here is the graph I generated.
```{r setup, message = FALSE, echo = FALSE, warning = FALSE}
# Require
library(plotly)
# Create
dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
# Plot
g <- qplot(carat, price, data=dsamp, colour=clarity)
# Call
p <- ggplotly(g)
```
```{r include, echo = FALSE}
htmlwidgets::saveWidget(p, "index.html")
htmltools::tags$iframe(
src=file.path(getwd(), "index.html"),
width="100%",
height="600",
scrolling="no",
seamless="seamless",
frameBorder="0"
)
```
At least when opening the preview HTML in an external viewer, the interactive graph shows up. The RStudio (preview version 1.3.938) viewer currently fails to render the image.
There seem to be some problems with github_document, see here. My workaround: knit to html_document and save the resulting *.md-file.
So the YAML header is:
---
title: "RmarkdownExample"
author: "fdetsch"
date: "April 16, 2020"
output:
html_document:
keep_md: true
---
You can then use the md file to upload to github.

Set the proportions of a gauge (from flexdashboard) plot in a RMD report

I'm using rmarkdown to make a report containing some gauge charts from flexdashboard, the problem is that I can't change the proportion of the chart.
Here's a reproducible example of the code I wrote:
---
title: "Reproducible Example"
author: "Name"
date: "3/6/2020"
output:
word_document: default
html_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(dplyr)
mtcars <- tibble::rownames_to_column(mtcars,"model_car")
MazdaRX4 <- mtcars[1,]
```
## Cars Gauge
A gauge displays a numeric value on a meter that runs between specified minimum and maximum values.
```{r fig1, fig.height = 1}
flexdashboard::gauge(MazdaRX4$mpg, min = 0, max = 50, symbol = 'mpg')
```
But when I knit the report my chart looks like this:
Is there a way to set the proportion of this kind of chart?
thanks in advance to anybody who can help!

add_tooltip in ggvis with ioslides

I'm trying to add a ggvis plot with a tooltip to an ioslides presentation using knitr:
---
title: "Untitled"
output: ioslides_presentation
---
```{r options}
library(knitr)
```
## Slide with ggvis plot
```{r, message=FALSE, warning=FALSE, echo=FALSE}
library(ggvis)
cars %>%
ggvis(~speed, ~dist) %>%
layer_points(fill.hover := "red") %>%
add_tooltip(function(data) data$dist)
```
If I plot the graph in RStudio the tooltip shows up properly in the Viewer, but when I knit it to html, only the fill.hover works, and no tooltip shows up.

Resources