dimanche 21 juin 2020

Dynamic Graphs not getting displayed properly in Shiny R?

I am trying to create a Shiny App. In this particular app i have a set of Radio buttons where choosing one will display a set of options below as Check Boxes and Choosing another radio button will choose other set of options and clicking on the checkboxes will generated graphs. Kindly find the UI and the Server Code below.

library(shiny)
library(shinydashboard)
library(shinyWidgets)
library(dplyr)
d <-
  data.frame(
    year = c(1995, 1995, 1995, 1996, 1996, 1996, 1997, 1997, 1997),
    Product_Name = c(
      "Table",
      "Chair",
      "Bed",
      "Table",
      "Chair",
      "Bed",
      "Table",
      "Chair",
      "Bed"
    ),
    Product_desc = c("X", "X", "X", "Y", "Y", "Y", "Z", "Z", "Z"),
    Cost = c(1, 2, 3, 4, 2, 3, 4, 5, 6)
  )

ui <- shinyUI(fluidPage(
  useShinydashboard(),
  tabPanel(
    "Plot",
    sidebarLayout(
      sidebarPanel(
        radioButtons(
          "Choose",
          "Choose One",
          c("Year" = "p", "Numbers" = "l")
        ),
        uiOutput('checkbox'),
        #width = 2,
        position = "bottom"),
      mainPanel(uiOutput("graph"),
                uiOutput("graph_1"))
      
    )
  )
))

server <- function(input, output, session) {
  
  z_1 <- reactiveValues(years = NULL)
  z_2 <- reactiveValues(numbers = NULL)
  
  observeEvent(input$X, {
    z_1$years <- input$X
  })
  
  observeEvent(input$X_1, {
    z_2$numbers <- input$X_1
  })
  
  output$checkbox <- renderUI({
    if (input$Choose == "p") {
      checkboxGroupInput("X",
                         "year",
                         choices = (unique(d$year)),selected = z_1$years)
      
    } else{
      checkboxGroupInput("X_1",
                         "Numbers",
                         choices = c("1","2","3","4"), ,selected = z_2$numbers)
    }
    
  })
  
  output$graph <- renderUI({
    ntabs = length(input$X)
    myTabs = lapply(seq_len(ntabs), function(i) {

        fluidRow(plotOutput(paste0("plot", i)))
    })
  })
  
  
  output$graph_1 <- renderUI({
    ntabs = length(input$X_1)
    myTabs = lapply(seq_len(ntabs), function(i) {

      fluidRow(plotOutput(paste0("plot_1", i)))
    })
  })
  
  
  observe (lapply(length(input$X), function(i) {
    output[[paste0("plot", i)]] <- renderPlot({
      if (length(input$X) > 0) {
        d %>%
          ggplot(aes(Product_Name, Cost)) +
          geom_col(aes(fill = Product_desc),
                   position = position_dodge(preserve = "single")) +
          facet_wrap( ~ input$X[i],
                      scales = "free_x",
                      strip.position = "bottom") +
          theme(strip.placement = "outside") +
          theme_bw()
      }
    })
    
  }))
  
  
  observe (lapply(length(input$X_1), function(i) {
    output[[paste0("plot_1", i)]] <- renderPlot({
      if (length(input$X_1) > 0) {
        d %>%
          ggplot(aes(Product_Name, Cost)) +
          theme(strip.placement = "outside") +
          theme_bw()
      }
    })
    
  }))
  
}

shinyApp(ui, server)

The choices in CheckboxGroupInput() are dynamic in my case. Depending upon the “Years” Radio Button Selection the Checkboxes would get generated. When the checkboxes are selected, corresponding Graphs will be getting generated in the main panel.

The Graph is getting generated, but when I choose the next “Numbers” radio button and select a checkbox under the "Numbers" radio button, the graph is getting generated for that checkbox selection But its below the previous graph that was generated in the previous "Years" radio Button checkbox selection. I am not sure why this particular issue is taking place.

I wanted only those graphs that get generated for the particular radio button selection and its corresponding Checkbox selections. According to the current code the checkbox selections would get retained even when the radio buttons are switched by the users. I don't want any change in this particular functionality please.

Please let me know your suggestions.




Aucun commentaire:

Enregistrer un commentaire