samedi 29 juin 2019

Draw multiple plots based on user checkbox selection in R Shiny

I have created an R shiny app, which asks user to enter certain values and select checkbox. I want o plot graphs based on checkbox. Plot get changes when checkbox is selected by user but I want to plot multiple graphs based on user selection checkbox. Right now I am getting only one checkboxGroupInput(I have gone through the following urls Multiple plots according to checkboxGroupInput , Combining R shiny checkboxGroupInput with other input selections but none seems to be working in my case. It might be a case that I am doing something wrong)

library(shiny)
library(rjson)
library(dplyr)
library(plotly)
library(DT)
require(gridExtra)
require(ggplot2)
l='[{"a": "abc", "date": "20190506","model": "honda", "features":"weather", "value": "10"},
{"a": "abc", "date": "20190506","model": "honda", "features":"bad", "value": "14"},
{"a": "abc", "date": "20190506","model": "honda", "features":"failure", "value": "20"},
{"a": "abc", "date": "20190506","model": "honda", "features":"not", "value": "1"},
{"a": "abc", "date": "20190506","model": "honda", "features":"search", "value": "24"},
{"a": "abc", "date": "20190506","model": "honda", "features":"esrs", "value": "2"},
{"a": "abc", "date": "20190506","model": "honda", "features":"issue", "value": "1"},
{"a": "abc", "date": "20190506","model": "honda", "features":"errors", "value": "30"},

{"a": "abc", "date": "20190510","model": "ford", "features":"ice", "value": "12"},
{"a": "xyz", "date": "20190509", "model": "honda", "features":"summer", "value":"18"},
{"a": "xyz", "date": "20190507", "model": "ford", "features":"hot", "value":"14"},

{"a": "abc", "date": "20190506","model": "ford", "features":"search", "value": "20"},
{"a": "abc", "date": "20190510","model": "honda", "features":"400", "value": "18"},
{"a": "xyz", "date": "20190509", "model": "ford", "features":"fail", "value":"24"},
{"a": "xyz", "date": "20190507", "model": "honda", "features":"200", "value":"15"}]'
l = fromJSON(l)
df = data.frame(do.call(rbind, l))

ui <- # Use a fluid Bootstrap layout
  fluidPage(

    # Give the page a title
    titlePanel("Top Terms by System"),

    # Generate a row with a sidebar

        selectInput("serialNumber", "Serial Number:",
                    choices=df$a),

        selectInput("date", "Date:",
                    choices=NULL),

        # checkboxGroupInput("plots", "draw plots:", 
        #                    choices=list("ford", "honda")),
    mainPanel(
    uiOutput("checkbox"),

    plotlyOutput("plot")
    # plotOutput("plot_list")
    )

    )

  # server

server <- # Define a server for the Shiny app
    function(input, output, session) {


      observe({
        print(input$serialNumber)
        # x = df %>% filter(date == input$date) %>% select(a)
        x = df %>% filter(a == input$serialNumber) %>% select(date)
        updateSelectInput(session, "date", "date", choices = x)
      })

      observe({
        # print(input$serialNumber)
        logselected = df$model[df$date == input$date]
        updateSelectInput(session, "models", "model", choices = logselected)
      })

      output$data = renderTable({
        selectedVal = subset(df, (df$a == input$serialNumber & df$date == input$date & df$model==input$models))


      })

      output$checkbox <- renderUI({
        choice <-  unique(df[df$a %in% input$serialNumber, "model"])
        print(choice)
        checkboxGroupInput("checkbox","Select model", choices = choice)

      })

      filtered_data <- reactive({
        req(input$serialNumber)
        req(input$date)
        req(input$checkbox)

        data = df[df$a==input$serialNumber & df$date == input$date& df$model==input$checkbox,]
        print(data)
      })

      # output$table=renderDataTable({
      #   filtered_data()
      # })
      output$plot <- renderPlotly({

        plot_ly(
          filtered_data(),
          y = ~features,
          x = ~value,
          width = 0.1,
          orientation='h',
          type = "bar"
        )
      })
      # output$plot <- renderPlotly({
      #   # selectedVal = subset(df, (df$a == input$serialNumber & df$date == input$date & df$model==input$models))
      #   data <-  df %>% filter(a %in% input$serialNumber)  %>% filter(date %in% input$date) %>% filter(model %in% input$checkbox)
      #   # df[input$serialNumber, input$date, input$models]
      #   plot_ly(
      #     y = data$features,
      #     x = data$value,
      #     width = 0.1,
      #     orientation='h',
      #     type = "bar"
      #   )
      # })

    }

shinyApp(ui = ui, server = server)




Aucun commentaire:

Enregistrer un commentaire