0

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

目前我可以让它在 plot.ly 图上绘制一个选定的变量,但是即使我选择了多个变量,它仍然只会绘制选定的第一个变量:

当前单变量输出 在此处输入图像描述

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

首次显示时收到错误 在此处输入图像描述

这是迄今为止我正在使用的代码的简化版本:

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 ...

在过去的几天里,我在这里阅读了数十个线程,但是它们似乎都关注围绕多个 selectInput 参数的问题,而不是单个 selectInput 需要能够选择和显示多个输出的问题。

您能提供的任何帮助将不胜感激!

4

1 回答 1

0

一种方法是创建一个单独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"
  )
})
于 2020-01-28T08:31:03.017 回答