Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shiny - problems with renderUI()'s reactivity

Tags:

r

shiny

I experience difficulties with Shiny package in R.

I am trying to understand why "# Option 1" in server.R doesn't work, but commented "# Option 2" does work. With "# Option 1" being active try typing random string and pressing the button. First time it replaces the values in the textbox with "abc", but all the next times doesn't.

In my understanding val() is already dependent on input$change, so it must re-execute every time the button is pressed, furthermore "# Option 2" is wrapped into isolate(), so it doesn't add any reactivity.

It seems that actually the value partially changes to "abc" when using "# Option 1". Having Google Chrome's inspect element opened you can see that "# Option 2" changes the values of every time the button is pressed, "# Option 1" also changes the value to "abc", but the the screen is updated only when the button is pressed for the first time.

Here is the code:

server.R

require(shiny)

shinyServer(function(input, output) {

  val <- reactive({
    if(input$change>0) {
     # Option 1
      'abc'
#       # Option 2
#       isolate({
#         paste('abc',input$txt,"")
#       })
    } else {
      ''
    }
  })

  output$textbox <- renderUI({
    textInput("txt","Text",val())
  })

})

ui.R

require(shiny)
require(shinyIncubator)

shinyUI(pageWithSidebar(
  headerPanel('Test'),

  sidebarPanel(
    uiOutput("textbox"),
    actionButton("change", "Change")
  ),

  mainPanel(

  )
))
like image 407
user1603038 Avatar asked Jun 18 '13 11:06

user1603038


2 Answers

See my response here:

https://groups.google.com/d/msg/shiny-discuss/PLHauRlFw3k/AnoD7NusvDIJ

The gist is that the server keeps sending exactly the same value to the textbox output, so the client is "smart" enough to ignore it.

like image 172
Joe Cheng Avatar answered Sep 29 '22 23:09

Joe Cheng


This does look like there might be a bug hidden in there somewhere, but I can't track it down.

The good news is, I think you should be able to accomplish this effect with the updateTextInput function.

For a deeper look at the issue, I've created a bug for this, however. https://github.com/rstudio/shiny/issues/181 . I'll try to update this post if/when we figure out what's going on.

like image 33
Jeff Allen Avatar answered Sep 29 '22 21:09

Jeff Allen