r - Shiny Plotly event_data 仅在 Shiny 服务器上出现错误

标签 r shiny plotly

我正在使用shiny、plotly和shinyBS,如下所示,当绘图上发生plotly_click事件时,生成带有新绘图的模式弹出窗口。当我在本地运行以及在本地浏览器中运行时,它可以完美地找到。

但是,当我将其部署到 Shiny 服务器上时,我收到此错误,并且不知道这意味着什么。有什么想法吗?

library(shiny)
library(plotly)
library(shinyBS)

df1 <- data.frame(x = 1:10, y = 1:10)
df2 <- data.frame(x = c(rep('a', 10), rep('b', 10)),
                  y = c(rnorm(10), rnorm(10, 3, 1)))

ui <- fluidPage(
  column(6, plotlyOutput('scatter')),
  bsModal('boxPopUp', '', '', plotlyOutput('box'))
)

server <- function(input, output, session) {
  output$scatter <- renderPlotly({
    plot_ly(df1, x = ~x, y = ~y, mode = 'markers',
            type = 'scatter', source = 'scatter')
  })
  observeEvent(event_data("plotly_click", source = "scatter"), {
    toggleModal(session, "boxPopUp", toggle = "toggle")
  })
  output$box <- renderPlotly({
    eventdata <- event_data('plotly_click', source = 'scatter')
    validate(need(!is.null(eventdata),
                  'Hover over the scatter plot to populate this boxplot'))
    plot_ly(df2, x = ~x, y = ~y, type = 'box')
  })
}

shinyApp(ui = ui, server = server)

错误消息如下(显示在应用程序的 Shiny 服务器日志中):

Warning: Error in event_data: attempt to apply non-function
Stack trace (innermost first):
    59: event_data
    58: observeEventExpr
     1: runApp

最佳答案

这是使用 Shiny 0.14 中提供的模式对话框的修改版本。 在 RStudio、本地浏览器中测试,shinyapps以及我本地安装的 Shiny 服务器开源版本。

这是代码:

    library(shiny)
    library(plotly)
    library(shinyBS)

    df1 <- data.frame(x = 1:10, y = 1:10)
    df2 <- data.frame(x = c(rep('a', 10), rep('b', 10)),
                      y = c(rnorm(10), rnorm(10, 3, 1)))

    ui <- fluidPage(
            column(6, plotlyOutput('scatter'))
    )

    server <- function(input, output, session) {
            output$scatter <- renderPlotly({
                    plot_ly(df1, x = x, y = y, mode = 'markers',
                            type = 'scatter', source = 'scatter')
            })

            observeEvent(event_data("plotly_click", source = "scatter"), {
                    showModal(modalDialog(
                            renderPlotly({
                                    plot_ly(df2, x = x, y = y, type = 'box')
                            }),
                            easyClose = TRUE
                    ))
            })

    }

    shinyApp(ui = ui, server = server)

关于r - Shiny Plotly event_data 仅在 Shiny 服务器上出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39737171/

相关文章:

R 表达式无明显原因导致 NaN

html - rvest - 在 1 个标签中抓取 2 个类

javascript - R DT :datatable remove . 无页脚边框底部

python - 如何将多迹线图制作为可重用代码?

r - 如何在 R 中使用 plotly 绘制 3d 曲面图?

r - 从 R 中的时间数据中剥离秒数

r - 在没有 TRUE 评估的情况下在 R Shiny 中使用 source 命令

r - 如何将 rmarkdown 报告与 shinyapps 链接以在 R 中导出 webapp 内容?

R:使用数据表重新排序因子级别(与 Plotly 一起使用)

r - 在 R 中,如何创建频率堆积条形图,但条形上有百分比标签,y 轴上有频率?