How to include plotly in R Studio Presentations (Rpres)





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







11















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.










share|improve this question























  • Nice workaround, thanks. Alternatively, plotly works fine with ioslides rendered from Rmarkdown.

    – Antoine Vernet
    Aug 19 '16 at 9:59











  • @Antoine Can you elaborate a bit on this workflow?

    – jakob-r
    Dec 8 '16 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.

    – Antoine Vernet
    Dec 8 '16 at 12:34


















11















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.










share|improve this question























  • Nice workaround, thanks. Alternatively, plotly works fine with ioslides rendered from Rmarkdown.

    – Antoine Vernet
    Aug 19 '16 at 9:59











  • @Antoine Can you elaborate a bit on this workflow?

    – jakob-r
    Dec 8 '16 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.

    – Antoine Vernet
    Dec 8 '16 at 12:34














11












11








11


1






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.










share|improve this question














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.







r rstudio plotly htmlwidgets rpres






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Aug 19 '16 at 9:14









jakob-rjakob-r

2,47711839




2,47711839













  • Nice workaround, thanks. Alternatively, plotly works fine with ioslides rendered from Rmarkdown.

    – Antoine Vernet
    Aug 19 '16 at 9:59











  • @Antoine Can you elaborate a bit on this workflow?

    – jakob-r
    Dec 8 '16 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.

    – Antoine Vernet
    Dec 8 '16 at 12:34



















  • Nice workaround, thanks. Alternatively, plotly works fine with ioslides rendered from Rmarkdown.

    – Antoine Vernet
    Aug 19 '16 at 9:59











  • @Antoine Can you elaborate a bit on this workflow?

    – jakob-r
    Dec 8 '16 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.

    – Antoine Vernet
    Dec 8 '16 at 12:34

















Nice workaround, thanks. Alternatively, plotly works fine with ioslides rendered from Rmarkdown.

– Antoine Vernet
Aug 19 '16 at 9:59





Nice workaround, thanks. Alternatively, plotly works fine with ioslides rendered from Rmarkdown.

– Antoine Vernet
Aug 19 '16 at 9:59













@Antoine Can you elaborate a bit on this workflow?

– jakob-r
Dec 8 '16 at 10:42







@Antoine Can you elaborate a bit on this workflow?

– jakob-r
Dec 8 '16 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.

– Antoine Vernet
Dec 8 '16 at 12:34





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.

– Antoine Vernet
Dec 8 '16 at 12:34












2 Answers
2






active

oldest

votes


















3














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)
```





share|improve this answer































    -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>')





    share|improve this answer


























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f39035308%2fhow-to-include-plotly-in-r-studio-presentations-rpres%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3














      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)
      ```





      share|improve this answer




























        3














        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)
        ```





        share|improve this answer


























          3












          3








          3







          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)
          ```





          share|improve this answer













          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)
          ```






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 8 '16 at 12:33









          Antoine VernetAntoine Vernet

          23647




          23647

























              -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>')





              share|improve this answer






























                -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>')





                share|improve this answer




























                  -1












                  -1








                  -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>')





                  share|improve this answer















                  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>')






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 2 days ago

























                  answered Nov 22 '18 at 12:32









                  Nov05Nov05

                  13




                  13






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f39035308%2fhow-to-include-plotly-in-r-studio-presentations-rpres%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Guess what letter conforming each word

                      Port of Spain

                      Run scheduled task as local user group (not BUILTIN)