I tried with code in Flexdashboard and it is working partially. There are 3 filters here "Performance", "Class" and "Product Family"
- When Performance is plot, Class is All and Product Family is All, the plot is displayed and it is perfect.
- When Performance is plot, Class is "A" and Product Family is empty, the plot is displayed and it is perfect.
- But When Performance is plot, Class is "A" and Product Family is "Product Family 1", the plot is not displayed for Product Family 1, instead there is a plot for all three families.
Basically I need these filters to be used as my wish. Is there a way to do above thing?
---
title: "ABC INC"
runtime: shiny
output:
flexdashboard::flex_dashboard:
orientation: rows
vertical_layout: scroll
source_code: embed
theme: cosmo
---
```{r setup, include=FALSE}
library(flexdashboard)
library(readxl)
library(tidyverse)
library(ggplot2)
library(plotly)
library(reshape)
```
```{r}
df <- structure(list(Class = c("A", "A", "A", "A", "A", "B", "B", "B",
"B", "B"), Product = c("Product 1", "Product 2", "Product 3",
"Product 4", "Product 5", "Product 1", "Product 2", "Product 3",
"Product 4", "Product 5"), `Product Family` = c("Product Family 1",
"Product Family 1", "Product Family 1", "Product Family 2", "Product Family 3",
"Product Family 1", "Product Family 1", "Product Family 1", "Product Family 2",
"Product Family 3"), `Month Ending Date` = structure(c(1420070400,
1422748800, 1425168000, 1427846400, 1430438400, 1420070400, 1422748800,
1425168000, 1427846400, 1430438400), class = c("POSIXct", "POSIXt"
), tzone = "UTC"), Spend = c(95, 93, 98, 100, 93, 95, 93, 98,
100, 93)), row.names = c(NA, -10L), class = c("tbl_df", "tbl",
"data.frame"))
df <- as.data.frame(df)
colnames(df) <- gsub(" ","_",colnames(df))
df$Month_Ending_Date <- as.Date(df$Month_Ending_Date)
```
Performance
=======================
Filters {.sidebar}
------------------
```{r}
selectInput("Plot",h5("Performance"),choices = c("","Plot"))
selectInput("Class",h5("Class"),choices = c("All",levels(factor(df$Class))),multiple = TRUE)
selectInput("Product_Family",h5("Product Family"),choices =c("All",levels(factor(df$Product_Family))),multiple = TRUE)
```
Output
------------------
### Chart A {data-height=1000}
```{r}
plotOutput("graph",height = "5000px",width = "1000px")
sel_data <- reactive({
df[df$Class %in% input$Class | df$Product_Family %in% input$ProductFamily,]
})
graph <- reactive({
if (input$Plot == "Plot" && (input$Class == "All" || input$Class != "All" )) {
ggplot(sel_data(),aes(x=Month_Ending_Date,y=Spend,color =
Product))+geom_point()+facet_wrap("Product_Family")+theme(legend.position = "none")+theme(legend.position = "none")
} else if (input$Plot == "Plot" && (input$Product_Family == "All" || input$Product_Family != "All")){
ggplot(sel_data(),aes(x=Month_Ending_Date,y=Spend,color =
Product))+geom_point()+facet_wrap("Product_Family")+theme(legend.position = "none")+geom_text(data = sel_data(),aes(x=Month_Ending_Date,label=round((Spend/10),1)),size=3,angle = 45, color = "black",vjust=-0.25)+theme(legend.position = "bottom")
}
})
output$graph <- renderPlot({
graph()
})
```