Filtering in FlexDashboard

I have a code that is running perfectly fine. But I have a filter option here at the side. Once I click on "Month" I should see the plots. But currently what is happening is that as soon as open the app the plots are displayed(this should not happen)

 ---
 title: "Untitled"
 output: 
 flexdashboard::flex_dashboard:
 orientation: rows
 vertical_layout: 
 runtime: shiny
 source_code: embed
 ---
library(flexdashboard)
library(readxl)
library(ggplot2)
library(reshape)
library(flexdashboard)
df <- structure(list(A = structure(c(1L, 4L, 6L, 1L, 8L, 2L, 7L, 3L, 
5L, 5L, 1L, 8L, 2L, 7L, 2L), .Label = c("asd", "dfg", "fgdsgd", 
"fsd", "gdfgd", "gs", "sdfg", "sf"), class = "factor"), B = c(29L, 
24L, 46L, 50L, 43L, 29L, 32L, 24L, 35L, 39L, 33L, 47L, 53L, 26L, 
31L), C = structure(c(8L, 5L, 1L, 6L, 3L, 2L, 9L, 7L, 6L, 3L, 
2L, 9L, 8L, 8L, 4L), .Label = c("asd", "er", "fg", "gf", "gfd", 
"gfg", "qw", "sf", "tr"), class = "factor"), D = c(36L, 56L, 
39L, 26L, 56L, 35L, 27L, 31L, 33L, 45L, 34L, 27L, 43L, 40L, 56L
), E = structure(c(9L, 4L, 3L, 4L, 2L, 7L, 10L, 8L, 6L, 2L, 1L, 
10L, 9L, 9L, 5L), .Label = c("er", "fg", "g", "gd", "gf", "gfg", 
"gtd", "qw", "sf", "tr"), class = "factor"), F = c(44L, 34L, 
37L, 23L, 37L, 51L, 28L, 36L, 33L, 31L, 39L, 43L, 25L, 37L, 43L
), num = 1:15), row.names = c(NA, -15L), class = "data.frame")
 Summary {data-width=650}
 ==========

 Month/Day{.sidebar}
-------------------
```{r}
selectInput("Interval","Intervals",choices = c("","Day","Month","Weekly","Hourly"))
```

Row
----------

### B Value

```{r}
gauge(paste(sum(df$B)),min = 0,max = 1000)
```

### D Value

```{r}

gauge(paste(sum(df$D)),min = 0,max = 1000)
```

I tried with the below code but still not coming

  ---
 title: "Untitled"
 output: 
 flexdashboard::flex_dashboard:
 orientation: rows
 vertical_layout: 
 runtime: shiny
 source_code: embed
 ---

 ```{r setup, include=FALSE}
 library(flexdashboard)
 library(readxl)
 library(ggplot2)
 library(reshape)
 ```

```{r}
  df <- structure(list(A = structure(c(1L, 4L, 6L, 1L, 8L, 2L, 7L, 3L, 
  5L, 5L, 1L, 8L, 2L, 7L, 2L), .Label = c("asd", "dfg", "fgdsgd", 
  "fsd", "gdfgd", "gs", "sdfg", "sf"), class = "factor"), B = c(29L, 
  24L, 46L, 50L, 43L, 29L, 32L, 24L, 35L, 39L, 33L, 47L, 53L, 26L, 
  31L), C = structure(c(8L, 5L, 1L, 6L, 3L, 2L, 9L, 7L, 6L, 3L, 
  2L, 9L, 8L, 8L, 4L), .Label = c("asd", "er", "fg", "gf", "gfd", 
 "gfg", "qw", "sf", "tr"), class = "factor"), D = c(36L, 56L, 
  39L, 26L, 56L, 35L, 27L, 31L, 33L, 45L, 34L, 27L, 43L, 40L, 56L
  ), E = structure(c(9L, 4L, 3L, 4L, 2L, 7L, 10L, 8L, 6L, 2L, 1L, 
 10L, 9L, 9L, 5L), .Label = c("er", "fg", "g", "gd", "gf", "gfg", 
 "gtd", "qw", "sf", "tr"), class = "factor"), F = c(44L, 34L, 
 37L, 23L, 37L, 51L, 28L, 36L, 33L, 31L, 39L, 43L, 25L, 37L, 43L
 ), num = 1:15), row.names = c(NA, -15L), class = "data.frame")
 ``
  Summary {data-width=650} 
 ==========

Month/Day{.sidebar}
-------------------
```{r}
selectInput("Interval","Intervals",choices = c("","Day","Month","Weekly","Hourly"))
 ```
 Row
 ----------
### B Value

 ```{r}
 plotOutput("graph")
 graph <- reactive({
 if(input$Interval == "Month"){
 gauge(paste(sum(df$B)),min = 0,max = 1000)}})
 output$graph <- renderGauge({
 graph()
 })
 ```

     ### D Value

   ```{r}
    gauge(paste(sum(df$D)),min = 0,max = 1000)
   ```

I think you just have to change plotOutput by gaugeOuput as you are using renderGauge

When I do that, graph appears only when Months is selected

Code
---
title: "Untitled"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
runtime: shiny
---
  
```{r setup, include=FALSE}
library(flexdashboard)
```

```{r}
df <- structure(list(A = structure(c(1L, 4L, 6L, 1L, 8L, 2L, 7L, 3L, 
                                     5L, 5L, 1L, 8L, 2L, 7L, 2L), .Label = c("asd", "dfg", "fgdsgd", 
                                                                             "fsd", "gdfgd", "gs", "sdfg", "sf"), class = "factor"), B = c(29L, 
                                                                                                                                           24L, 46L, 50L, 43L, 29L, 32L, 24L, 35L, 39L, 33L, 47L, 53L, 26L, 
                                                                                                                                           31L), C = structure(c(8L, 5L, 1L, 6L, 3L, 2L, 9L, 7L, 6L, 3L, 
                                                                                                                                                                 2L, 9L, 8L, 8L, 4L), .Label = c("asd", "er", "fg", "gf", "gfd", 
                                                                                                                                                                                                 "gfg", "qw", "sf", "tr"), class = "factor"), D = c(36L, 56L, 
                                                                                                                                                                                                                                                    39L, 26L, 56L, 35L, 27L, 31L, 33L, 45L, 34L, 27L, 43L, 40L, 56L
                                                                                                                                                                                                 ), E = structure(c(9L, 4L, 3L, 4L, 2L, 7L, 10L, 8L, 6L, 2L, 1L, 
                                                                                                                                                                                                                    10L, 9L, 9L, 5L), .Label = c("er", "fg", "g", "gd", "gf", "gfg", 
                                                                                                                                                                                                                                                 "gtd", "qw", "sf", "tr"), class = "factor"), F = c(44L, 34L, 
                                                                                                                                                                                                                                                                                                    37L, 23L, 37L, 51L, 28L, 36L, 33L, 31L, 39L, 43L, 25L, 37L, 43L
                                                                                                                                                                                                                                                 ), num = 1:15), row.names = c(NA, -15L), class = "data.frame")
```

Summary {data-width=650} 
==========
  
Month/Day {.sidebar}
-------------------

```{r}
selectInput("Interval","Intervals",choices = c("","Day","Month","Weekly","Hourly"))
```

Row
----------

### B Value
  
```{r}
gaugeOutput("graph")
graph <- reactive({
  if(input$Interval == "Month"){
    gauge(paste(sum(df$B)),min = 0,max = 1000)}})
output$graph <- renderGauge({
  graph()
})
```

### D Value

```{r}
gauge(paste(sum(df$D)),min = 0,max = 1000)
```

A kind of ,,, thanks. But other is not gauge is not coming. I tried to replicate the same code for other gauge as well. Below is the code

 ---
 title: "Untitled"
 output: 
 flexdashboard::flex_dashboard:
 orientation: rows
 runtime: shiny
 ---

 ```{r setup, include=FALSE}
 library(flexdashboard)
 ```

 ```{r}
df <- structure(list(A = structure(c(1L, 4L, 6L, 1L, 8L, 2L, 7L, 3L, 
                                 5L, 5L, 1L, 8L, 2L, 7L, 2L), .Label = c("asd", "dfg", "fgdsgd", 
                                                                         "fsd", "gdfgd", "gs", "sdfg", "sf"), class = 
 "factor"), B = c(29L, 
                                                                                                                                       24L, 
 46L, 50L, 43L, 29L, 32L, 24L, 35L, 39L, 33L, 47L, 53L, 26L, 
                                                                                                                                       31L), 
 C = structure(c(8L, 5L, 1L, 6L, 3L, 2L, 9L, 7L, 6L, 3L, 
                                                                                                                                                             
  2L, 9L, 8L, 8L, 4L), .Label = c("asd", "er", "fg", "gf", "gfd", 
                                                                                                                                                                                             
  "gfg", "qw", "sf", "tr"), class = "factor"), D = c(36L, 56L, 
                                                                                                                                                                                                                                                
  39L, 26L, 56L, 35L, 27L, 31L, 33L, 45L, 34L, 27L, 43L, 40L, 56L
                                                                                                                                                                                             
   ), E = structure(c(9L, 4L, 3L, 4L, 2L, 7L, 10L, 8L, 6L, 2L, 1L, 
                                                                                                                                                                                                                
    10L, 9L, 9L, 5L), .Label = c("er", "fg", "g", "gd", "gf", "gfg", 
                                                                                                                                                                                                                                             
    "gtd", "qw", "sf", "tr"), class = "factor"), F = c(44L, 34L, 
                                                                                                                                                                                                                                                                                                
      37L, 23L, 37L, 51L, 28L, 36L, 33L, 31L, 39L, 43L, 25L, 37L, 43L
                                                                                                                                                                                                                                             
      ), num = 1:15), row.names = c(NA, -15L), class = "data.frame")
      ```

 Summary {data-width=650} 
 ==========

 Month/Day {.sidebar}
 -------------------

 ```{r}
 selectInput("Interval","Intervals",choices = c("","Day","Month","Weekly","Hourly"))
 ```

 Row
 ----------

 ### B Value

```{r}
gaugeOutput("graph")
graph <- reactive({
if(input$Interval == "Month"){
 gauge(paste(sum(df$B)),min = 0,max = 1000)}})
 output$graph <- renderGauge({
graph()
})
```

 ### D Value

  ```{r}
 gaugeOutput("graph")
 graph <- reactive({
 if(input$Interval == "Month"){
 gauge(paste(sum(df$B)),min = 0,max = 1000)}})
 output$graph <- renderGauge({
 graph()
 })
 ```

I think in shiny you can have two outputs name the same. You can't give the same id.
If you use graph2 instead of graph for the seconde gauge, it is working fine.

 gaugeOutput("graph2")
 graph2 <- reactive({
 if(input$Interval == "Month"){
   gauge(paste(sum(df$B)),min = 0,max = 1000)}})
   output$graph2 <- renderGauge({
     graph2()
 })
1 Like

Very perfect and this is what i wanted. Thanks a lot for the help.

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

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