Shiny evaluates twice

て烟熏妆下的殇ゞ 提交于 2019-12-10 18:28:17

问题


I have a rather complex Shiny application and something weird happens: When I print out some of my intermediate steps the App makes, everything gets printed out twice. That means, everything gets evaluated etc. twice.

I know without seeing the progamme its rather hard to tell what causes the problem, but maybe someone can pin point me (based on experierence/knowledge) what might be the problem.


回答1:


Like I mentioned in the comment, isolate() should solve your problem. Beyond the documentation of Rstudio http://shiny.rstudio.com/articles/reactivity-overview.html I recommend the following blog article for interesting informations beyond the RStudio docu. https://shinydata.wordpress.com/2015/02/02/a-few-things-i-learned-about-shiny-and-reactive-programming/

In a nutshell, the easiest way to deal with triggering is to wrap your code in isolate() and then just write down the variables/inputs, that should trigger changes before the isolate.

output$text <- renderText({
   input$mytext # I trigger changes
   isolate({ # No more dependencies from here on
     # do stuff with input$mytext
     # .....
     finishedtext = input$mytext
     return(finishedtext) 
   })
})

Reproducible example:

library(shiny)

ui <- fluidPage(
  textInput(inputId = "mytext", label = "I trigger changes", value = "Init"),
  textInput(inputId = "mytext2", label = "I DONT trigger changes"),
  textOutput("text")
)

server <- function(input, output, session) {
  output$text <- renderText({
    input$mytext # I trigger changes
    isolate({ # No more dependencies from here on
      input$mytext2
      # do stuff with input$mytext
      # .....
      finishedtext = input$mytext
      return(finishedtext) 
    })
  })  
}

shinyApp(ui, server)



回答2:


I encountered the same problem when using brush events in plotOutput. The solution turned out to be resetOnNew = T when calling plotOutput to prevent changes in my plot causing the brush event to be evaluated again.



来源:https://stackoverflow.com/questions/33806811/shiny-evaluates-twice

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