当单击按钮时,我正在尝试根据小部件选择将行添加到数据框中。
我已经把observeEvent
里面的renderTable
,但应用程序返回一个错误:无法将类“c(“观察者”,“R6”)”强制转换为 data.frame。
这是将行添加到的测试数据框:
> DF <- data.frame(matrix(c("A","B"), ncol = 2), stringsAsFactors = FALSE)
> colnames(DF) <- c("col1", "col2")
> DF
col1 col2
1 A B
这是闪亮的应用程序代码:
library(shiny)
DF <- data.frame(matrix(c("A","B"), ncol = 2), stringsAsFactors = FALSE)
colnames(DF) <- c("col1", "col2")
ui <- fluidPage(
titlePanel("Save input"),
sidebarLayout(
sidebarPanel(
wellPanel(
h3("Widget 1"),
radioButtons("add", "Letter", c("A", "B", "C"))
),
wellPanel(
h3("Save button"),
actionButton("save", "Save")
)
),
mainPanel(
tableOutput("table")
)
)
)
server <- function(input, output) {
output$table <- renderTable({
DF
observeEvent(input$save, {
l <- nrow(DF)
DF[l+1,] <- list(input$add, input$add)
})
})
}
shinyApp(ui = ui, server = server)
你知道有什么问题吗?
更新:
使用@Rémi 回答错误消失,但应用程序不会向表中添加新行:app
如果你只想显示你的表格,你应该把 DF 放在observeEvent 之后,因为“observeEvent 返回一个观察者引用类对象”,所以当你有一个时,renderTable
你必须返回一个矩阵或一个 data.frame。
server <- function(input, output) {
output$table <- renderTable({
observeEvent(input$save, {
l <- nrow(DF)
DF[l+1,] <- list(input$add, input$add)
})
DF
})
}
编辑 1 :如果我很理解你的要求,你不需要保存数据框但你想添加行,它可能是一样的,你只需要使用rbind
以下帖子作为建议:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句