单击R Shiny中的按钮后,将输入字段重置为空

 奥咨达医疗器_械丶服务集团 发布于 2023-01-17 11:36

我正在构建一个应用程序,用户可以在其中逐列输入数据值.单击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.编辑:我没有看到你想重置输入.要重置输入,请使用updateNumericInputupdateTextInput功能.您还需要sessionserver函数中传递变量.

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和"默认名称"

1 个回答
  • 该表需要使用renderTable而不是使用verbatimTextOutput.我想你想保留旧的输入.一种方法是使用reactiveValues.编辑:我没有看到你想重置输入.要重置输入,请使用updateNumericInputupdateTextInput功能.您还需要sessionserver函数中传递变量.

    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和"默认名称"

    2023-01-17 11:41 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有