从R中的列表中选择许多项

从R中的列表中选择许多项,r,shiny,render,reactive,R,Shiny,Render,Reactive,我在Shiny中创建了一个应用程序,我想从下拉菜单中选择多个项目。不幸的是,我不知道如何在给定的菜单选择后减少列表中的项目。所有的线通过它合并成一个整体。我应该在代码中添加什么,以便每个模型都是一个单独的行。下面我放了一张带图表的图片 我的代码: library(shiny) library(plotly) library(readxl) library(shinyWidgets) library(shinydashboard) library(shinyjs) library(DT) df1

我在Shiny中创建了一个应用程序,我想从下拉菜单中选择多个项目。不幸的是,我不知道如何在给定的菜单选择后减少列表中的项目。所有的线通过它合并成一个整体。我应该在代码中添加什么,以便每个模型都是一个单独的行。下面我放了一张带图表的图片

我的代码:

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)

要在绘图上将每个模型显示为单独的一行,可以通过以下方式将数据集的模型列指定给绘图的颜色参数:

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