0

I have created a drop down menu with a list of parameter names (such as Calcium and Copper) using RShiny. Depending on the parameter the individual chooses, a graph should popup. However, when there is a missing value for a specific parameter in the USL, LSL, UCl, or LCL excel sheet that I pull in, I get an error message in the Shiny app that says: "missing value where TRUE/FALSE needed".

If, for example, one of the parameters has a missing value for LSL, how can I get RShiny to ignore this missing value, and deploy the graph associated with that parameter just without the LSL value? The USL, LSL, UCL, and LCL values are stored in the excel sheet, with an example of it for the Copper parameter here:

Parameter LSL USL Mean LCL UCL
Copper NA 1.2 N/A N/A 1.7

I want my code to ignore plotting the LSL, Mean, and LCL values since they are NA, and only plot the UCl and USL values. Instead, since there are some NA values, RShiny gives me the error message that says "missing value where TRUE/FALSE" when I select Copper from the drop down menu. Since there are no NA values for Calcium, a different parameter, the graph shows up perfectly.

I will attach my code for reference.

 data <- read.csv("data.csv")
excel <- read.csv("excel.csv") #where the missing values occur

name <- data %>% select(DrugSubstance_Calcium:Load_LoadDensity) #Parameter names
col <-  colnames(name) %>% as.list()

data$PSDate <- as.Date(data$PSDate, "%m/%d/%Y")  




ui <- fluidPage(h1("Dashboard"),
                selectInput("select",label = h4("Parameters") , choices = col),
                plotlyOutput(outputId = "p"),
                tableOutput("checker")
)

server <- function(input, output){
    reactivedata <- reactive({data %>% 
            filter(PSDate>=input$dateRange[1], PSDate<input$dateRange[2]) %>% select(PSDate, input$select)
    })
    x <- reactive({reactivedata() %>% drop_na() %>% pull(PSDate)})
    y <- reactive({
        reactivedata() %>% pull(input$select) %>% na.omit()
    })
    meaan = reactive({
        rep(excel[excel$Quality.Attributes == input$select, "Mean"],times=length(x()))
    })
    ucl = reactive({
        rep(excel[excel$Quality.Attributes == input$select, "UCL"],times=length(x()))
    })
    lcl = reactive({
        rep(excel[excel$Quality.Attributes == input$select, "LCL"],times=length(x()))
    })
    lsl = reactive({
        rep(excel[excel$Quality.Attributes == input$select, "LSL"],times=length(x()))
    })
    usl = reactive({
        rep(excel[excel$Quality.Attributes == input$select, "USL"],times=length(x()))
    })
    

    output$p <- renderPlotly({
        plot1 <- plot_ly(x = x(), y = y(), name = as.character(input$select), type = 'scatter', mode = 'markers+lines', color = I("#696969")) %>% 
            add_lines(y = meaan(), name = paste("Mean =", meaan()[1]), color = I("black")) %>%
            add_lines(y = ucl(), name = paste("UCL =", ucl()[1]), color = I("green")) %>%
            add_lines(y = lcl(), name = paste("LCL =", lcl()[1]), color = I("green")) %>%
            add_lines(y = usl(), name = paste("USL =", usl()[1]), color = I("red")) %>%
            add_lines(y = lsl(), name = paste("LSL =", lsl()[1]), color = I("red")) %>%
            layout(
                title = paste("X", input$select),
                xaxis = list(
                    rangeselector = list(
                        buttons = list(
                            list(
                                count = 3,
                                label = "3 mo",
                                step = "month",
                                stepmode = "backward"),
                            list(
                                count = 6,
                                label = "6 mo",
                                step = "month",
                                stepmode = "backward"),
                            list(
                                count = 1,
                                label = "1 yr",
                                step = "year",
                                stepmode = "backward"),
                            list(
                                count = 1,
                                label = "YTD",
                                step = "year",
                                stepmode = "todate"),
                            list(step = "all"))),
                    
                    rangeslider = list(type = "date")))
    })
}

shinyApp(ui = ui, server = server)
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jul 07 '21 at 22:58
  • Can you provide us a part of your data by editing your post to include output of `dput(head(data))` and `dput(head(excel))` ? – Ronak Shah Jul 08 '21 at 06:34

0 Answers0