如何使用 R shiny 中的 DT 包格式化数据 table 输入?
How to format data table inputs using the DT package in R shiny?
我很难理解关于这个问题的帖子,这些帖子深入研究了 CSS/java 脚本,我对此知之甚少。
在下面的 运行 代码中,我试图获取下载按钮、要查看的 table 行数和过滤器,以便整齐地呈现。有人知道怎么做吗?
我故意使用 fluidRow(column(width...))
将它们挤在下面以更好地说明问题,因为在更完整的应用程序中,这源自 table 在狭窄的主面板中呈现。请查看底部的图像以了解一些清理此问题的想法:缩小下载按钮的大小,减少显示行数输入中的文本等。我愿意接受任何其他格式建议!虽然我不想减少项目(下载按钮、长度、过滤器)。
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)
请不要在一个问题中提出多个问题。这是一个答案,除了对齐:
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)
我很难理解关于这个问题的帖子,这些帖子深入研究了 CSS/java 脚本,我对此知之甚少。
在下面的 运行 代码中,我试图获取下载按钮、要查看的 table 行数和过滤器,以便整齐地呈现。有人知道怎么做吗?
我故意使用 fluidRow(column(width...))
将它们挤在下面以更好地说明问题,因为在更完整的应用程序中,这源自 table 在狭窄的主面板中呈现。请查看底部的图像以了解一些清理此问题的想法:缩小下载按钮的大小,减少显示行数输入中的文本等。我愿意接受任何其他格式建议!虽然我不想减少项目(下载按钮、长度、过滤器)。
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)
请不要在一个问题中提出多个问题。这是一个答案,除了对齐:
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)