简体   繁体   中英

Selecting many items from the list in R

I created an application in Shiny where I would like to choose multiple items from the drop-down menu. Unfortunately, I don't know how to make items on the list reduce after a given menu selection. By which all lines merge into a whole. what should I add in the code so that each model is a separate line. Below I put a picture with charts.

My code:

library(shiny)
library(plotly)
library(readxl)
library(shinyWidgets)
library(shinydashboard)
library(shinyjs)
library(DT)

df1 <- data.frame(Month = rep(month.abb[1:12],10,replace = TRUE), Model = paste0('Ferrari ', rep(LETTERS[1:10], each = 12)),
                  Value = sample(c(0:300),120, replace = T), 
                  Car = rep('Ferrari', 10,each = 12), Year =  rep(2019:2020, each = 60),Country =  rep(c("USA","DE"), each = 12, times = 5), stringsAsFactors = F)

df2 <- data.frame(Month = rep(month.abb[1:12],10,replace = TRUE), Model = paste0('Porsche ', rep(LETTERS[1:10], each = 12)),
                  Value = sample(c(0:300),120, replace = T),
                  Car = rep('Porsche', 10,each = 12), Year =  rep(2019:2020, each = 60), Country =  rep(c("USA","DE"), each = 12, times = 5),stringsAsFactors = F)

data <-rbind(df1, df2)

ui <- fluidPage(
  
  titlePanel("Test"),
  sidebarLayout(
    sidebarPanel( width = 3,
                  uiOutput("category1"),
                  uiOutput("category2"),
                  uiOutput("category3"),
                  uiOutput("category4")),
      mainPanel(
      tabsetPanel(type = "tabs",
                  tabPanel("Plot", plotlyOutput("plot", height = 550,width = 1000))
      )
    )
  )
)

server <- function(input, output,session) {
  
  output$category1 <- renderUI({
    selectInput('cat1', 'Choose year:', multiple = T, selected = NULL, choices = sort(as.numeric(unique(data$Year))))
  })

  df_subset <- eventReactive(input$cat1,{
    if(input$cat1=="All") {df_subset <- data}
    else{df_subset <- data[data$Year == input$cat1,]}
  })
  
  df_subset1 <- reactive({
    if(is.null(input$cat2)){df_subset()} else {df_subset()[df_subset()$Country %in% input$cat2,]}
  })
  
  output$category2 <- renderUI({
    selectInput('cat2', 'Choose country:', choices = sort(as.character(unique(df_subset()$Country))), multiple = T, selected = NULL)
  })

  df_subset2 <- reactive({
    if(is.null(input$cat3)){df_subset1()} else {df_subset1()[df_subset1()$Car %in% input$cat3,]}
  })
  
  output$category3 <- renderUI({
    selectInput('cat3', 'Choose car:', choices = sort(as.character(unique(df_subset1()$Car))), multiple = F,  selected = NULL)
  })
  

  df_subset3 <- reactive({
    if(is.null(input$cat4)){df_subset2()} else {df_subset2()[df_subset2()$Model %in% input$cat4,]}
  })
  
  output$category4 <- renderUI({
    pickerInput('cat4', 'Choose model:', choices = sort(as.character(unique(df_subset2()$Model))), multiple = TRUE,  selected = NULL)
  })
  
  output$plot <- renderPlotly({
    
    xform <- list(categoryorder = "array",
                  categoryarray = df_subset3()$Month, 
                  title = " ",
                  nticks=12)
    
    plot_ly(data=df_subset3(), x=~Month,  y = ~Value, type = 'scatter', mode = 'lines', name = 'Value') %>% 
      layout(title = " ",xaxis = xform) %>%
      layout(legend = list(orientation = 'h', xanchor = "center", y=1.1, x=0.5))
  })
}
shinyApp(ui, server)

在此处输入图像描述

To display each model as a separate line on the plot, you can assign the Model column of your dataset to the color parameter of plot_ly this way:

plot_ly( data = df_subset3(), x = ~Month,  y = ~Value, color = ~Model, ...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM