错误 $: object of type 'closure' is not subsettable shiny R

Error in $: object of type 'closure' is not subsettable shiny R

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

  1. 我创建 DT 的函数获取我想要可视化的数据
  2. 我创建框的函数获取框的标题,信息如果是应该是 折叠,并且 UI - 应该包含什么框(例如 few 像
  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)

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

您需要 render datatable。此外,您的 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)