I would like some help regarding the formatRound
of my output$table
in my code. The code works fine, but I'd like to make an adjustment that goes like this: as you can see I specify formatRound
, as being formatRound(c(3:4))
, but instead of inserting 4
, I'd like to do something like this: c(3:ncol(All))
, however when I do it appears object All
not found and it also doesn't work if I insert c(3:last_col())
, do you know how I can adjust this, that is, I wouldn't like to specify the last column in formatRound
?
Thanks!
library(shiny)
library(shinythemes)
library(dplyr)
Test <- structure(list(date2 = structure(c(18808, 18808, 18809, 18810
), class = "Date"), Category = c("FDE", "ABC", "FDE", "ABC"),
coef = c(4, 1, 6, 1)), row.names = c(NA, 4L), class = "data.frame")
ui <- fluidPage(
shiny::navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
br(),
tabPanel("",
sidebarLayout(
sidebarPanel(
uiOutput('daterange'),
br()
),
mainPanel(
dataTableOutput('table')
)
))
))
server <- function(input, output,session) {
data <- reactive(Test)
data_subset <- reactive({
req(input$daterange1)
req(input$daterange1[1] <= input$daterange1[2])
days <- seq(input$daterange1[1], input$daterange1[2], by = 'day')
All<- subset(data(), date2 %in% days)
All<-All%>% mutate(date2 = format(ymd(date2), "%d/%m/%Y"))%>%
mutate(TOTAL = rowSums(across(3:last_col()), na.rm = TRUE)) %>%
mutate(across(everything(), ~ replace_na(as.character(.), '-')))
})
output$daterange <- renderUI({
dateRangeInput("daterange1", "Period you want to see:",
start = min(data()$date2),
end = max(data()$date2))
})
output$table <- renderDataTable({
data_subset()
datatable(data_subset(),options = list( columnDefs = list(
list(className = 'dt-center', targets = "_all")),
paging = TRUE, searching = FALSE,pageLength = 10,dom = 'tip', scrollx=TRUE), rownames = FALSE) %>%
formatRound(c(3:4), digits=0)
})
}
shinyApp(ui = ui, server = server)
CodePudding user response:
It is a reactive object. All
is an object created within the reactive
element and it is not acessible. Instead, get the output with data_subset()
and wrap with ncol
formatRound(3:ncol(data_subset()), digits=0)
-server code
server <- function(input, output,session) {
data <- reactive(Test)
data_subset <- reactive({
req(input$daterange1)
req(input$daterange1[1] <= input$daterange1[2])
days <- seq(input$daterange1[1], input$daterange1[2], by = 'day')
All<- subset(data(), date2 %in% days)
All<-All%>% mutate(date2 = format(ymd(date2), "%d/%m/%Y"))%>%
mutate(TOTAL = rowSums(across(3:last_col()), na.rm = TRUE)) %>%
mutate(across(everything(), ~ replace_na(as.character(.), '-')))
})
output$daterange <- renderUI({
dateRangeInput("daterange1", "Period you want to see:",
start = min(data()$date2),
end = max(data()$date2))
})
output$table <- renderDataTable({
data_subset()
datatable(data_subset(),options = list( columnDefs = list(
list(className = 'dt-center', targets = "_all")),
paging = TRUE, searching = FALSE,pageLength = 10,dom = 'tip', scrollx=TRUE), rownames = FALSE) %>%
formatRound(3:ncol(data_subset()), digits=0)
})
}
shinyApp(ui = ui, server = server)
Or instead of doing data_subset()
multiple times, assign it to an object and use that object
server <- function(input, output,session) {
data <- reactive(Test)
data_subset <- reactive({
req(input$daterange1)
req(input$daterange1[1] <= input$daterange1[2])
days <- seq(input$daterange1[1], input$daterange1[2], by = 'day')
All<- subset(data(), date2 %in% days)
All<-All%>% mutate(date2 = format(ymd(date2), "%d/%m/%Y"))%>%
mutate(TOTAL = rowSums(across(3:last_col()), na.rm = TRUE)) %>%
mutate(across(everything(), ~ replace_na(as.character(.), '-')))
})
output$daterange <- renderUI({
dateRangeInput("daterange1", "Period you want to see:",
start = min(data()$date2),
end = max(data()$date2))
})
output$table <- renderDataTable({
All <- data_subset()
datatable(All,options = list( columnDefs = list(
list(className = 'dt-center', targets = "_all")),
paging = TRUE, searching = FALSE,pageLength = 10,dom = 'tip', scrollx=TRUE), rownames = FALSE) %>%
formatRound(3:ncol(All), digits=0)
})
}
shinyApp(ui = ui, server = server)