我正在构建一个应用程序,用户可以在其中逐列输入数据值.单击ADD按钮后,输入的值将按列添加到现有值.例如,如果输入col1,2,3并且点击了ADD,我们就会在显示屏中显示
col1 2 3
如果输入col2,4,7并单击ADD,我们就有了显示
col1 col2 2 4 3 7
等等
我希望这样,当单击添加按钮时,将清除输入字段以允许输入新列.请查找以下ui和服务器代码.输出表也没有正确显示,也可以理解任何解决这个问题的帮助.
ui.R shinyUI(pageWithSidebar( headerPanel("My data table"), sidebarPanel(h5("Enter input"), textInput("colname","Enter Column Name",NA), numericInput("x","X",NA), numericInput("y","Y",NA), br(), actionButton("Add","ADD")), mainPanel(verbatimTextOutput("out")) ))
和
server.R shinyServer(function(input,output){ myTable <- reactive({ if(input$Add > 0) return(isolate({ colnm <- input$colname x <- input$x y <- input$y result <- data.frame(rbind(colnm,x,y)) result })) }) output$out <- renderTable({ myTable() }) })
jdharrison.. 6
该表需要使用renderTable
而不是使用verbatimTextOutput
.我想你想保留旧的输入.一种方法是使用reactiveValues
.编辑:我没有看到你想重置输入.要重置输入,请使用updateNumericInput
和updateTextInput
功能.您还需要session
在server
函数中传递变量.
runApp( list(ui = pageWithSidebar( headerPanel("My data table"), sidebarPanel(h5("Enter input"), textInput("colname","Enter Column Name",NA), numericInput("x","X",NA), numericInput("y","Y",NA), br(), actionButton("Add","ADD")), mainPanel(tableOutput("out")) ), server = function(input,output,session){ myValues <- reactiveValues() observe({ if(input$Add > 0){ isolate({ colnm <- input$colname x <- input$x y <- input$y if(!is.null(myValues$myDf)){ myValues$myDf <- cbind(myValues$myDf, data.frame(setNames(list(c(x, y)), colnm)) ) }else{ myValues$myDf <- data.frame(setNames(list(c(x, y)), colnm)) } }) updateNumericInput(session, "x","X", NA) updateNumericInput(session, "y","Y", NA) updateTextInput(session, "colname","Enter Column Name",NA) } }) output$out <- renderTable({ myValues$myDf }) }) )
编辑:
你可以改为
updateNumericInput(session, "x","X", 3) updateNumericInput(session, "y","Y", 5) updateTextInput(session, "colname","Enter Column Name",'Default NAME')
它的工作原理.现在,值将更改为默认值3,5和"默认名称"
该表需要使用renderTable
而不是使用verbatimTextOutput
.我想你想保留旧的输入.一种方法是使用reactiveValues
.编辑:我没有看到你想重置输入.要重置输入,请使用updateNumericInput
和updateTextInput
功能.您还需要session
在server
函数中传递变量.
runApp( list(ui = pageWithSidebar( headerPanel("My data table"), sidebarPanel(h5("Enter input"), textInput("colname","Enter Column Name",NA), numericInput("x","X",NA), numericInput("y","Y",NA), br(), actionButton("Add","ADD")), mainPanel(tableOutput("out")) ), server = function(input,output,session){ myValues <- reactiveValues() observe({ if(input$Add > 0){ isolate({ colnm <- input$colname x <- input$x y <- input$y if(!is.null(myValues$myDf)){ myValues$myDf <- cbind(myValues$myDf, data.frame(setNames(list(c(x, y)), colnm)) ) }else{ myValues$myDf <- data.frame(setNames(list(c(x, y)), colnm)) } }) updateNumericInput(session, "x","X", NA) updateNumericInput(session, "y","Y", NA) updateTextInput(session, "colname","Enter Column Name",NA) } }) output$out <- renderTable({ myValues$myDf }) }) )
编辑:
你可以改为
updateNumericInput(session, "x","X", 3) updateNumericInput(session, "y","Y", 5) updateTextInput(session, "colname","Enter Column Name",'Default NAME')
它的工作原理.现在,值将更改为默认值3,5和"默认名称"