将值添加到SHINY中的反应表 [英] Add values to a reactive table in shiny
本文介绍了将值添加到SHINY中的反应表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望我的闪亮应用程序的用户能够迭代地向表中添加元素,但是我不知道如何保存这些值。
在本例中,我希望用户能够在文本框中添加值,这些值应该添加到主面板中表格的底部。此时,以前添加的值将丢失。
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())})
}))
推荐答案
我认为您希望使用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))
这篇关于将值添加到SHINY中的反应表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文