更新时间:2023-11-30 20:09:46
我认为您希望使用reactiveValues()
存储您的数据框。以下是可能的解决方案:
library(shiny)
runApp(list(
ui=pageWithSidebar(headerPanel("Adding entries to table"),
sidebarPanel(textInput("text1", "Column 1"),
textInput("text2", "Column 2"),
actionButton("update", "Update Table")),
mainPanel(tableOutput("table1"))),
server=function(input, output, session) {
values <- reactiveValues()
values$df <- data.frame(Column1 = NA, Column2 = NA)
newEntry <- observe({
if(input$update > 0) {
newLine <- isolate(c(input$text1, input$text2))
isolate(values$df <- rbind(values$df, newLine))
}
})
output$table1 <- renderTable({values$df})
}))
若要避免创建空行,请创建一个空数据帧,而不是使用NA
:
values$df <- data.frame(Column1 = numeric(0), Column2 = numeric(0))
与rbind()
相比,索引似乎更适合添加行(这会弄乱列名.不确定原因):
isolate(values$df[nrow(values$df) + 1,] <- c(input$text1, input$text2))