Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shiny evaluates twice

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.

like image 213
Stophface Avatar asked Oct 18 '22 21:10

Stophface


1 Answers

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)
like image 96
Tonio Liebrand Avatar answered Oct 21 '22 15:10

Tonio Liebrand