Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Text fields inside renderUI() don't update #308

Closed
dmitrie opened this issue Nov 25, 2013 · 4 comments
Closed

Text fields inside renderUI() don't update #308

dmitrie opened this issue Nov 25, 2013 · 4 comments
Milestone

Comments

@dmitrie
Copy link

@dmitrie dmitrie commented Nov 25, 2013

Every time I use actionButtons to fill textboxes/textareas (via renderUI()) I have an issue that those text fields don't always update. I'll bring couple of examples:

  1. I use a drop-down list and "Add" actionButton to fill a textbox. Let's say I have "abc" selected in a drop-down list. I press "Add", so textbox gets a value "abc", then modify the textbox contents (e.g. make it "abcd"), press "Add" again, but the content is still "abcd".
  2. I have a textarea with "Save" and "Load" buttons, which save and load data to/from a text file. Let's say I load some data into the textbox, then modify it and press "Load" again to get the initial data, as my modifications were incorrect. But I can't do that, as textarea doesn't get updated.

Currently I use the following fix. Inside renderUI() I save the previous value of the textbox to some environment that I keep specifically for such cases, so every time I generate new contents for the textbox I check if the value is the same as the previous value. If it is the same, I either add a space at the end and remove it if there is already a space at the end.

It would be useful if you could fix this problem in some next release. Absolutely all my GUIs face this issue and have this ugly fix.

@wch
Copy link
Collaborator

@wch wch commented Nov 25, 2013

Can you provide a (minimal) example app? You can put it in a gist, like this: https://gist.github.com/wch/6196669

@dmitrie
Copy link
Author

@dmitrie dmitrie commented Nov 25, 2013

server.R

require(shiny)

shinyServer(function(input, output) {
  output$test <- renderUI({
      VALUE <- ''
      if(input$add>0) {
        isolate({
          VALUE <- input$options
        })
      }
      textInput("textbox","Text",VALUE)
  })

  output$caption <- renderText({
    input$textbox
  })
})

ui.R

require(shiny)
require(shinyIncubator)

shinyUI(pageWithSidebar(
  headerPanel("Test"),

  sidebarPanel(
    selectInput("options", "options", choices=c('abc','def')),
    uiOutput("test"),
    actionButton("add","Add")
  ),

  mainPanel(
    textOutput("caption")
  )
))

@wch
Copy link
Collaborator

@wch wch commented Nov 25, 2013

The reason it behaves that way is because textInput() is sending this HTML code to the browser each time you press "Add":

<input id="textbox" type="text" value="abc" class="shiny-bound-input">

The value field of the element doesn't change when the user types in the box. When the HTML code is re-sent to the browser, Shiny doesn't bother updating the object since the HTML code is exactly the same.

You can use the updateTextInput function instead:

# server.r
shinyServer(function(input, output, session) {
  observe({
    VALUE <- ''
    if(input$add>0) {
      isolate({
        VALUE <- input$options
      })
    }
    updateTextInput(session, inputId = "textbox", value = VALUE)
  })

  output$caption <- renderText({
    input$textbox
  })
})
# ui.r
shinyUI(pageWithSidebar(
  headerPanel("Test"),

  sidebarPanel(
    selectInput("options", "options", choices=c('abc','def')),
    textInput("textbox", "Text", ""),
    actionButton("add","Add")
  ),

  mainPanel(
    textOutput("caption")
  )
))

@dmitrie
Copy link
Author

@dmitrie dmitrie commented Nov 25, 2013

Thank you, works like a charm for both textInput and tags$textarea!

@yihui yihui closed this as completed Nov 25, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants