Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R:如何构建动态UI(文本输入)_R_Textinput_Shiny - Fatal编程技术网

R:如何构建动态UI(文本输入)

R:如何构建动态UI(文本输入),r,textinput,shiny,R,Textinput,Shiny,我正在尝试用R构建动态文本输入。用户应该在一个文本字段中写入,然后按add按钮填充另一个字段。然而,每次我按下按钮,所有字段都变为空,就好像我必须事先定义需要多少字段一样。任何帮助都将不胜感激 多谢各位 这是我的密码: library(shiny) shiny::runApp(list( ui = pageWithSidebar( headerPanel("test"), sidebarPanel( helpText("Alternatives list"), verbatimText

我正在尝试用R构建动态文本输入。用户应该在一个文本字段中写入,然后按add按钮填充另一个字段。然而,每次我按下按钮,所有字段都变为空,就好像我必须事先定义需要多少字段一样。任何帮助都将不胜感激

多谢各位

这是我的密码:

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)
  })
})
}))
库(闪亮)
闪亮::runApp(列表(
ui=页面带边框(
headerPanel(“测试”),
侧栏面板(
帮助文本(“备选方案列表”),
逐字输出(“摘要”)
),    
主面板(
操作按钮(“obs”、“添加”),
htmlOutput(“选择输入”)
)  
)
, 
服务器=功能(输入、输出){
观察({
如果(输入$obs==0)
返回()
隔离({

输出$selectInputs您的问题是每次单击按钮时都会重新生成所有按钮(因此输入$aX)。默认情况下,这些按钮都是无值生成的,这就是为什么在读取它们时,它们都是空的

例如,如果在
输出$selectInputs
代码中取消循环,您可以看到错误:(但现在它只允许您将每个aX设置一次。)

    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)
    })
    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)
    })