在R Shiny中点击按钮后,将输入域重置为null [英] Resetting Input field to null after clicking button in R Shiny

查看:1093
本文介绍了在R Shiny中点击按钮后,将输入域重置为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个应用程序,用户可以逐列输入数据值。单击ADD按钮后,输入的值将按列添加到现有按钮。例如
如果输入col1,2,3,并且我们在显示中点击了ADD $ /

  col1 
2
3

如果输入col2,4,7,并点击ADD显示

  col1 col2 
2 4
3 7

等。



我希望这样,当点击添加按钮时,输入字段被清除以允许新列的输入。请查看以下ui和服务器的代码。输出表也没有正确显示,任何帮助来解决这个问题也不会感激。

  ui.R 

shinyUI(pageWithSidebar(
headerPanel(我的数据表),
sidebarPanel(h5(Enter input),
textInput(colname,输入列名称,NA),
numericInput(x,X,NA),
numericInput(y,Y,NA),
br(),
actionButton(Add,ADD)),
mainPanel(verbatimTextOutput(out))
))

  server.R 

ShinyServer (输入,输出){

myTable < - reactive({
if(input $ Add> 0)
return(isolation({
colnm& - 输入$ colname
x< - 输入$ x
y< - 输入$ y
结果< - data.frame(rbind(colnm,x,y))
结果
}))
})

输出$ out< - renderTable({
myTable()
})

})


解决方案

表需要使用 renderTable 而不是 verbatimTextOutput 。我想你想保留旧的输入。执行此操作的一种方法是使用 reactiveValues 。编辑:我没看到你想重置输入。要重置输入,请使用 updateNumericInput updateTextInput 函数。您还需要在服务器函数中传递会话变量。

  runApp(
list(ui = pageWithSidebar(
headerPanel(我的数据表),
sidebarPanel(h5(输入输入),
textInput(colname,输入列名称,NA),
numericInput(x,X,NA),
numericInput(y
br(),
actionButton(Add,ADD)),
mainPanel(tableOutput(out))
),

server = function(input,output,session){
myValues< - reactiveValues()

观察({
if(input $ Add> ; 0){
isolation({
colnm < - input $ colname
x < - input $ x
y& - - $ $ $ $ $ $ $ $ .nu​​ll(myValues $ myDf)){
myValues $ myDf< - cbind(myValues $ myDf,
data.frame(setNames(list(c(x,y)),coln m))

} else {
myValues $ myDf< - data.frame(setNames(list(c(x,y)),colnm))
}

})
updateNumericInput(session,x,X,NA)
updateNumericInput(session,y,Y,NA)
updateTextInput (会话,colname,输入列名称,NA)
}
})

输出$ out< - renderTable({
myValues $ myDf
})

})

编辑:



您可以更改为

  updateNumericInput(session, x,X,3)
updateNumericInput(会话,y,Y,5)
updateTextInput(会话,colname,输入列名称,默认名称

它的工作原理。现在值将更改为默认值3,5和默认名称


I am building an application in which users can enter data values for table by column. Once ADD button is clicked the entered values would be appended by column to the existing one. e.g. if col1, 2, 3 are entered and ADD is clicked we have in the display

col1
   2
   3

and if col2, 4, 7 are entered and ADD clicked we have have the display

col1 col2
   2    4
   3    7

etc.

I would like it such that when the add button is clicked, the input fields are cleared to allow for the entry of new column. Please find below codes for the ui and server. The output table also does not display properly, any assistance to solve this problem too would be appreciated.

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

And

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

})

解决方案

The table needs to be rendered using renderTable rather then verbatimTextOutput. I guess you want to keep old inputs. One way to do this would be to use reactiveValues. EDIT: I didnt see you wanted to reset inputs. To reset inputs use the updateNumericInput and updateTextInput function. You will also need to pass a session variable inot your server function.

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

  })
)

EDIT:

You could change to

    updateNumericInput(session, "x","X", 3)
    updateNumericInput(session, "y","Y", 5)
    updateTextInput(session, "colname","Enter Column Name",'Default NAME')

and it works. Now the values change to default values of 3,5 and 'Default NAME'

这篇关于在R Shiny中点击按钮后,将输入域重置为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆