Home > Software engineering >  Error in $: object of type 'closure' is not subsettable shiny R
Error in $: object of type 'closure' is not subsettable shiny R

Time:11-21

I have problem with my Shiny App. In my app I have many DT, Boxes, sometimes DT in Box so I decided to create functions to do my code more clean.

  1. My function to create DT get data which I want to visualize
  2. My function to create Box get title of box, information if is should be collapsed, and UI - what box should contain (for example few elements like
fluidRow(
   column(6, uiOutput("aaa")),
   column(6, uiOutput("bbb"))
)
  1. I also created function to create DT in Box which is based on the previously described functions.

As I understand, the problem is the way data is transferred, but I cannot solve it.

I prepared example of functionality I would like to achieve but doesn't work.

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)

Any Idea how this app should look like to work fine while maintaining the structure of the function from the example?

CodePudding user response:

You need to render the datatable. Also, your reactiveValues need to be defined properly. Try this

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)

output

  • Related