尝试使用 shinyJS() 时找不到闪亮的 seesion 对象

Shiny seesion object is not found when trying to use shinyJS()

在下面的 shiny 应用程序中,我尝试使用 shinyJS() 来隐藏和显示文本,但我得到:

Error: shinyjs: could not find the Shiny session object. This usually happens when a shinyjs function is called from a context that wasn't set up by a Shiny session.

不要为数据集不存在而烦恼它只是一个例子

## app.R ##
library(shiny)
library(shinydashboard)
library(dplyr)
library(shinyjs)

ui <- dashboardPage(
  dashboardHeader(title = "Biodiversity"),
  dashboardSidebar(
    
    actionButton("action","Submit")
    
    
  ),
  dashboardBody(
    useShinyjs(),
    
    show(
      div(id='text_div',
          verbatimTextOutput("text")
      )
    ),
    uiOutput("help_text"),
    plotlyOutput("plot")
  )
)

server <- function(input, output) {
  output$help_text <- renderUI({
    HTML("<b>Click 'Show plot' to show the plot.</b>")
  })
  
  
  react<-eventReactive(input$action,{
    hide("help_text")
    
    omited <-subset(omited, omited$scientificName %in% isolate(input$sci)&omited$verbatimScientificName %in% isolate(input$ver))
  })
  
  
  
  
}

shinyApp(ui = ui, server = server)

你不能在show()中使用ui,这些函数是在服务器中使用的。删除它并且它有效。示例:

## app.R ##
library(shiny)
library(shinydashboard)
library(dplyr)
library(shinyjs)
library(plotly)

ui <- dashboardPage(
  dashboardHeader(title = "Biodiversity"),
  dashboardSidebar(
    
    actionButton("action","Submit")
    
    
  ),
  dashboardBody(
    useShinyjs(),
    
      div(id='text_div',
          verbatimTextOutput("text")
      )
    ,
    uiOutput("help_text"),
    plotOutput("plot")
  )
)

server <- function(input, output) {
  output$help_text <- renderUI({
    HTML("<b>Click 'Show plot' to show the plot.</b>")
  })
  
  
  observeEvent(input$action,{
    hide("help_text")
    
  
  output$plot <- renderPlot({
    plot(1)
  })
  
  
})}

shinyApp(ui = ui, server = server)

输出: