【问题标题】:Duplicate legend entries using plotly and Shiny server使用 plotly 和 Shiny 服务器复制图例条目
【发布时间】:2017-08-04 03:19:00
【问题描述】:

我想在 R 中绘制数据,以便用户可以选择要绘制的实例(哪些因子水平)。我想使用plotly 来完成它,然后使用Shiny Server 托管它。

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

下面是插图:

奇怪的是,在本地运行(例如,在 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 

【问题讨论】:

  • 我已经在我的 mac 笔记本电脑上本地测试了你的代码,但我无法重现图例问题。使用 Chrome、Firefox 和 Safari 进行测试。我的 sessionInfo:R 版本 3.3.3(2017-03-06),平台:x86_64-apple-darwin13.4.0(64 位),运行于:OS X El Capitan 10.11.6,plotly_4.5.6,ggp​​lot2_2.2.1, Shiny_1.0.0.
  • @bdemarest 添加了关于在本地运行(使用runApp())时不存在该问题的说明,但仅在使用 Shiny Server 时才存在。
  • 应该也包括sessionInfo() 数据。

标签: r shiny plotly shiny-server


【解决方案1】:

问题是由普通用户和闪亮用户的不同版本的 plotly 包引起的。 Shiny Server(至少默认情况下)在“shiny”用户下运行,但是当您在本地运行应用程序时,它会在您当前用户下运行。因此,这两种运行应用的方式使用了不同的包版本。

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

【讨论】:

    猜你喜欢
    • 2019-12-06
    • 2019-07-08
    • 2021-05-18
    • 2021-01-18
    • 2019-11-04
    • 1970-01-01
    • 2017-06-07
    • 2019-04-12
    • 2021-11-15
    相关资源
    最近更新 更多