I'm inserting two codes: the first code works very well, but my database is mentioned in the code. However, I have this database in Excel and I'm trying to use it only by fileInput
(second code). Unfortunately it's not working, the table that was supposed to be shown isn't working. Could you help me solve this problem?? Thank you very much!
To make it easier, I'm providing the excel database I'm using. ex.xlsx - Google Sheets
First code:
library(shiny)
library(shinythemes)
library(dplyr)
library(tidyverse)
library(lubridate)
library(readxl)
library(DT)
function.test<-function(){
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")
return(Test)
}
ui <- fluidPage(
shiny::navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
br(),
tabPanel("",
sidebarLayout(
sidebarPanel(
uiOutput('fileInput'),
uiOutput('daterange')
),
mainPanel(
dataTableOutput('table')
)
))
))
server <- function(input, output,session) {
data <- reactive(function.test())
output$daterange <- renderUI({
dateRangeInput("daterange1", "Period you want to see:",
min = min(data()$date2),
max = max(data()$date2))
})
data_subset <- reactive({
req(input$daterange1)
req(input$daterange1[1] <= input$daterange1[2])
days <- seq(input$daterange1[1], input$daterange1[2], by = 'day')
showModal(modalDialog("Wait", footer=NULL))
on.exit(removeModal())
All<- subset(data(), date2 %in% days)
All<-All%>% mutate(TOTAL = rowSums(across(3:last_col()), na.rm = TRUE))
})
output$table <- renderDataTable({
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)
Second code:
library(shiny)
library(shinythemes)
library(dplyr)
library(tidyverse)
library(lubridate)
library(readxl)
library(DT)
ui <- fluidPage(
shiny::navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
br(),
tabPanel("",
sidebarLayout(
sidebarPanel(
uiOutput('fileInput'),
uiOutput('daterange')
),
mainPanel(
dataTableOutput('table')
)
))
))
server <- function(input, output,session) {
data <- eventReactive(input$file, {
if (is.null(input$file)) {
return(NULL)
}
else {
df <- read_excel(input$file$datapath)
return(df)
}
})
output$fileInput <- renderUI({
fileInput("file",h4("Import file"),
multiple = T,accept = ".xlsx",
placeholder = "No file selected")
})
output$daterange <- renderUI({
dateRangeInput("daterange1", "Period you want to see:",
min = min(data()$date2),
max = max(data()$date2))
})
data_subset <- reactive({
req(input$daterange1)
req(input$daterange1[1] <= input$daterange1[2])
days <- seq(input$daterange1[1], input$daterange1[2], by = 'day')
showModal(modalDialog("Wait", footer=NULL))
on.exit(removeModal())
All<- subset(data(), date2 %in% days)
All<-All%>% mutate(TOTAL = rowSums(across(3:last_col()), na.rm = TRUE))
})
output$table <- renderDataTable({
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)