Problems using only fileInput on Shiny

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)

Your non working code has two definitions of data which would seem to be one too many

Thanks @nirgrahamuk for reply. I adjusted the second code, it still doesn't work. And it's practically the same as the first one, the only difference is that one has a built-in database and the other doesn't. I don't understand why the table is not generated.

I don't think this is a shiny issue, I expect you can resolve this by working in a plain script until you come up with a preferred solution.
Basically your code requires a date2 field to be a base R Date class.
readxl will not give you this by default, it wants to give you a datetime, which you would need to convert

(rxl <- readxl::read_excel("ex.xlsx"))
lubridate::as_date(rxl$date2)

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.