Radio button function rendering no results after execution

Hi Team,

Hope you all had a good day :slight_smile:

Not sure what wrong I am doing here in the below code. The logic looks correct. But only issue is in the last code (else if code). Let me explain the scenario. Renderplot has series of filters as shown below. The output has a trend plot (that you get once select "T"). Only addition I need is, as soon as I click "Yes" under Highlight Anomalies outliers should be highlighted. I cannot put the entire code here as it becomes lengthy. Hope I am have made problem understandable here. In short my else if function is not working after I click on "Yes".

Note : You can run the code in your system directly to see the problem and also I am sure the problem is in else if statement at the last that is

title: "Untitled"
    orientation: rows
    runtime: shiny

```{r setup, include=FALSE}

df <- structure(list(Date = structure(c(1506006000, 1506005700, 1506005400, 
1506006300, 1505883000, 1505883900, 1506248400, 1506249300), class = c("POSIXct", 
"POSIXt"), tzone = "UTC"), variable = c("A", "A", "B", "B", "A", 
"A", "B", "B"), value = c(745.173877823288, 685.732510117053, 
684.267831168306, 641.841992352615, 544.232023118396, 543.837484424048, 
542.964415849758, 542.949123696338), label = c("Outlier", "Outlier", 
"Outlier", "Outlier", NA, NA, NA, NA)), row.names = c(NA, -8L
), class = c("tbl_df", "tbl", "data.frame"))
df <-
df <- df %>% mutate(onlyDate = as.Date(Date))


Inputs {.sidebar}
selectInput("P","Filter1",choices = c("","T"))
selectInput("b","Var",choices = c("All",levels(factor(df$variable))),selected = "All",multiple = TRUE)
radioButtons("r",h5("Highlight Anomalies"),choices = list("No", "Yes"),selected = "No", inline = T)

Column {data-width=650}

### Chart A

output$g1 <- renderPlot({
    if (input$P == "T" & (input$r == "No" | input$r == "Yes")) {
        plot_data <- df

    if (input$P == "T" & (input$r == "No" | input$r == "Yes") & input$b != "All") {
        plot_data <- plot_data %>% filter(variable %in% input$b)

    if (input$P == "T") 
        ggplot(plot_data, aes(x = Date, y = value, color = variable)) +
            geom_line(size =  .2)+theme(legend.position = "none")
else if (input$P == "T" & input$r == "Yes")
        ggplot(plot_data, aes(x = Date, y = value, color = variable)) +
            geom_line(size =  .2)+theme(legend.position = "none")+geom_point(data = df %>% filter(label == "Outlier"),aes(x=Date, y = value),color='red',size=1) 

Hi @vinayprakash808. Are your choices of plotting also depend on input$r. If input$r == "No", plot first, else if input$r == "Yes" plot second. Right? If yes, you miss one conditional phase in the first plot if (input$P == "T" & input$r == "No"). Because input$P == "T" always TRUE, that why you only get the first plot.

Yes your right. But, I have added this code if (input$P == "T" & input$r == "No") in the beginning right. I am not able to understand the logic here. If (input$P == "T") plots the graph, then (input$P == "T" & input$r == "Yes") should also plot another plot highlighting outliers right?

No, because you are asigning the plot for a single output, not two.

Hi , My answer to your question " should also plot another plot highlighting outliers right?" Yes your right. I need in the same plot/output it self

That was not my question, it was yours, and I was answering to it, the answer was

1 Like

Ok. Let me try re-coding it

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.