In the simplified functioning code at the bottom, in the colDefs = list()
section of datatable()
under renderDT()
, in server
section, I manually center-align the two right-most column outputs of the table using the instructions targets = 1:2, class = "dt-center"
.
I've been trying to make the number of columns that are formatted in this manner reactive, based on the actual number of columns detected in the output table -- because in the full code this is extracted from, the number of output table columns varies based on the actual composition of the data. In the below code commented out with #
you can see my latest attempt to reactively format the columns, and of course it doesn't work.
Please, how do I reactively format the data table columns where all columns to the right of the header rows are center-aligned?
Simplified functioning code:
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({
datatable(
data = summed_data(),
rownames = FALSE,
options =
list(
columnDefs = list(
list(targets = 0, class = "dt-left"),
list(targets = 1:2, class = "dt-center")
# list(targets = 1:ncol(summed_data()), class = "dt-center")
)
),
)
})
}
shinyApp(ui, server)
The following seems to work. However, I'm not sure why your initial approach doesn't work - it looks good to me.
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({
print(names(summed_data())[1])
datatable(
data = summed_data(),
rownames = FALSE,
options = list(columnDefs = list(
list(className = 'dt-left', targets = 0),
list(className = 'dt-center', targets = seq_len(ncol(summed_data())) - 1)
))
)
})
}
shinyApp(ui, server)