Shiny 中的 conditionalPanel 不起作用

Pet*_*erV 6 r shiny

我正在尝试使用 conditionalPanel 在加载文件时显示消息。但是,一旦条件为真,面板就不会消失。我在下面创建了一个可重现的代码:

服务器

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

})
Run Code Online (Sandbox Code Playgroud)

用户界面

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..")
    )
  )
)
Run Code Online (Sandbox Code Playgroud)

Mat*_*rde 6

提供给的条件conditionalPanel在 javascript 环境中执行,而不是在 R 环境中执行,因此无法在 R 环境中引用或检查变量或函数。针对您的情况的解决方案是使用uiOutput,如下例所示。

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)
Run Code Online (Sandbox Code Playgroud)