Warm tip: This article is reproduced from stackoverflow.com, please click
r shiny

Button to close modal throws an error: `$ operator is invalid for atomic vectors`

发布于 2020-03-29 21:02:38

I would like to display a modal when the user launches the app. To do so, I used this answer. However, when clicking on the button supposed to close the modal, I have the error:

Warning: Error in : $ operator is invalid for atomic vectors

Apparently, this error is due to something in observeEvent but I can't figure out what the error is. How can I solve this?

Reproducible example:

library(shiny)
library(shinyBS)

popup <- bsModalNoClose("window", "Window",
                        title="Click 'Choice 1' to close the popup", size='small',
                        actionButton('choice1', 'Choice 1', class = 'btn action-button btn-success'),
                        actionButton('choice2', 'Choice 2', class = 'btn action-button btn-success'),
                        tags$head(tags$style("#window .modal-footer{display:none}
                                       .modal-header"),
                                  tags$script("$(document).ready(function(){
                                        $('#window').modal();
                                        });")
                        ))

ui <- shinyUI(fluidPage(
  popup
))

server <- shinyServer(function(input, output, session) {

  observeEvent(input$choice1, {
    toggleModal(session = "session",
                modalId = "window",
                toggle = "close")
  })

})
shinyApp(ui, server)
Questioner
bretauv
Viewed
26
Freakazoid 2020-01-31 19:05

The toggleModal function requires in the session argument the actual session function, and not the string "session". Which you may notice from here.

toggleModal(session=session,
            modalId="window",
            toggle="close")