How to initialize an empty reactiveValues() for file input in Shiny?

Hello there,

I wonder how I can initialize an empty reactiveValues() when no file input is uploaded yet. I tried if(is.null(v$data)) {return(NULL)) here and there, but it didn't work out.

Currently, the code works fine when user upload file. However, when nothing has been uploaded, it always shows "argument of length 0". I don't want this warning show in the user interface.

I guess the problem can be data=NULL, but I'm not sure how to make it work without setting it to NULL. Any help is greatly appreciated!!!

thank you!

library(shiny)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(data.table)
#> 
#> Attaching package: 'data.table'
#> The following objects are masked from 'package:dplyr':
#> 
#>     between, first, last
library(tidyr)
library(DT)
#> 
#> Attaching package: 'DT'
#> The following objects are masked from 'package:shiny':
#> 
#>     dataTableOutput, renderDataTable

df1<-data.frame("Network"=c("50K", "45K", "40K","30K"),"Year"=c(2021,2022,2023,2024),"Rate"=c(0.1,0.2,0.3,0.4))
df2<-data.frame("BG"=c("B","G"))
df3<-data.frame("RD"=c("R","D"))


df<-df1 %>% merge(df2) %>% merge(df3)



raw<-function(data,input){
  
  data<-data %>% filter(BG %in% input$BG, RD %in% input$RD) 
  
  if ('Select All' %in% input$Network | is.null(input$Network)) {
    
  } else {
    data <- data %>% filter(Network %in% input$Network)
  }
  data<-data %>% spread(key=Year, value=Rate, fill = FALSE)
  return(data)
}
### Module for Rate
modFunction <- function(input, output, session, data) {
  
  v <- reactiveValues(data = NULL)
  
  observeEvent(data(), {
    v$data <- data()
  })
  
  output$mod_table <- DT::renderDataTable({
    DT::datatable(v$data) %>%
      formatPercentage(c(6:ncol(v$data)), 2)
    
  })
}

modFunctionUI <- function(id) {
  ns <- NS(id)
  DT::dataTableOutput(ns("mod_table"))
  
}


modFunctionUI <- function(id) {
  ns <- NS(id)
  DT::dataTableOutput(ns("mod_table"))
  
}



ui <- fluidPage(
  titlePanel("Input"),
  sidebarLayout(
    sidebarPanel(
      fileInput("file1", "Choose Data",
                multiple = FALSE,
                accept = c("text/csv",
                           "text/comma-separated-values,text/plain",
                           ".csv")),
      
      tags$hr(),
      
      selectInput("Network","Choose a network:",
                  choices = c("Select All",unique(toupper(df$Network))),
                  selected = 'Select All'),
      selectInput("BG","Choose B or G:",
                  choices = c(unique(df$BG)),
                  selected = "B"),
      selectInput("RD","Choose R or D:",
                  choices = c(unique(df$RD)),
                  selected = "R")),
    
    mainPanel(
      modFunctionUI("editable")
    )
  )
)



# Define server logic for random distribution app ----
server <- function(input, output) {
  
  data1<-reactive({
    
    req(input$file1)
    data1<-fread(input$file1$datapath, header = TRUE, sep = ",", stringsAsFactors = FALSE)
    raw(data1, input) 
    
  })
  
  callModule(modFunction,"editable", data1)
  
}

shinyApp(ui=ui, server = server)

Shiny applications not supported in static R Markdown documents

Created on 2020-10-23 by the reprex package (v0.3.0)
...

Hi @ llyvslly,

I would suggest to make use of the famous req() function that shiny provides to check the "truthiness".

From the help:

Ensure that values are available ("truthy"–see Details) before proceeding with a calculation or action. If any of the given values is not truthy, the operation is stopped by raising a "silent" exception (not logged by Shiny, nor displayed in the Shiny app's UI).

And here is a snippet from your code:

### Module for Rate
modFunction <- function(input, output, session, data) {
  
[...]
  
  output$mod_table <- DT::renderDataTable({
    # HERE it comes:
    req(v$data)
    DT::datatable(v$data) %>%
      formatPercentage(c(6:ncol(v$data)), 2)
    
  })
}

Hi @smichal,

thank you so much for your help! It works great. Perfect solution for me.

1 Like

This topic was automatically closed 7 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.