简体   繁体   中英

R Shiny checkboxGroupInput - Actions based on different combinations?

I'm working with R shiny to build a simple plotting app, and I don't think I really understand how checkboxGroupInput works, and the documentation from the R shiny app website isn't too helpful, so I was hoping to get some clarification.

This is what my checkboxGroupInput looks like:

checkboxGroupInput("trend1DisplayOptions", "Graph Display Options",
                                    choiceNames = list("Connect Data Points", "Display Loess Smoother with Uncertainty Bounds"),
                                    choiceValues = list("connect", "loess"),
                                    selected = "connect")

So, I'm essentially looking to display 4 different "types" of plots depending on which checkboxes are selected (whether to display loess/connect points or not).

And in my server code, here are the if/else statements:

if (input$trend1DisplayOptions == "connect" && input$trend1DisplayOptions != "loess") {
  # Only connect points
  p <- plot_ly(x = df2$labels, y = df2$values, type = 'scatter', mode='lines+markers') %>%
    layout(xaxis = list(type = "category")) 
  p

# Probably wrong way to check
} else if (input$trend1DisplayOptions == "connect" && input$trend1DisplayOptions == "loess") {
  # Should display nothing

# Probably wrong way to check
} else if (input$trend1DisplayOptions != "connect" && input$trend1DisplayOptions == "loess") {
  # Should display nothing

} else if (is.null(input$trend1DisplayOptions)) {
  # Should show
  p <- plot_ly(x = x_labels, y = df$values, type = 'scatter') %>%
    layout(xaxis = list(type = "category"), title = input$trendTitle) 
  p

}

The issue I'm running into is that when both checkboxes are selected, the shiny app should display nothing, but it still displays the plot from the first if statement (ie when only "connect" is selected). And, when neither are selected, I get a missing value where TRUE/FALSE needed error.

What did I misunderstand about checkboxGroupInput ?

I can see the issue is assuming the input values are equal to 2 things at the same time. So, what is the correct way to check if checkbox 1 is selected and checkbox 2 isn't, and vice versa? Also, I've searched on this forum to see how to deal with when neither are selected, and it seems is.null() is the right way to go. Why isn't it working for my case?

Looks like I misread one of the SO answers. I should be using %in% and !(... %in% ...) to check for combinations.

Here is the correct version if anyone needs it:

if ("connect" %in% input$trend1DisplayOptions && !("loess" %in% input$trend1DisplayOptions)) {
  p <- plot_ly(x = df2$labels, y = df2$values, type = 'scatter', mode='lines+markers') %>%
    layout(xaxis = list(type = "category")) 
  p


} else if (!("connect" %in% input$trend1DisplayOptions) && "loess" %in% input$trend1DisplayOptions) {
  # Should display nothing


} else if ("connect" %in% input$trend1DisplayOptions && "loess" %in% input$trend1DisplayOptions) {
  # Should display nothing


} else if (is.null(input$trend1DisplayOptions)) {
  p <- plot_ly(x = df2$labels, y = df2$values, type = 'scatter') %>%
    layout(xaxis = list(type = "category"), title = input$trendTitle) 
  p

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM