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)