[英]object of type ‘closure’ is not subsettable in R Shiny
I would like to add buttons to my DT (one for each row - in first column). 我想将按钮添加到DT(每行一个-第一列)。 When I click on the button it should remove the row from DT.
当我单击按钮时,应从DT中删除该行。 I wrote a code:
我写了一个代码:
library(shiny)
library(DT)
shinyApp(
ui <- fluidPage(DT::dataTableOutput("data")),
server <- function(input, output) {
values <- reactiveValues(data = NULL)
values$data <- as.data.frame(
cbind(c("a", "d", "b", "c", "e", "f"),
c(1463, 159, 54, 52, 52, 220),
c(0.7315, 0.0795, 0.027, 0.026, 0.026, 0.11)
)
)
shinyInput <- function(FUN, len, id, ...) {
inputs <- character(len)
for (i in seq_len(len)) {
inputs[i] <- as.character(FUN(paste0(id, i), ...))
}
inputs
}
df <- reactive({
data = data.frame(
Delete = shinyInput(actionButton, nrow(values$data), 'button_', label = "Remove", onclick = 'Shiny.onInputChange(\"select_button\", this.id)'),
as.data.frame(values$data),
stringsAsFactors = FALSE,
row.names = 1:nrow(values$data)
)
})
output$data <- DT::renderDataTable(
df$data, server = FALSE, escape = FALSE, selection = 'none'
)
observeEvent(input$select_button, {
selectedRow <- as.numeric(strsplit(input$select_button, "_")[[1]][2])
df$data <- df$data[rownames(df$data) != selectedRow, ]
})
}
)
But unfortunately I get error: object of type 'closure' is not subsettable . 但是不幸的是我得到了错误: 类型为'closure'的对象不能被子集化 。 values$data has to stay as reactiveValues because it is only fragment of my project and I use it in others functions.
values $ data必须保留为reactValues,因为它只是我的项目的一部分,我在其他函数中使用它。 So how can I write df to make my program working?
那么,如何编写df以使程序正常工作?
The reason for the error is that you call the df
as df$data
, which would be correct if it was a reactiveValues
. 究其原因,错误是你所说的
df
的df$data
,这将是正确的,如果它是一个reactiveValues
。 However, df
is a reactive which returns just one object, so you should just call it with df()
. 但是,
df
是反应式的,它仅返回一个对象,因此您应该使用df()
调用。
For your issue, you could make a reactiveVal
that holds the rows that should be removed. 对于您的问题,您可以制作一个
reactiveVal
来保存应删除的行。 Please note that you should tweak this a little yourself, for example add something like observeEvent(values$data, {rows_to_remove(NULL)})
so the rows_to_remove()
object is reset to NULL when the input data changes. 请注意,您应该自己进行一些调整,例如,添加诸如
observeEvent(values$data, {rows_to_remove(NULL)})
之类的rows_to_remove()
以便在输入数据更改时将rows_to_remove()
对象重置为NULL。 Another approach would be to make the entire dataframe a reactiveVal
, and use observers to update it. 另一种方法是使整个数据帧一
reactiveVal
,并用观察来更新它。
Working example below, hope this helps! 下面的工作示例,希望对您有所帮助!
library(shiny)
library(DT)
shinyApp(
ui <- fluidPage(DT::dataTableOutput("data")),
server <- function(input, output) {
values <- reactiveValues(data = NULL)
values$data <- as.data.frame(
cbind(c("a", "d", "b", "c", "e", "f"),
c(1463, 159, 54, 52, 52, 220),
c(0.7315, 0.0795, 0.027, 0.026, 0.026, 0.11)
)
)
shinyInput <- function(FUN, len, id, ...) {
inputs <- character(len)
for (i in seq_len(len)) {
inputs[i] <- as.character(FUN(paste0(id, i), ...))
}
inputs
}
rows_to_remove <- reactiveVal()
df <- reactive({
data = data.frame(
Delete = shinyInput(actionButton, nrow(values$data), 'button_', label = "Remove", onclick = 'Shiny.onInputChange(\"select_button\", this.id)'),
as.data.frame(values$data),
stringsAsFactors = FALSE,
row.names = 1:nrow(values$data)
)
data[!rownames(data) %in% rows_to_remove(), ]
})
output$data <- DT::renderDataTable(
df(), server = FALSE, escape = FALSE, selection = 'none'
)
observeEvent(input$select_button, {
selectedRow <- as.numeric(strsplit(input$select_button, "_")[[1]][2])
rows_to_remove(c(rows_to_remove(),selectedRow)) # update the rows to remove
})
}
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.