3

I want to plot data in R, such that user can select which instances (which factor levels) to plot. I would like to do it using plotly and then host it using Shiny Server.

The problem is that I am getting every legend entry repeated twice after I update my selection (i.e. the first time it renders correctly, but after I click another variable to plot, the problem appears).

Here is the illustration: enter image description here

The weird part is that this problem is not present when running locally (e.g., inside R Studio or using runApp()) or using shinyapps.io, but only when using Shiny Server.R

Is there a way to solve this and avoid duplicate entries?

Reproducible example:

library(plotly)

ui <- fluidPage(
  fluidRow(
    column(2, 
           checkboxGroupInput("species", 
                              label = "Species", 
                              choices = unique(iris$Species))),
    column(10, 
           plotlyOutput('irisPlotly'))
  )
)

server <- function(input, output) {
  # Filter only selected species
  selectedData <- reactive({
    iris[iris$Species %in% input$species, ]
  })

  # Render plotly plot
  output$irisPlotly <- renderPlotly({
    plot_ly(selectedData(), 
            x = ~Sepal.Length, 
            y =~Sepal.Width,
            color = ~Species) %>%
      layout(showlegend = TRUE)
  })
}

shinyApp(ui = ui, server = server)

Output of sessionInfo():

R version 3.3.2 (2016-10-31) 
Platform: x86_64-pc-linux-gnu (64-bit) 
Running under: Arch Linux 

locale: 
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C 
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C 
[9] LC_ADDRESS=C LC_TELEPHONE=C 
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C 

attached base packages: 
[1] stats graphics grDevices utils datasets methods base 

other attached packages: 
[1] plotly_4.5.6.9000 ggplot2_2.2.1.9000 shiny_1.0.0 

loaded via a namespace (and not attached): 
[1] Rcpp_0.12.9 magrittr_1.5 munsell_0.4.3 viridisLite_0.1.3 
[5] colorspace_1.3-2 xtable_1.8-2 R6_2.2.0 httr_1.2.1 
[9] plyr_1.8.4 dplyr_0.5.0 tools_3.3.2 grid_3.3.2 
[13] gtable_0.2.0 DBI_0.5-1 crosstalk_1.0.0 htmltools_0.3.5 
[17] yaml_2.1.14 lazyeval_0.2.0 digest_0.6.12 assertthat_0.1 
[21] tibble_1.2 tidyr_0.6.1 purrr_0.2.2 base64enc_0.1-3 
[25] htmlwidgets_0.8 mime_0.5 scales_0.4.1 jsonlite_1.3 
[29] httpuv_1.3.3 
Nick To
  • 848
  • 8
  • 9
  • 1
    I've tested your code locally on my mac laptop and I can't reproduce the legend problem. Tested with Chrome, Firefox, and Safari. My sessionInfo: R version 3.3.3 (2017-03-06), Platform: x86_64-apple-darwin13.4.0 (64-bit), Running under: OS X El Capitan 10.11.6, plotly_4.5.6, ggplot2_2.2.1, shiny_1.0.0. – bdemarest Mar 13 '17 at 23:46
  • @bdemarest Added clarification that the problem is not present when running locally (using `runApp()`), but only when using Shiny Server. – Nick To Mar 14 '17 at 07:18
  • Should probably include the `sessionInfo()` data as well. – Mike Wise Mar 14 '17 at 11:02

1 Answers1

0

The problem was caused by different versions of plotly package for the usual and shiny user. Shiny Server (by default at least) runs under the "shiny" user, but when you run the app locally it is run under your current user. Thus, these two ways of running the app were using different package versions.

Moreover, the issue is not present in the stable plotly version (plotly_4.5.6) available on CRAN.

Nick To
  • 848
  • 8
  • 9