What is the initial state of a reactive({}) object?

I am making a Rshiny app with a file upload system. As part of this system I need to build-in a check to make sure the data has the ''correct''/ expected columns. This check works fine, but I would like to have an initial/neutral ValueBox (in blue) that is shown before the user has uploaded any data. I have tried quite a bit, but al my solutions either show nothing until the data is uploaded or do not update once the data is actually uploaded. I think this has to do with the initial (empty) state of a value of a EventReactive/Reactive object. I know my way around R, but a bit newer to some of the Rshiny logics, can someone maybe help me here.

This is a MWE of what I am trying to do

library(shiny)
library(tidyverse)
library(shinydashboard)

## create some data
#data("mtcars")
#mtcars
#write_csv(mtcars, "mtcars.csv")

ui <- dashboardPage(
    
    dashboardHeader(title = "Test Shiny App"),
    dashboardSidebar(
        fileInput('data_set', 'Upload csv file here',
                  accept = c(".csv"))
    ),
    dashboardBody( 
        valueBoxOutput("test_1")
    )
)


server <- function(input, output){

    rawData <- eventReactive(eventExpr = {input$data_set},
                             {
                                 read.csv(input$data_set$datapath)
                             })
    
    
    check_1 <- reactive({
        all(c( "mpg", "cyl", "disp", "hp") %in% names(rawData()))
    })
    
    output$test_1 <- renderValueBox({
        
        # I Tried this from here https://stackoverflow.com/questions/35792052/initial-state-of-eventreactive
        # if(!exists("check_1()")){
        #     valueBox( value =  "Waiting for input", subtitle = "test",icon = icon("thumbs-up"), color = "aqua")
        # } else{
        
        # I also tried this
        #if(!is.logical(check_1())){
        #     valueBox( value =  "Waiting for input", subtitle = "test",icon = icon("thumbs-up"), color = "aqua")
        #     } else{
        
        valueBox(
            value =  if (check_1() ){paste0("Correct")} else {paste0("Warning")},
            subtitle =  if (check_1() ){paste0("Correct data was uploaded")} else {paste0("Please make sure the right data and columns are uploaded")},
            icon = if (check_1() ){icon("thumbs-up")} else {icon("thumbs-down")},
            color = if (check_1() ) "green" else "red"
        )
        #} # escape curly bracket for the else statement
        
    })
}

shinyApp(ui, server)

I understand, this would work

 if(is.null(input$data_set$datapath)){
            valueBox( value =  "Waiting for input", subtitle = "test",icon = icon("thumbs-up"), color = "aqua")
                } else{

but that does not feel like a nice solution somehow. I would like to consistently use check_1()


server <- function(input, output) {
  rawData <- eventReactive(
    eventExpr = {
      input$data_set
    },
    {
      if (isTruthy(input$data_set$datapath)) {
        read.csv(input$data_set$datapath)
      } else {
        NULL
      }
    },
    ignoreNULL = FALSE
  )


  check_1 <- reactive({
    all(c("mpg", "cyl", "disp", "hp") %in% names(rawData()))
  })

  output$test_1 <- renderValueBox({
    if (isTruthy(check_1())) {
      valueBox(
        value = "Correct",
        subtitle = "Correct data was uploaded",
        icon = icon("thumbs-up"),
        color = "green"
      )
    } else {
      valueBox(
        value = "warning",
        subtitle = "Please make sure the right data and columns are uploaded",
        icon = icon("thumbs-down"),
        color = "red"
      )
    }
  })
}

I do not think this places a placeholder. basically, I am looking for 3 states.

  1. no data has been uploaded = blue box
  2. correct data uploaded = green box
  3. incorrect data uploaded = red box
output$test_1 <- renderValueBox({
    
    if (!isTruthy(rawData())){
      valueBox(
        value = "Placeholder",
        subtitle = "No data was uploaded",
        color = "blue"
      )
    } else if (check_1()) {
      valueBox(
        value = "Correct",
        subtitle = "Correct data was uploaded",
        icon = icon("thumbs-up"),
        color = "green"
      )
    } else {
      valueBox(
        value = "warning",
        subtitle = "Please make sure the right data and columns are uploaded",
        icon = icon("thumbs-down"),
        color = "red"
      )
    }
  })

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.