5

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.

four-eyes
  • 10,740
  • 29
  • 111
  • 220
  • I suggest you look how your reactive expressions are bound to each other. If any prior dependencies change then it will re-execute causing it to update – Pork Chop Nov 19 '15 at 14:38
  • 2
    Wrap the code in isolate(), except for the variable you want to trigger the output with. – Tonio Liebrand Nov 19 '15 at 14:41

2 Answers2

3

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)
Tonio Liebrand
  • 17,189
  • 4
  • 39
  • 59
0

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.

Holger Brandl
  • 10,634
  • 3
  • 64
  • 63