使用Shiny和selectinput添加DT扩展

时间:2019-07-05 23:35:51

标签: r datatable shiny dt

尝试将以下信息添加到Shiny中的数据表中,并在使用selectinput时出错:

  

错误:尺寸错误

library(DT)
library(readr)
library(jsonlite)
library(data.table)

gumdad <- fromJSON("data/boxes.json")
# Define UI for app  ----
ui <- fluidPage(

  # App title ----
  titlePanel("Box Scores"),

#FILTERS
selectInput("season",
            "Season:",
          c("All",
         unique(as.character(gumdad$season)))),
    # Main panel for displaying outputs ----
    mainPanel(
      # Output: Table----
      DT::dataTableOutput('tableone')

    )
  )

# Define server logic  ----
server <- function(input, output) {

output$tableone = renderDataTable({
  data <- datatable(gumdad, extensions = 'Buttons', rownames = FALSE, escape = FALSE, selection = 'none',
        colnames = c('Season', 'Date', 'Opponent', 'Result', 'UNC', 'Opp', 'OT', 'Location', 'Type','Box Score'),
        options = list(buttons = c('copy', 'csv'), paging = FALSE, dom = 'Bfrtip')
    )
    if (input$season != "All") {
data <- data[data$season == input$season,]
}
  return(data)
 })


gumdad$box <- sapply(gumdad$box, function(x)
            toString(tags$a(href=paste0("https://boxscorexxx.com/", x), "Box Score")))
}
shinyApp(ui = ui, server = server)

如何在使用正确尺寸自定义selectInput的同时使用datatable

1 个答案:

答案 0 :(得分:0)

在您的代码中,data不是数据帧,这是数据表。无法像这样data[data$season == input$season,]对其进行子集化。子集gumdad代替:

output$tableone = renderDT({
  data <- gumdad
  if (input$season != "All") {
    data <- data[data$season == input$season,]
  }
  datatable(data, extensions = 'Buttons', rownames = FALSE, escape = FALSE, selection = 'none',
                    colnames = c('Season', 'Date', 'Opponent', 'Result', 'UNC', 'Opp', 'OT', 'Location', 'Type','Box Score'),
                    options = list(buttons = c('copy', 'csv'), paging = FALSE, dom = 'Bfrtip')
  )
})

还要注意,您应该使用renderDT而不是renderDatatable(或使用DT::renderDatatable,它与renderDT相同)。