1

My shiny app is working however until I upload my xlsx file I get this error shown on the dashboard Error: no applicable method for 'tbl_vars' applied to an object of class "NULL" error in Shiny. As soon I upload my file the error goes away. I can't seem to figure out why I am getting this error shown.

I have checked out these SO posts however I couldn't get much hint from them-

  1. How to merge dataframes ? Error in UseMethod("tbl_vars") : no applicable method for 'tbl_vars' applied to an object of class "list"
  2. Error in R: no applicable method for 'predict' applied to an object of class "NULL"

My dput of iris.xlsx looks like this-

structure(list(date = structure(c(15706, 15707, 15708, 15723, 
15740, 15741, 15742, 15771, 15791, 15792, 15855), class = "Date"), 
    Sepal.Length = c(5.1, 4.9, 4.7, 5.1, 4.9, 5, 5.5, 6.7, 6, 
    6.7, 5.9), Sepal.Width = c(3.5, NA, NA, NA, NA, NA, NA, 3.1, 
    3.4, 3.1, 3), Petal.Length = c(1.4, 1.4, 1.3, 1.4, 1.5, 1.2, 
    1.3, 4.4, 4.5, 4.7, 5.1), Petal.Width = c(0.2, 0.2, 0.2, 
    0.3, 0.2, 0.2, 0.2, 1.4, 1.6, 1.5, 1.8), Species = c("setosa", 
    "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", 
    "versicolor", "versicolor", "versicolor", "virginica")), row.names = c(NA, 
11L), class = "data.frame")

Here is my reprex-

library(shiny)
library(openxlsx)
library(shinyjs)
library(htmltools)
library(lubridate)
library(DT)
library(dplyr)


#--------------------
#global.R

local_iris <- data.frame(Date= lubridate::mdy(c("1/1/2013","1/2/2013","3/27/2013","3/28/2013",
                                                "1/18/2013","2/4/2013","2/5/2013","2/6/2013")),
                        SPECLENTH= c(5.1,4.9,4.7,4.6,5,5.4,4.6,5.1),
                        SPECWIDTH= c(3,7,6, 8,8,9,5,1))

#-------------

ui <- fluidPage(
  
  sidebarLayout(
    sidebarPanel(
      
      fileInput("xlsxfile", "Choose an xlsx file",
                accept = c(".xlsx")),
      
      tags$hr(),
      
      
      # Input: Select number of rows to display ----
      selectInput("disp", "Display",
                  choices = c(All = "all",
                              Head = "head"),
                  selected = "all"),
      
      # Select variables to display ----
      uiOutput("checkbox"),
      uiOutput("checkbox_2")
      
    ),
    mainPanel(
      DT::DTOutput("contents")
    )
  )
)


server <- function(input, output) {
  
  # File handler ----
  mydata <- reactive({
    
    inFile <- input$xlsxfile
    
    if (is.null(input$xlsxfile)){
      return(NULL)
    }
    
    req(input$xlsxfile,
        file.exists(input$xlsxfile$datapath))
    
    openxlsx::read.xlsx(xlsxFile = inFile$datapath,
                        sheet = 1 ,
                        detectDates = TRUE,
                        sep.names = "_")
    
  })
  
  # Dynamically generate UI input when data is uploaded, only sow numeric columns ----
  output$checkbox <- renderUI({
    selectInput(inputId = "select_var", 
                label = "Select variables", 
                choices = c("", names(mydata() %>%
                                        dplyr::select_if(is.numeric))),
                selected = NULL, 
                multiple = FALSE)
  })
  
  # Select columns to print ----
  df_sel <- reactive({
    req(input$select_var)
    df_sel <- mydata() %>% 
      dplyr::select(input$select_var, date)
  })
  

  # Same as above but for global.R variable  ----
  output$checkbox_2 <- renderUI({
    selectInput(inputId = "select_var_2", 
                label = "Select variables", 
                choices = c("", names(local_iris %>%
                                        dplyr::select_if(is.numeric))),
                selected = NULL, 
                multiple = FALSE)
  })
  
  
  df_sel_global <- reactive({
    
    req(input$select_var_2)
    
    df_sel_global <- local_iris %>% 
      dplyr::select(input$select_var_2, Date)
  })
  
  
  # Join the dataframes together based on a key  ----
  joined_dfs <- reactive({
    
    df_joi <- dplyr::inner_join(df_sel(), df_sel_global(), by= c("date" = "Date")) %>%
      dplyr::select(input$select_var,input$select_var_2)
    
  })
  
  
  # Render data frame ----
  
  output$contents <- DT::renderDT(server = FALSE, {
    
    DT::datatable(
      if(input$disp == "head") {
        head(joined_dfs())
      }
      else {
        joined_dfs()
      }, filter = 'top', 
      extensions = c('Buttons'),
      options = list(scrollY = 600,
                     scrollX = TRUE,
                     pageLength = 20,
                     dom =  '<"float-left"l><"float-right"f>rt<"row"<"col-sm-4"B><"col-sm-4"i><"col-sm-4"p>>',
                     lengthMenu=  list(c(20, 40, 60, -1), 
                                       c('20', '40', '60','All')),
                     scrollCollapse= TRUE,
                     lengthChange = TRUE, 
                     widthChange= TRUE,
                     rownames = TRUE)
    )
    
  })
    
}

# Run  ----
shinyApp(ui, server)


Thank you.

WannabeSmith
  • 435
  • 4
  • 18

1 Answers1

2

A simple fix before the infile statement would solve

 req(input$xlsxfile)

-server

server <- function(input, output) {
  
  # File handler ----
  mydata <- reactive({
    req(input$xlsxfile)
    inFile <- input$xlsxfile
    
    
    
    req(input$xlsxfile,
        file.exists(input$xlsxfile$datapath))
    
    openxlsx::read.xlsx(xlsxFile = inFile$datapath,
                        sheet = 1 ,
                        detectDates = TRUE,
                        sep.names = "_")
    
  })
  
  # Dynamically generate UI input when data is uploaded, only sow numeric columns ----
  output$checkbox <- renderUI({
    selectInput(inputId = "select_var", 
                label = "Select variables", 
                choices = c("", names(mydata() %>%
                                        dplyr::select_if(is.numeric))),
                selected = NULL, 
                multiple = FALSE)
  })
  
  # Select columns to print ----
  df_sel <- reactive({
    req(input$select_var)
    df_sel <- mydata() %>% 
      dplyr::select(input$select_var, date)
  })
  
  
  # Same as above but for global.R variable  ----
  output$checkbox_2 <- renderUI({
    selectInput(inputId = "select_var_2", 
                label = "Select variables", 
                choices = c("", names(local_iris %>%
                                        dplyr::select_if(is.numeric))),
                selected = NULL, 
                multiple = FALSE)
  })
  
  
  df_sel_global <- reactive({
    
    req(input$select_var_2)
    
    df_sel_global <- local_iris %>% 
      dplyr::select(input$select_var_2, Date)
  })
  
  
  # Join the dataframes together based on a key  ----
  joined_dfs <- reactive({
    
    df_joi <- dplyr::inner_join(df_sel(), df_sel_global(), by= c("date" = "Date")) %>%
      dplyr::select(input$select_var,input$select_var_2)
    
  })
  
  
  # Render data frame ----
  
  output$contents <- DT::renderDT(server = FALSE, {
    
    DT::datatable(
      if(input$disp == "head") {
        head(joined_dfs())
      }
      else {
        joined_dfs()
      }, filter = 'top', 
      extensions = c('Buttons'),
      options = list(scrollY = 600,
                     scrollX = TRUE,
                     pageLength = 20,
                     dom =  '<"float-left"l><"float-right"f>rt<"row"<"col-sm-4"B><"col-sm-4"i><"col-sm-4"p>>',
                     lengthMenu=  list(c(20, 40, 60, -1), 
                                       c('20', '40', '60','All')),
                     scrollCollapse= TRUE,
                     lengthChange = TRUE, 
                     widthChange= TRUE,
                     rownames = TRUE)
    )
    
  })
  
}

-testing

enter image description here

akrun
  • 874,273
  • 37
  • 540
  • 662