Question

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

})
Was it helpful?

Solution

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'

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top