具有多变量输出的 R Shiny selectInput 反应性

R Shiny selectInput reactivity with multiple variable output

我正在就闪亮的仪表板问题寻求帮助。 本质上,我只有一个 select 输入菜单,但是希望能够 select 多个变量并将它们全部绘制在同一个图表上。

目前我可以让它在 plot.ly 图表上绘制单个 selected 变量,但是即使我 select 多个变量,它仍然只会绘制第一个变量 selected:

当前单变量输出

此外,当应用程序首次 运行 时,我收到此错误,直到我手动 select 一个变量:

首次显示时收到错误

这是我目前使用的代码的简化版本:

global_indices <- read_excel("Market_Data.xlsx", 
                             sheet = 4,
                             col_names = FALSE, 
                             skip = 5)
global_indices_clean <- global_indicies[,c(1,3,7,9,13,21,23)]
colnames(global_indices_clean) <- c("Date", "Australia", "US", "UK", "Germany", "Japan", "Hong_Kong")
global_indices_2y2 <- global_indices_clean %>% filter(between(Date, now() - years(2), now()))


header <- dashboardHeader(
  title = "Dashboard"
)

sidebar <- dashboardSidebar(
  sidebarMenu(
    menuItem("Global Indices",
             tabName = "global_indices")
  )
)

body <- dashboardBody(
  tabItems(
    tabItem(tabName = "global_indices",
            fluidRow(
              box(plotlyOutput("plot_3"), title = "Developed indices", status = "primary", width = 4, ""),
              box(plotlyOutput(""), title = "", status = "primary", width = 4, ""),
              box(plotlyOutput(""), title = "", status = "primary", width = 4, "")
            ),
            fluidRow(
              box(selectInput("global_indices_input", "Indices", 
                              choices = 
                                list("Australia" = "Australia", 
                                     "US" = "US",
                                     "UK" = "UK", 
                                     "Germany" = "Germany",
                                     "Japan" = "Japan",
                                     "Hong Kong" = "Hong_Kong"),
                              multiple = TRUE), 
                  width = 4)
            )
    )
  )  
)


ui <- dashboardPage(
  header,
  sidebar,
  body
)


server <- function(input, output) {


  output$plot_3 <- renderPlotly({
    plot_3 <- plot_ly(
      global_indices_2y2, x = global_indices_2y2$Date, y = ~get(input$global_indices_input), type="scatter", mode="lines"
    )
  })

}

shinyApp(ui, server)

我无法提供数据集本身,但下面是其中的一小部分:

> str(global_indices_2y2)
'data.frame':   478 obs. of  7 variables:
 $ Date     : POSIXct, format: "2018-01-29" "2018-01-30" "2018-01-31" "2018-02-01" ...
 $ Australia: num  107 106 106 107 108 ...
 $ US       : num  113 112 112 112 110 ...
 $ UK       : num  104 103 102 102 101 ...
 $ Germany  : num  103.9 102.9 102.8 101.4 99.7 ...
 $ Japan    : num  116 114 113 115 114 ...
 $ Hong_Kong: num  120 118 119 118 118 ...

在过去的几天里,我已经阅读了这里的几十个话题,但是它们似乎都关注围绕多个 select 输入参数的问题,而不是单个 select 输入要求select 和显示多个输出的能力。

如果您能提供任何帮助,我们将不胜感激!

一种方法是创建一个单独的 reactive 来过滤您的数据,并在您的绘图中使用过滤后的数据。首先,我会考虑将您的数据转换为长格式(例如,使用 tidyverse pivot_longer)。例如:

global_indices_2y2 <- data.frame(
  Date = as.POSIXct(c("2018-01-29", "2018-01-30", "2018-01-31", "2018-02-01")),
  Australia = c(107, 106, 106, 107),
  US = c(113,112,112,112),
  UK = c(104,103,102,102)
) %>%
  pivot_longer(cols = -Date, names_to = "country", values_to = "index")

然后添加 reactive 以根据您 server 中的多项选择进行过滤:

mydata <- reactive({
  global_indices_2y2 %>%
    filter(country %in% input$global_indices_input)
})

然后绘制过滤后的数据:

output$plot_3 <- renderPlotly({
  plot_3 <- plot_ly(
    mydata(), 
    x = ~Date, 
    y = ~index, 
    name = ~country,
    type="scatter", 
    mode="lines"
  )
})