1

我是闪亮的新手,正在练习制作一个小应用程序。我试图让两列对用户做出的选择做出反应,但似乎无法在线找到一个简单的解决方案。我希望有人可以在这里帮助我。

1) 如何在下面设置我的代码,以便当用户更改sliderInput 中的年份时,下面的绘图会更新为仅显示所选年份的数据?

2)我如何在下面设置我的代码,以便当用户选择一些国家时,只有这些国家出现在下面的图中?

3)如何格式化sliderInput的内容,以便在滑块上的仪表板中显示“2012”而不是显示“2012”?

library(shiny)
library(plotly)
library(shinydashboard)

#Creating dataset
`Country Name`<- c("country 1", "country 2", "country 3",
                   "country 1", "country 2", "country 3",
                   "country 1", "country 2", "country 3",
                   "country 1", "country 2", "country 3")

Year <- c(2010, 2010, 2010,
          2011, 2011, 2011,
          2012, 2012, 2012,
          2013, 2013, 2013)

GDP <- c(2345, 2465, 3985,
         3453, 3748, 4847,
         5674, 4957, 5763,
         6475, 9387, 7564)

dataset <- data.frame(`Country Name`, Year, GDP)

#Creating shiny app
ui <- dashboardPage(
  dashboardHeader(title = "Top 3 Countries"),
  dashboardSidebar(

    sliderInput(inputId = "Year",
                label = "Choose a timeframe",
                min = min(dataset$Year), 
                max = max(dataset$Year),
                value = c(min(dataset$Year),max(dataset$Year)), 
                ticks = TRUE,
                round = TRUE,
                step = 1),

    selectizeInput("countries",
                   "Select Country:",
                   choices = c("country 1",
                               "country 2",
                               "country 3"),
                   selected = "country 2",
                   multiple = TRUE
    )
  ),
  dashboardBody(plotlyOutput("top3countries"))) 

server <- function(input, output) {

  output$top3countries <- renderPlotly({

    plot_ly(dataset, 
            x = ~Year,
            y = ~GDP,
            color = ~`Country Name`,
            mode = 'lines+markers')
  })
}

shinyApp(ui=ui, server=server)
4

1 回答 1

0

我们可以filter使用数据集来实现这一点

library(shiny)
library(plotly)
library(shinydashboard)
library(dplyr)

#Creating dataset
`Country Name`<- c("country 1", "country 2", "country 3",
                   "country 1", "country 2", "country 3",
                   "country 1", "country 2", "country 3",
                   "country 1", "country 2", "country 3")

Year <- c(2010, 2010, 2010,
          2011, 2011, 2011,
          2012, 2012, 2012,
          2013, 2013, 2013)

GDP <- c(2345, 2465, 3985,
         3453, 3748, 4847,
         5674, 4957, 5763,
         6475, 9387, 7564)

dataset <- data.frame(`Country Name`, Year, GDP, 
           check.names = FALSE, stringsAsFactors = FALSE)

-ui

ui <- dashboardPage(
  dashboardHeader(title = "Top 3 Countries"),
  dashboardSidebar(

    sliderInput(inputId = "Year",
                label = "Choose a timeframe",
                min = min(dataset$Year), 
                max = max(dataset$Year),
                value = c(min(dataset$Year),max(dataset$Year)), 
                ticks = TRUE,
                round = TRUE,
                step = 1),

    selectizeInput("countries",
                   "Select Country:",
                   choices = c("country 1",
                               "country 2",
                               "country 3"),
                   selected = "country 2",
                   multiple = TRUE
    )
  ),
  dashboardBody(plotlyOutput("top3countries"))) 

-服务器

server <- function(input, output) {



  output$top3countries <- renderPlotly({



    dataSub <- dataset %>%
                  filter(Year >= as.numeric(input$Year[1]), 
                         Year <= as.numeric(input$Year[2]), 
                         `Country Name` %in% input$countries)

    plot_ly(dataSub, 
            x = ~Year,
            y = ~GDP,
            color = ~`Country Name`,
            mode = 'lines+markers')
  })
}

-跑

shinyApp(ui=ui, server=server)

-输出

在此处输入图像描述

于 2018-03-20T06:06:00.970 回答