Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shiny Plotly event_data Error only with shiny server

Tags:

r

shiny

plotly

I am using shiny, plotly and shinyBS as follows to generate a modal pop up with a new plot when a plotly_click event happens on the plot. It works perfectly find when I run locally, and also in the local browser.

However, when I deploy it on the Shiny server, I get this error, and have no idea what it means. Any thoughts?

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)

Error message is as follows (shown in the Shiny server log for the app):

Warning: Error in event_data: attempt to apply non-function
Stack trace (innermost first):
    59: event_data
    58: observeEventExpr
     1: runApp
like image 762
Gopala Avatar asked Oct 30 '22 19:10

Gopala


1 Answers

This is a modified version using the modal dialog available in Shiny 0.14. Tested in RStudio, local browser, shinyapps and on my local instalation of shiny server open source version.

This is the code:

    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)
like image 62
Valter Beaković Avatar answered Nov 15 '22 05:11

Valter Beaković