How to adjust error using observe function in a shiny app

I would like to make an adjustment to the code below. Note that I have two input options: Excel and Database. If you choose, input$button ="Excel", do data <- reactive(Test), if it is input$button=="database", a connection is made to the database. But, please test for the Excel option, and see that it doesn't work right. It only works, if I put data <- reactive(Test), outside of observe. What is wrong?

library(shiny)
library(dplyr)
library(shinythemes)
 
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("PAGE1",
                             sidebarLayout(
                               sidebarPanel(
                                 radioButtons("button", 
                                              label = h3("Data source"),
                                              choices = list("Excel" = "Excel",
                                                             "Database" = "database"), 
                                              selected = "File"),
                                 br(),br(),
                                 uiOutput('daterange')
                                 
                               ),
                               mainPanel(
                                 dataTableOutput('table')
                               )))))



server <- function(input, output) {
  
 #data <- reactive(Test)

  observe({
    if(is.null(input$button)) {
      
    }else if (input$button =="Excel"){
      
     data <- reactive(Test)
  
    } else if(input$button=="database"){
      
    
      con <- DBI::dbConnect(odbc::odbc(),
                            Driver   = "[your driver's name]",
                            Server   = "[your server's path]",
                            Database = "[your database's name]",
                            UID      = rstudioapi::askForPassword("Database user"),
                            PWD      = rstudioapi::askForPassword("Database password"),
                            Port     = 1433)
      
      data <-tbl(con, in_schema("dbo", "date1")) %>%
        collect()

    } 
  })
 
  output$daterange <- renderUI({
    req(data())
    dateRangeInput("daterange1", "Period you want to see:",
                   start = min(data()$date2),
                   end   = max(data()$date2))
  })
  
  data_subset <- reactive({
    req(input$daterange1)
    days <- seq(input$daterange1[1], input$daterange1[2], by = 'day')
    subset(data(), date2 %in% days)
  })
  
  output$table <- renderDataTable({
    data_subset()
  })
  
}

shinyApp(ui = ui, server = server)

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.