使用 plotly 和 Shiny 服务器复制图例条目

Duplicate legend entries using plotly and Shiny server

我想在 R 中绘制数据,这样用户就可以 select 绘制哪些实例(哪些因子水平)。我想使用 plotly and then host it using Shiny Server 来完成。

问题是,在我更新 selection 后,我让每个图例条目重复了两次(即第一次正确呈现,但在我单击另一个变量进行绘图后,问题出现了)。

插图如下:

奇怪的是,当 运行 在本地(例如,在 R Studio 中或使用 runApp())或使用 shinyapps.io 时,这个问题不会出现,但只有在使用 Shiny Server.R

有没有办法解决这个问题并避免重复输入?

可重现的例子:

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)

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 

这个问题是由于普通用户和 shiny 用户使用不同版本的 plotly 包引起的。闪亮的服务器(至少默认情况下)运行s 在 "shiny" 用户下,但是当你在本地 运行 应用程序时,它是你当前用户下的 运行。因此,运行应用程序的这两种方式使用不同的包版本。

此外,CRAN 上可用的稳定 plotly 版本 (plotly_4.5.6) 中不存在此问题。