-
Notifications
You must be signed in to change notification settings - Fork 205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Authoring widgets with an alternate representation #166
Comments
@cpsievert I need to look deeper into what you are doing, but based on a quick read, it seems like you are doing what @hafen was doing as well with a custom I think, there might be a cleaner way to do this, without hacking at the internals of |
I don't think We don't have a version of plotly that uses htmlwidgets without this hack. We recently added an 'offline' feature for paying customers, but it doesn't use htmlwidgets at all since the plotly.js source wasn't bundled with the package (it will be now). If you're curious, you can grab the plotlyjs source and poke around with what's on master right now: dir.create("~/.plotly/plotlyjs", recursive = TRUE)
download.file("https://raw.githubusercontent.com/ropensci/plotly/htmlwidgets/inst/htmlwidgets/lib/plotlyjs/plotly.min.js", "~/.plotly/plotlyjs/plotly.min.js")
install_github("ropensci/plotly") |
cc @hadley to comment on "mix visualization and data manipulation verbs" which ggvis does as well (in a different way). |
I haven't tried this but could you do: renderPlotly <- function(expr, env = parent.frame(), quoted = FALSE) {
if (!quoted) { expr <- substitute(expr) } # force quoted
expr <- call("toWidget", expr)
shinyRenderWidget(expr, plotlyOutput, env, quoted = TRUE)
} |
ooh, that'd be awesome @jcheng5, but I think it requires |
Is there any reason you don't take the same approach as ggvis, and just override the appropriate dplyr methods? |
I suppose one reason is that it works with any function that inputs/outputs a data frame |
And preserves all the attributes of the data frame. |
renderPlotly <- function(expr, env = parent.frame(), quoted = FALSE) {
if (!quoted) { expr <- substitute(expr) } # force quoted
expr <- as.call(list(call(":::", quote("plotly"), quote("toWidget")), expr))
shinyRenderWidget(expr, plotlyOutput, env, quoted = TRUE)
} |
@jcheng5 thanks, that works great! @hadley Just to clarify, are you saying preserving attributes is a reason to not use the ggvis approach? FYI, the plotly approach should work even when a manipulation removes attributes. A |
Here is an example ( library(plotly)
library(broom)
library(dplyr)
economics %>%
mutate(rate = unemploy / pop) %>%
plot_ly(x = date, y = rate, name = "raw") %>%
loess(rate ~ as.numeric(date), data = .) %>%
augment() %>%
add_trace(y = .fitted, name = "smooth") |
@cpsievert to me, that sounds like a system that will work fine 95% of the time, but when it doesn't you'll have a heck of a time figuring out what went wrong |
plotly is about to convert to htmlwidgets, but I had to produce some hacks, since I'd like to keep the 'central object' in plotly a data frame (with some metadata attached). I like this design since it allows us to mix visualization and data manipulation verbs, for example:
In order to get this working in the R console and knitr/rmarkdown, I decided to convert a plotly object to an htmlwidget object in the print.plotly() method. For shiny, since it assumes that the expression generates an htmlwidget object, I had to modify the body of
htmlwidgets::shinyRenderWidget()
slightly (I guess we could also assume users know toprint()
their plotly objects inrenderPlotly()
, but I'd rather not impose that on them).If nothing else, I hope this helps out other htmlwidgets authors in a similar pickle. More optimistically, maybe this will spark some discussion around a more standard way to do a work-around like this.
The text was updated successfully, but these errors were encountered: