Problems with renderUI's reactivity

1,377 views
Skip to first unread message

Peter

unread,
Jun 18, 2013, 6:32:03 AM6/18/13
to shiny-...@googlegroups.com
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 <input id="txt" type="text" ...> 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

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

shinyUI(pageWithSidebar(
  headerPanel('Test'),
  
  sidebarPanel(
    uiOutput("textbox"),
    actionButton("change", "Change")
  ),
  
  mainPanel(
    
  )
))

Peter

unread,
Jun 18, 2013, 7:27:11 AM6/18/13
to shiny-...@googlegroups.com
Sorry, server.R must start with: 
require(shiny)
 
and ui.R must start with:
require(shiny)
require(shinyIncubator)

Winston Chang

unread,
Jun 18, 2013, 10:57:40 AM6/18/13
to shiny-...@googlegroups.com
When it executes val(), if the value changes, it will invalidate its descendants, namely output$textbox. However, if the returned value of val() doesn't change, then it won't invalidate them.

-Winston


--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Joe Cheng

unread,
Jun 18, 2013, 3:25:54 PM6/18/13
to shiny-...@googlegroups.com
Ignore what Winston said. :) val() will invalidate its callers regardless of whether the returned value changes or not.

The problem isn't in the reactivity; if you set "options(shiny.trace=TRUE)" you can see that when you click the button, the value is going to the server ("RECV"); and that the server replies back with the HTML for textbox ("SEND"). Yet nothing actually changes on the client.

The problem is that the client knows what the previous value of the output was, and since the new version is no different, it thinks it's safe to ignore it rather than having the new value rendered. I'm not sure whether I consider this a feature or a bug.

In your case I think you would want to use updateTextInput() in an observe() call, anyway...

Winston Chang

unread,
Jun 18, 2013, 4:02:41 PM6/18/13
to shiny-...@googlegroups.com
Yup, listen to Joe on this one. :)
Reply all
Reply to author
Forward
0 new messages