簡體   English   中英

如何使用 R shiny 中的 DT package 格式化數據表輸入?

[英]How to format data table inputs using the DT package in R shiny?

我很難理解關於這個問題的帖子,這些帖子深入研究了 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)

在此處輸入圖像描述

請不要在一個問題中問多個問題。 這是一個答案,除了 alignment:

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM