【问题标题】:Add DT extensions using Shiny and selectinput使用 Shiny 和 selectinput 添加 DT 扩展
【发布时间】:2023-04-02 05:17:01
【问题描述】:

尝试将以下信息添加到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)

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

【问题讨论】:

    标签: r datatable shiny dt


    【解决方案1】:

    在您的代码中,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 相同)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-24
      • 2019-08-04
      • 2018-11-24
      • 1970-01-01
      • 2018-10-07
      • 2020-03-08
      • 2018-05-13
      • 1970-01-01
      相关资源
      最近更新 更多