【问题标题】:How to format data table inputs using the DT package in R shiny?如何使用 R Shiny 中的 DT 包格式化数据表输入?
【发布时间】:2022-01-22 00:51:26
【问题描述】:

我很难理解关于这个问题的那些深入研究 CSS/java 脚本的帖子,我对此知之甚少。

在运行下面的代码时,我试图让下载按钮、要查看的表格行数和过滤器整齐地呈现。有人知道怎么做吗?

我故意使用fluidRow(column(width...)) 将它们挤在下面,以更好地说明问题,因为在更完整的应用程序中,这源自表格在狭窄的主面板中呈现。请查看底部的图片以了解一些清理此问题的想法:通过缩小下载按钮的大小、减少显示输入行数中的文本等。我​​愿意接受任何其他格式建议!虽然我不想减少项目(下载按钮、长度、过滤器)。

library(dplyr)
library(DT)
library(shiny)
library(shinyWidgets)
library(tidyverse)

ui <-
  fluidPage(
    fluidRow(
      column(width = 8,
          h3("Data table:"),
          tableOutput("data"),
          h3("Sum the data table columns:"),
          radioButtons(
            inputId = "grouping",
            label = NULL,
            choiceNames = c("By period 1", "By period 2"),
            choiceValues = c("Period_1", "Period_2"),
            selected = "Period_1",
            inline = TRUE
          ),
          DT::dataTableOutput("sums")
      )
    )
  )

server <- function(input, output, session) {
  data <- reactive({
    data.frame(
      Period_1 = c("2020-01", "2020-02", "2020-03", "2020-01", "2020-02", "2020-03"),
      Period_2 = c(1, 2, 3, 3, 1, 2),
      ColA = c(1000.01, 20, 30, 40, 50, 60),
      ColB = c(15.06, 25, 35, 45, 55, 65)
    )
  })
  
  summed_data <- reactive({
    data() %>%
      group_by(!!sym(input$grouping)) %>%
      select("ColA","ColB") %>%
      summarise(across(everything(), sum))
  })
  
  output$data <- renderTable(data())
  
  output$sums <- renderDT({ # this section changed
    summed_data() %>% 
      datatable(rownames = FALSE) %>% 
      formatCurrency(c("ColA", "ColB"), currency = '\U20AC', digits = 2)
  })

  output$sums <- renderDT({
    summed_data() %>% 
      datatable(rownames = FALSE,
                extensions = 'Buttons',
                options = list(
                  buttons = list(
                    list(extend = 'copy', filename = "flowsBalances"),
                    list(extend = 'csv', filename = "flowsBalances"),
                    list(extend = 'excel', filename = "flowsBalances")
                  ),
                  dom = 'Blfrtip'
                ),
                class = "display"
      ) %>% 
      formatCurrency(c("ColA", "ColB"), currency = '', digits = 2)
  })
  
}

shinyApp(ui, server)

【问题讨论】:

    标签: r shiny dt


    【解决方案1】:

    请不要在一个问题中提出多个问题。这是一个答案,除了对齐:

    library(shiny)
    library(DT)
    
    dat <- iris[1:20, 1:3]
    
    # change the width of the search box, and make the buttons smaller:
    css <- '
    .dataTables_filter input[type=search] {
      width: 50px;
    }
    button.dt-button {
      padding: 1px !important
    }
    '
    
    ui <- fluidPage(
      tags$head(
        tags$style(
          HTML(css)
        )
      ),
      br(),
      DTOutput("dtable")
    )
    
    server <- function(input, output){
    
      output[["dtable"]] <- renderDT({
        datatable(
          dat,
          extensions = "Buttons",
          options =
            list(
              dom = "Blfrtip",
              language =
                list(
                  lengthMenu = "Show _MENU_" # remove "Entries"
                ),
              buttons = list("csv", "excel")
            )
        )
      })
    
    }
    
    shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-13
      • 1970-01-01
      • 2019-02-05
      • 2016-12-17
      • 2019-04-21
      • 2018-04-13
      • 2021-04-17
      • 2015-08-20
      相关资源
      最近更新 更多