R shiny: How to create a dynamic interface (text input)

I am trying to build a dynamic textInput with R brilliant. The user must write a text field, and then click the add button to fill in another field. However, every time I click the button, all fields become empty. I like it if I have to determine how many fields I want in advance. Any help would be greatly appreciated.

thank

Here is my code:

library(shiny) shiny::runApp(list( ui = pageWithSidebar( headerPanel("test"), sidebarPanel( helpText("Alternatives list"), verbatimTextOutput("summary") ), mainPanel( actionButton("obs","add"), htmlOutput("selectInputs") ) ) , server = function(input,output){ observe({ if (input$obs == 0) return() isolate({ output$selectInputs <- renderUI({ if(!is.null(input$obs)) print("w") w <- "" for(i in 1:input$obs) { w <- paste(w,textInput(paste("a",i,sep=""),paste("a",i,sep=""))) } HTML(w) }) output$summary <- renderPrint({ print("your Alternative are:") for(i in 1:input$obs) { print( input[[sprintf("a%d",i)]] ) } }) outputOptions(output, 'selectInputs', suspendWhenHidden=FALSE) }) }) })) 
+5
r shiny textinput
Mar 04 '14 at 0:19
source share
1 answer

Your problem is that every time you press a button, you restore all the buttons (so the input is $ aX). And they are generated without a default value, so when you read them, they are all zeros.

For example, you can see your error if you suppress the loop in the output$selectInputs : (But now it allows you to set each aX only 1 time.)

  output$selectInputs <- renderUI({ if(!is.null(input$obs)) print("w") w <- "" w <- paste(w, textInput(paste("a", input$obs, sep = ""), paste("a", input$obs, sep = ""))) HTML(w) }) 

Saving your code (and then saving the regeneration of all buttons), you must add to your argument the values โ€‹โ€‹of each textInput your "own value" (in fact, the value of the old input with the same identifier) value = input[[sprintf("a%d",i)]]

  output$selectInputs <- renderUI({ w <- "" for(i in 1:input$obs) { w <- paste(w, textInput(paste("a", i, sep = ""), paste("a", i, sep = ""), value = input[[sprintf("a%d",i)]])) } HTML(w) }) 
+5
Mar 04 '14 at 10:29
source share



All Articles