15

How to include a plotly plot in a Rpres file? If you do it like in a normal Rmd file

Basic Plot
========================================================
```{r, echo=FALSE}
library(plotly)
plot_ly(economics, x = date, y = unemploy / pop)
```

The result looks as follows: Error in file(con, "rb") : cannot open the connection

The solution I came up with, which uses the possibility that Markdown can contain HTML:

Basic Plot
========================================================
```{r, results='hide', echo=FALSE}
library(plotly)
p = plot_ly(economics, x = date, y = unemploy / pop)
htmlwidgets::saveWidget(as.widget(p), file = "demo.html")
```
<iframe src="demo.html" style="position:absolute;height:100%;width:100%"></iframe>

But I am hoping for a somehow more elegant solution which does not use any additional files.

3
  • Nice workaround, thanks. Alternatively, plotly works fine with ioslides rendered from Rmarkdown. Commented Aug 19, 2016 at 9:59
  • @Antoine Can you elaborate a bit on this workflow?
    – jakob-r
    Commented Dec 8, 2016 at 10:42
  • I have added a minimal example on how to do this as an answer below. Let me know if this is not what you had in mind. Commented Dec 8, 2016 at 12:34

2 Answers 2

6

The following is a minimal example on how to include a plot_ly graph in an ioslides presentation, so it does not quite answer the question for Rpres, but provides an alternative.

The first slide displays a plot transformed from a ggplot into a plot_ly, preserving the ggplot style. The second slide displays a plot using plot_ly directly.

---
title: "Plot_ly demo"
date: "8 December 2016"
output: ioslides_presentation
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```

## A simple plot_ly

```{r, fig.align='center', message = FALSE}
library(plotly)

df <- data.frame(x =  1:10, y = (1:10)^2)

p <- ggplot(df, aes(x = x, y = y)) + geom_line() + labs(x = "X", y = "Y", title = "X and Y")

ggplotly(p)
```

## Another simple plot_ly

```{r, echo = FALSE, fig.align = 'center', message = FALSE}
plot_ly(df, x = x, y = y)
```
-1

Had the same problem. When I executed slidify(index.Rmd), there was a message saying PhantomJS not found, and suggesting me run webshot::install_phantomjs(). So I did and the error was gone. However I still got no plotly interactive map output. It was blank.

Also tried the following code in terminal, which worked for some people but not for me. I got html file output, and there still wasn't a map. It comes from this post. It might work for you.

Rscript -e "library(knitr); library(rmarkdown); 
rmarkdown::render('index.Rmd', output_file='index.html')"

I am sure it is plotly. Cause ggplots works fine.

Update:

Reinstalled/updated the wetshot package by running install.packages("webshot"), then ran webshot::install_phantomjs() again, then library(knitr); library(rmarkdown); rmarkdown::render('index.Rmd', output_file='index.html'). It worked. The html file has a plotly map, though it doesn't appear in the Knitr preview window.

Update:

By adding the following code, I am able to display the map in the sides. Refer to this post.

htmlwidgets::saveWidget(as_widget(p), "p.html")
cat('<iframe src="./p.html" width=100% height=100% allowtransparency="true"> </iframe>')

Full context would be something listed below.

library(plotly)
cities <- readRDS("D:/R/data/cn_cities.rds")
cities <- cities[1:50,]

geo <- list(
  scope = 'asia',
  projection = list(type = 'Mercator'),
  showland = TRUE,
  landcolor = toRGB("gray85"),
  countrycolor = toRGB("white"),
  subunitcolor = toRGB("white"),
  countrywidth = 1,
  subunitwidth = 1)

p <- plot_geo(cities, 
              locationmode='CHN', 
              sizes=c(1, 200)) %>% 
     add_markers(x=~lng, y=~lat, 
                 size=~sqrt(population),
                 hoverinfo="text", 
                 text=~paste(city, "<br />", population)) %>%
     layout(title='', 
            geo=geo)

htmlwidgets::saveWidget(as_widget(p), "p.html")
cat('<iframe src="./p.html" width=100% height=100% allowtransparency="true"> </iframe>')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.