【问题标题】:R Shiny: Prevent cssloader from showing until action button is clickedR Shiny:在单击操作按钮之前阻止显示 cssloader
【发布时间】:2018-11-14 04:58:55
【问题描述】:

我想在某些处理完成时显示一个 cssloader 微调器。 cssloader 必须在点击 actionButton 后才会显示,这会触发数据处理(此步骤需要一些时间,由下面示例中的 sys.Sleep() 函数模拟)。另外,我希望它在每次由 actionButton 触发此操作时显示,而不仅仅是第一次。

这是我正在尝试的示例:

library(shiny)
library(shinycssloaders)

ui <- fluidPage(
   titlePanel("CSS loader test"),

   sidebarLayout(
      sidebarPanel(
         selectInput("imageOptions", "Choose an image:", choices = list(option1="RStudio-Logo-Blue-Gradient.png", option2="RStudio-Logo-All-Gray.png")),
         actionButton("getImage", "Show image:")
      ),

      mainPanel(
         withSpinner(uiOutput("logo"))
      )
   )
)

server <- function(input, output) {
  url<-reactive(
   paste0("https://www.rstudio.com/wp-content/uploads/2014/07/", input$imageOptions)
   )

  observeEvent(input$getImage,{
    output$logo<-renderText({
      URL<-isolate(url())
      print(URL)
      Sys.sleep(2)
      c('<center><img src="', URL, '"width="50%" height="50%" align="middle"></center>')
    })
  })

}
# Run the application 
shinyApp(ui = ui, server = server)

【问题讨论】:

  • 我认为微调器可能会出现,因为uiOutput() 是一个反应函数,我在您的代码中看到的所有其他内容似乎都是正确的。
  • 然后再次使用plotOutput() 进行测试得出相同的效果。我一定是错过了什么。

标签: r shiny shinyjs


【解决方案1】:

我敢打赌有更好的方法来完成我所需要的,但这里有一个解决方案:

library(shiny)
library(shinycssloaders)

ui <- fluidPage(
  titlePanel("CSS loader test"),

  sidebarLayout(
    sidebarPanel(
      selectInput("imageOptions", "Choose an image:", choices = list(option1="RStudio-Logo-Blue-Gradient.png", option2="RStudio-Logo-All-Gray.png")),
      actionButton("getImage", "Show image:")
    ),

    mainPanel(
      withSpinner(uiOutput("logo"))
    )
  )
)

server <- function(input, output) {
  url<-reactive(
    paste0("https://www.rstudio.com/wp-content/uploads/2014/07/", input$imageOptions)
  )
  output$logo<-renderText({
    validate(need(input$getImage, "")) #I'm sending an empty string as message.
    input$getImage
      URL<-isolate(url())
      print(URL)
      Sys.sleep(2)
      c('<center><img src="', URL, '"width="50%" height="50%" align="middle"></center>')
  })
}
# Run the application 
shinyApp(ui = ui, server = server)

工作解决方案不是通过调用observeEvent 来获得反应性,而是在render 函数中使用对validate(need(input$getImage, "")) 的调用。

【讨论】:

  • 似乎是一个需要修复的功能,因为大型程序通常依赖于观察事件。作者自己说这是如何在他的GitHub上修复它的包,所以我怀疑它会改变。
  • 你是对的。这不是编写反应式输入/输出的常用方法,但至少它很容易让它工作。干杯!
猜你喜欢
  • 2023-02-15
  • 2020-01-03
  • 1970-01-01
  • 2021-02-10
  • 2021-07-28
  • 1970-01-01
  • 1970-01-01
  • 2022-01-15
  • 2018-10-09
相关资源
最近更新 更多