6
votes

I am trying to use conditionalPanel to display message when a file is being loaded. However, the panel is not disappearing once the condition is TRUE. I have created a reproducible code below:

server.R

library(shiny)

print("Loading start")
print(paste("1->",exists('FGram')))
FGram <- readRDS("data/UGram.rds")
print(paste("2->",exists('FGram')))
print("Loading end")

shinyServer( function(input, output, session) {

})

ui.R

library(shiny)

shinyUI( fluidPage(
  sidebarLayout(
    sidebarPanel(
      h4("Side Panel")
      )
    ),

    mainPanel(
      h4("Main Panel"),
      br(),
      textOutput("First Line of text.."),
      br(),
      conditionalPanel(condition = "exists('FGram')", HTML("PLEASE WAIT!!     <br>App is loading, may take a while....")),
      br(),
      h4("Last Line of text..")
    )
  )
)
1
conditions are supposed to be in javascript, not R. If you open the developer panel in Chrome (Ctrl+Shift+J), I bet you'll see an error in the console. You won't be able to get a conditional panel to check the R environment for the existence of a variable.Matthew Plourde
I use conditionalPanel() all the time in shiny dashboards with menu items where selecting a different menu removes the conditionalPanel. Not clear what would 'trigger' it to be removed in this specific case. You may need some server side processing as the UI won't be able to react on own.Gopala
Thanks @Matthew. I saw this error in console : "Uncaught ReferenceError: exists is not defined" Any workaround to this?PeterV
@user3949008, But does that not defeat the purpose of conditionalPanel() if I have to handle it separately on server side. Any pointers is appreciated as I am new to this.PeterV
@PeterV, no. You can't call R code or inspect the R environment from the javascript environment, which is where the condition statement is execute. What you can do is use custom message handlers in Shiny to create a new environment variable.Matthew Plourde

1 Answers

6
votes

Conditions supplied to conditionalPanel are executed in the javascript environment, not the R environment, and therefore cannot reference or inspect variables or functions in the R environment. A solution to your situation would be to use uiOutput, as in the example below.

myGlobalVar <- 1

server <- function(input, output) {

    output$condPanel <- renderUI({
        if (exists('myGlobalVar')) 
            HTML("PLEASE WAIT!!     <br>App is loading, may take a while....")
    })

}

ui <- fluidPage({
    uiOutput('condPanel')
})


shinyApp(ui=ui, server=server)