Search code examples
rshinydashboardshinydashboardflexdashboard

Display graphic according to selected input


I want to display a graphic in another tab according to selected input in the first tab.

For "auto", i want to display a graphic (named "graph_auto") in TAB B and if "boat" is selected, i want to display another graphic (named "graph_boat") in TAB B.This i what i did :

---
title: "my report"

output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
    social: menu

runtime: shiny
---


```{r setup, include=FALSE}
library(flexdashboard)
library(shiny)
library(htmltools)
library(flextable)
library(plotly)


```


RESULTS
=======================================================================


Column
-----------------------------------------------------------------------

### TAB A

```{r}
# creation du tibble sur ce KPI

my_data = data.frame(product = rep(c("auto", "boat"),each=2),
                     year = c("2009", "2011", "2005", "2019"),
                     price = c("10 000", "20 000", "7 000", "60 000"),
                     speed = c("220", "250", "70", "140"))


ui <- fluidPage(
  selectInput("product", "", choices = my_data$product),
  uiOutput("tbl")
)

server <- function(input, output, session) {
  output$tbl <- renderUI( {
    out <- subset(my_data, product ==input$product)
    library(flextable)
    htmltools_value((flextable(out)))
  })
}

shinyApp(ui, server)


```


Column 
-----------------------------------------------------------------------

### TAB B
```{r}
# if "auto" is selected, then display this graphic
graph_auto <- plot_ly(midwest, x = ~percollege, color = ~state, type = "box")

# if "boat" is selected, then display this graphic
graph_boat <- plot_ly(midwest, x = ~percollege, type = "box", width = 600, height = 400)



```

but i am stuck to display graphic according to the selected product. How can we access to selected input from another tab ? Some help would be appreciated


Solution

  • Is this what you had in mind (note that the first plot doesn't work)?

    ---
    title: "my report"
    
    output: 
      flexdashboard::flex_dashboard:
        orientation: columns
        vertical_layout: fill
        social: menu
    
    runtime: shiny
    ---
    
    
    ```{r setup, include=FALSE}
    library(htmltools)
    library(flextable)
    library(plotly)
    
    
    
    my_data = data.frame(product = rep(c("auto", "boat"),each=2),
                         year = c("2009", "2011", "2005", "2019"),
                         price = c("10 000", "20 000", "7 000", "60 000"),
                         speed = c("220", "250", "70", "140"))
    ```
    
    
    RESULTS
    =======================================================================
    
    
    Column
    -----------------------------------------------------------------------
    
    ### TAB A
    
    ```{r}
    selectInput("product", "", choices = my_data$product)
    
    renderUI({
      out <- subset(my_data, product ==input$product)
      htmltools_value((flextable(out)))
    })
    ```
    
    
    Column 
    -----------------------------------------------------------------------
    
    ### TAB B
    ```{r}
    # if "auto" is selected, then display this graphic
    renderPlotly({
      if (input$product == "auto") {
        plot_ly(midwest, x = ~percollege, color = ~state, type = "box")
      }
      # if "boat" is selected, then display this graphic
      if (input$product == "boat") {
        plot_ly(midwest, x = ~percollege, type = "box", width = 600, height = 400)
      }
    })
    ```
    

    You don't need to actually include the structure of a shiny app in flexdashboard but only individual UI/render elements. Have a look at the help page.