Skip to content

[Documentation] Incomplete shiny plotly integration example #583

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

Closed
MySchizoBuddy opened this issue May 14, 2016 · 3 comments
Closed

[Documentation] Incomplete shiny plotly integration example #583

MySchizoBuddy opened this issue May 14, 2016 · 3 comments

Comments

@MySchizoBuddy
Copy link

MySchizoBuddy commented May 14, 2016

The shiny-plotly example about diamonds facet plots has an input slider named height of plot which doesn't do anything. The server.R code doesn't use it anywhere. Either remove that slider or show us how to change the plot height using the slider

Also pageWithSidebar() is deprecated you have to use fluidPage instead

@cpsievert
Copy link
Collaborator

Thanks. Moved to plotly/documentation#385

@timelyportfolio
Copy link
Collaborator

timelyportfolio commented May 15, 2016

@cpsievert, while this is a documentation problem, I think we might also have either a bug or confusing situation. For instance, if we simplify this example as below.

library(shiny)
library(plotly)
data(diamonds, package = "ggplot2")

nms <- names(diamonds)

ui <- shinyUI(pageWithSidebar(

  headerPanel("Diamonds Explorer"),

  sidebarPanel(

    sliderInput('sampleSize', 'Sample Size', min = 1, max = nrow(diamonds),
                value = 1000, step = 500, round = 0),

    selectInput('x', 'X', choices = nms, selected = "carat"),
    selectInput('y', 'Y', choices = nms, selected = "price"),
    selectInput('color', 'Color', choices = nms, selected = "clarity"),
    sliderInput('plotHeight', 'Height of plot (in pixels)', 
                min = 100, max = 2000, value = 1000)
  ),

  mainPanel(
    plotlyOutput('trendPlot', height = "800px")
  )
))

server <- function(input, output, session) {

  #add reactive data information. Dataset = built in diamonds data
  dataset <- reactive({
    diamonds[sample(nrow(diamonds), input$sampleSize),]
  })

  output$trendPlot <- renderPlotly({

    # build graph with ggplot syntax
    p <- ggplot(dataset(), aes_string(x = input$x, y = input$y, color = input$color)) + 
      geom_point()

    # just verify that this is reactive as expected to the height slider
    height = input$plotHeight
    print(height)
    ggp<-as.widget(ggplotly(p,height=height)%>%layout(autosize=TRUE))
    ggp$x$layout$height=height
    ggp
  })

}

shinyApp(ui,server)

In the code, to make the height reactive I had to do two unexpected hacks.

  1. do %>% layout(autosize=TRUE)) which does not make a whole lot of sense given that we are providing a new height. However, this has the adverse impact that when the window is resized, then the height reverts back to original and not the value specified by the height slider. I don't have any suggestions on how to handle, since I think the likelihood of autosize in a "expected" situation is far higher than this contrived example using shiny inputs to control size.
  2. set ggp$x$layout$height=height since setting the height as documented in ?ggplotly has no impact in Shiny contexts. However, height in ggplotly seems to work as expected in non-Shiny contexts. When not in Shiny, ggplotly(p, height=100) works just fine.

I thought I could just do the following to make this work.

ggplotly(p, height = input$plotHeight)

I'm still getting a feel for all the magic that is happening, so I have probably gotten a lot wrong here, and feel free to correct all my mistakes :) This all stemmed from a naive belief that I could quickly fix the documentation.

@cpsievert
Copy link
Collaborator

cpsievert commented May 15, 2016

Thank you for bringing this up @timelyportfolio.

I can fix (2) pretty easily, but (1) has me stumped at the moment. Could you file a new issue with the content you have here? I'll fix it once I know what's going on here -> plotly/plotly.js#537

cpsievert referenced this issue in plotly/documentation May 15, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants