R 在列表中将值添加到反应表中
我希望我的闪亮应用程序的用户能够以迭代方式向表中添加元素,但我无法确定如何保存这些值 在本例中,我希望用户能够在文本框中添加值,这些值应该添加到主面板的表底部。此时,先前添加的值将丢失R 在列表中将值添加到反应表中,r,shiny,R,Shiny,我希望我的闪亮应用程序的用户能够以迭代方式向表中添加元素,但我无法确定如何保存这些值 在本例中,我希望用户能够在文本框中添加值,这些值应该添加到主面板的表底部。此时,先前添加的值将丢失 library(shiny) runApp(list( ui=pageWithSidebar(headerPanel("Adding entries to table"), sidebarPanel(textInput("text1", "Column 1"),
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) {
tableStart <- data.frame(Column1 = NA, Column2 = NA)
newEntry <- reactive({
input$update
newLine <- isolate(c(input$text1, input$text2))
})
output$table1 <- renderTable({rbind(tableStart, newEntry())})
}))
库(闪亮)
runApp(列表(
ui=pageWithSidebar(标题面板(“向表中添加条目”),
侧边栏面板(textInput(“text1”,“第1列”),
textInput(“text2”,“第2列”),
操作按钮(“更新”、“更新表”),
主面板(表输出(“表1”)),
服务器=功能(输入、输出、会话){
tableStart我想您应该使用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})
}))
对于添加行,索引似乎比rbind()
更好(这会弄乱列名…不知道为什么):
isolate(values$df[nrow(values$df)+1,]我想尝试其他方法作为添加。但它不起作用。dplyr::bind_rows()
很好地保留了列名,顺便说一句:isolate(值$df为什么不使用observeEvent
?它会自动隔离第二个参数中的代码?observeEvent({(输入$update){newLine
values$df <- data.frame(Column1 = numeric(0), Column2 = numeric(0))
isolate(values$df[nrow(values$df) + 1,] <- c(input$text1, input$text2))