Can I set html class dynamically? Or how does Shiny set 'html'.hasClass('Shiny-busy')?

泪湿孤枕 提交于 2019-12-10 11:05:24

问题


So this is more of a conceptual question in response to getting a 'busy' notification working on my shiny app using the:

conditionalPanel(
        condition="$('html').hasClass('shiny-busy')",
        img(src="images/busy.gif"))  

I've gotten a animation gif to show during an initial query to a database, but it becomes unpredictable after that. I added in a second conditionalPanel with to hide the output graph if a new database call is made:

conditionalPanel(
     condition="!($('html').hasClass('shiny-busy'))",
     plotOutput("Some Graph"))  

The setup works through the second data pull, but if a third data base query is made, 'Some Graph' no longer hides and 'busy.gif' is no longer shown. It does flash up as a new plot is loaded.

So my overarching question is:
Is there a way to explicitly set the html class in server?
OR
How/when does Shiny set the class value?


回答1:


I'm not going to comment on the rest of the question, but I will answer the question of "is there a way to explicitly set the html class in server?"

You can use the package shinyjs to add/remove/toggle the class of an HTML element in the server. Here's an example of adding/removing the "shiny-busy" class to/form the <html> tag

library(shiny)
library(shinyjs)

runApp(shinyApp(
  ui = fluidPage(
    useShinyjs(),
    actionButton("add", "add `shiny-busy` class to html tag"),
    actionButton("remove", "remove `shiny-busy` class from html tag")
  ),
  server = function(input, output, session) {
    observeEvent(input$add, {
      shinyjs::addClass(selector = "html", class = "shiny-busy")
    })
    observeEvent(input$remove, {
      shinyjs::removeClass(selector = "html", class = "shiny-busy")
    })    
  }
))


来源:https://stackoverflow.com/questions/31663659/can-i-set-html-class-dynamically-or-how-does-shiny-set-html-hasclassshiny-b

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!