Hi Team,
Hope you all had a good day
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"
output:
flexdashboard::flex_dashboard:
orientation: rows
runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
library(tidyverse)
library(ggplot2)
library(shiny)
```
```{r}
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 <- as.data.frame(df)
df <- df %>% mutate(onlyDate = as.Date(Date))
```
Summary
=================
Inputs {.sidebar}
-----------------------------------------------------------------------
```{r}
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
```{r}
output$g1 <- renderPlot({
req(input$P)
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)
}
})
plotOutput("g1")
```