Reputation: 119
I'm trying to add a Shiny feature where it checks for missing values and if there are any, display a warning notifying the user that there are missing values present but still be able to output scatterplot normally. Is there a way to do this?
I know that the req() function can check for missing values and display an error message if conditions aren't met. However, the user can't proceed to continue plotting normally if the conditions aren't met, which is what I want to do.
For reference this is what my Shiny code looks like at the moment and I've just been using the iris dataset.
library(shiny)
library(datasets)
ui <- shinyUI(fluidPage(
titlePanel("Column Plot"),
tabsetPanel(
tabPanel("Upload File",
titlePanel("Uploading Files"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose CSV File',
accept=c('text/csv',
'text/comma-separated-values,text/plain',
'.csv')),
),
mainPanel(
tableOutput('datainput')
)
)
),
tabPanel("Scatterplot",
pageWithSidebar(
headerPanel('Scatterplot'),
sidebarPanel(
# User selected columns
selectInput('xcol', 'X', ""),
selectInput('ycol', 'Y', "", selected = "")
),
mainPanel(
plotOutput('NewPlot')
)
)
)
)
)
)
server <- shinyServer(function(input, output, session) {
data <- reactive({
req(input$file1)
inFile <- input$file1
df <- read.csv(inFile$datapath, header = input$header, sep = input$sep,
quote = input$quote)
updateSelectInput(session, inputId = 'xcol', label = 'X',
choices = names(df), selected = names(df))
updateSelectInput(session, inputId = 'ycol', label = 'Y',
choices = names(df), selected = names(df)[2])
return(df)
})
output$datainput <- renderTable({
data()
})
output$NewPlot <- renderPlot({
x <- data()[, c(input$xcol, input$ycol)]
plot(x)
})
})
shinyApp(ui, server)
Upvotes: 0
Views: 548
Reputation: 2217
You can notify the user using {shinytoastr}
. I could not reproduce your example so I made my own reprex:
library(shiny)
library(shinytoastr)
ir <- iris
# add missing values in Sepal.Length and Sepal.Width:
ir[1, c(1, 2)] <- NA
ui <- fluidPage(
shinytoastr::useToastr(),
selectInput(
inputId = "xcol",
label = "x column",
choices = colnames(ir),
selected = colnames(ir)[length(colnames(ir))]
),
selectInput(
inputId = "ycol",
labe = "y column",
choices = colnames(ir),
selected = colnames(ir)[2]
),
plotOutput(outputId = "theplot")
)
server <- function(input, output, session) {
output$theplot <- renderPlot({
data <- ir[, c(input$xcol, input$ycol)]
if (anyNA(data)) {
shinytoastr::toastr_warning(
message = "There are missing values in the selected columns. Plotting anyway.",
title = "Missing Values!"
)
}
plot(data)
})
}
shinyApp(ui, server)
Upvotes: 1