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'