【问题标题】:Error in $: object of type 'closure' is not subsettable shiny R$中的错误:“闭包”类型的对象不是可子集的闪亮R
【发布时间】:2023-03-27 01:20:01
【问题描述】:

我的 Shiny 应用程序有问题。 在我的应用程序中,我有很多 DT,Box,有时是 DT in Box,所以我决定创建函数来让我的代码更干净。

  1. 我创建 DT 的函数获取我想要可视化的数据
  2. 我创建盒子的函数获取盒子的标题,如果是的话应该是 折叠,和 UI - 应该包含什么框(例如几个 像
  3. 这样的元素
fluidRow(
   column(6, uiOutput("aaa")),
   column(6, uiOutput("bbb"))
)
  1. 我还创建了基于前面描述的函数在 Box 中创建 DT 的函数。

据我了解,问题出在数据传输方式上,但我无法解决。

我准备了我想要实现但不起作用的功能示例。

library(shiny)
library(shinydashboard)
library(DT)

Create_DT       <- function(dataSource){
  datatable(
    dataSource,
    rownames = FALSE,
    selection = 'none',
    class = 'cell-border stripe',
    extensions = 'Buttons',
    options = list(
      buttons = list('copy', 'print', list(extend = 'collection',buttons = c('csv', 'excel', 'pdf'),text = 'Download')),
      dom = 'Bfrtip',
      info = FALSE,
      lengthChange = FALSE,
      paging = FALSE,
      searching = FALSE,
      scrollX = TRUE,
      columnDefs = list(list(className = 'dt-center', targets = "_all"))
    )
  ) %>% formatStyle(colnames(dataSource),"white-space"="nowrap")
}
Create_Box      <- function(description, collapsed, ui){
  box(
    width = 12,
    title = strong(description),
    color = "primary",
    collapsible = TRUE, 
    collapsed = collapsed,
    ui
  )
}
Create_DTinBox  <- function(description, collapsed, ui){
  Create_Box(description, collapsed, ui)
}

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    uiOutput("result")
  )
)

server <- function(input, output){
  reactiveValues(iris = iris)
  output$result <- renderUI({
    Create_DTinBox(
      description = "test", 
      collapsed = TRUE, 
      ui = column(6, offset = 3, Create_DT(reactiveValues$iris))
    )
  })
}

shinyApp(ui, server)

知道这个应用程序在保持示例中的函数结构的同时应该如何正常工作吗?

【问题讨论】:

    标签: r shiny reactive


    【解决方案1】:

    你需要renderdatatable。此外,您的reactiveValues 需要正确定义。试试这个

    library(shiny)
    library(shinydashboard)
    library(DT)
    
    Create_DT       <- function(dataSource){
      datatable(
        dataSource,
        rownames = FALSE,
        selection = 'none',
        class = 'cell-border stripe',
        extensions = 'Buttons',
        options = list(
          buttons = list('copy', 'print', list(extend = 'collection',buttons = c('csv', 'excel', 'pdf'),text = 'Download')),
          dom = 'Bfrtip',
          info = FALSE,
          lengthChange = FALSE,
          paging = FALSE,
          searching = FALSE,
          scrollX = TRUE,
          columnDefs = list(list(className = 'dt-center', targets = "_all"))
        )
      ) %>% formatStyle(colnames(dataSource),"white-space"="nowrap")
    }
    Create_Box      <- function(description, collapsed, ui){
      box(
        width = 12,
        title = strong(description),
        color = "primary",
        collapsible = TRUE,
        collapsed = collapsed,
        ui
      )
    }
    Create_DTinBox  <- function(description, collapsed, ui){
      Create_Box(description, collapsed, ui)
    }
    
    ui <- dashboardPage(
      dashboardHeader(),
      dashboardSidebar(),
      dashboardBody(
        uiOutput("result")
      )
    )
    
    server <- function(input, output){
      rv <- reactiveValues(df = iris)
      output$result <- renderUI({
        Create_DTinBox(
          description = "test",
          collapsed = TRUE,
          ui = column(8, offset = 3, renderDT(Create_DT(rv$df)))
        )
      })
    }
    
    shinyApp(ui, server)
    

    【讨论】:

    • 不幸的是,它没有帮助,仍然是同样的错误
    • 请复制整个代码并尝试一下。请注意,reactiveValues$iris 未在您的代码中分配。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-30
    • 2015-06-18
    • 2019-03-23
    • 1970-01-01
    • 1970-01-01
    • 2018-06-23
    • 2016-03-30
    相关资源
    最近更新 更多