Plotly: iframes only work when pressing knit html button in rstudio

Created on 18 Apr 2014  Â·  14Comments  Â·  Source: ropensci/plotly

Or at least for some people the hook only works when pressing knit html button in rstudio.

Perhaps we need to edit the knit hook to make it work in any context.

bug

All 14 comments

Hm, I am not an expert in writing knit hooks, so do you have any ideas? In
practice I just use the knit html button in rstudio. The other context is
just using knit2html on the R command line?

Another thing that would be nice but I don't have any idea how to do is to
change the knit hook to automatically detect and embed plotlys (rather than
having to specify plotly=TRUE), any idea how to do that?

On Fri, Apr 18, 2014 at 2:13 AM, Scott Chamberlain <[email protected]

wrote:

Or at least for some people the hook only works when pressing knit html
button in rstudio.

Perhaps we need to edit the knit hook to make it work in any context.

—
Reply to this email directly or view it on GitHubhttps://github.com/ropensci/plotly/issues/12
.

When knitting from the command line, interactive() is true.

pub$ggplotly <- function(gg=last_plot()){
    if(!is.ggplot(gg)){
      stop("gg must be a ggplot")
    }
    pargs <- gg2list(gg)
    if(interactive()){ # we are on the command line.
      resp <- do.call(pub$plotly, pargs)
      browseURL(resp$url)
      invisible(list(data=pargs, response=resp))
    }else{ # we are in knitr/RStudio.
      do.call(pub$iplot, pargs)
    }
  }

Not sure how to get around this. Interactivity seems to be a property of the session. Is it possible to create a new non-interactive session from within the console?

I can not get the embedded iframes to work, tried both RStudio's 'knit html' button and knit(). Trying to replicate this: http://ropensci.org/blog/2014/04/17/plotly/.
Here's my sessionInfo()
R version 3.1.1 (2014-07-10)
Platform: x86_64-apple-darwin13.1.0 (64-bit)
locale: [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] digest_0.6.4 htmltools_0.2.6 rmarkdown_0.3.10 tools_3.1.1 yaml_2.1.13

Hello @ouzor !

Follow the syntax below when writing your .Rmd file:

plotly_knitr

You want to set the session parameter to "knitr" in the ggplotly() call and also add the plotly=TRUE option in the code chunk where you embed a Plotly plot.

Hope this helps!

@ouzor Note that after clicking "Knit HTML" I also had to "Open in Browser" in order to see the embedded Plotly plot. The plot was generated (you can check online in your Plotly account), it's just a rendering thing.

Thanks @mkcor, this works! I was not able to find much documentation for the ggplotly command, is that available somewhere?

@ouzor We are indeed behind with respect to documentation.
Now this is documented here: http://blog.plot.ly/post/106630252117/plot-with-ggplot2-and-plotly-within-knitr-reports
Thank you for your patience!

Thanks!
I made a short comparison of different interactive visualization packages in R, including plotly, see here: http://ouzor.github.io/blog/2014/11/21/interactive-visualizations.html
I like very much the idea of turning an existing ggplot2 object to a nice interactive version. However, as a data scientist I work a lot with confidential data and can not make any of that public, which to me seems that I can not use the free version of plotly at all...

Hi @ouzor

We definitely understand that’s a concern. Thanks for asking. A couple thoughts.

You control the privacy of your data, and can make your work public or keep it private (like Google Docs or GitHub).

You own your data; we don’t make an IP claim to it. We also have an on-premise version of plotly you can run on your own servers. Would any of these options work? Let me know if we can help with anything.

Thanks a lot @mkcor, I'll take a closer look at these options!

why does this not work??
library(shiny)
library(plotly)

Rely on the 'WorldPhones' dataset in the datasets

package (which generally comes preloaded).

library(datasets)

Define a server for the Shiny app

shinyServer(function(input, output) {

# Fill in the spot we created for a plot
output$phonePlot <- renderPlotly({

# Render a barplot

gg <- barplot(WorldPhones[,input$region]*1000,
main=input$region,
ylab="Number of Telephones",
xlab="Year")
})

p <- ggplotly(gg,session="knitr")
p
})

Despite following @mkcor's fix that seemed to work for @ouzor, I run into the listed error msg when running Knit HTML and when executing the code line-by-line from inside RStudio.

``` r, plotly=TRUE
require(ggplot2)
gg <- ggplot(cars) + geom_point(aes(speed, dist))
gg

require(plotly)
py <- plotly("aniruhil", "maskedkey")
py$ggplotly(gg, session="knitr")
```


Quitting from lines 27-30 (testingplotly.Rmd)
Error in eval(expr, envir, enclos) : attempt to apply non-function
Calls: ... handle -> withCallingHandlers -> withVisible -> eval -> eval
Execution halted

The interface has changed quite a bit since this thread first started. You should only need this:

library(plotly)
gg <- ggplot(cars) + geom_point(aes(speed, dist))
ggplotly(gg)

thanks Carson

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fabiangehring picture fabiangehring  Â·  4Comments

johncarew picture johncarew  Â·  6Comments

pssguy picture pssguy  Â·  7Comments

cpsievert picture cpsievert  Â·  5Comments

msummersgill picture msummersgill  Â·  3Comments