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

Time:12-31

In the shiny app below Im trying to use shinyJS() to hide and display text but I get:

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.

Do not bother that dataset does not exist its just an example

## 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)

CodePudding user response:

You can't use show() in the ui, these functions are used in the server. Remove that and it works. Sample:

## 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)

Output:

enter image description here

  • Related