object of type ‘closure’ is not subsettable in R Shiny

一个人想着一个人 提交于 2020-01-25 06:44:33

问题


I would like to add buttons to my DT (one for each row - in first column). When I click on the button it should remove the row from 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. values$data has to stay as reactiveValues because it is only fragment of my project and I use it in others functions. So how can I write df to make my program working?


回答1:


The reason for the error is that you call the df as df$data, which would be correct if it was a reactiveValues. However, df is a reactive which returns just one object, so you should just call it with df().

For your issue, you could make a reactiveVal that holds the rows that should be removed. 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. Another approach would be to make the entire dataframe a reactiveVal, and use observers to update it.

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
    })

  }
)


来源:https://stackoverflow.com/questions/48239366/object-of-type-closure-is-not-subsettable-in-r-shiny

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!