使Shiny

时间:2018-03-20 02:28:55

标签: r shiny shiny-reactivity

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

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

2)如何在下方设置我的代码,以便当用户选择某些国家/地区时,只有这些国家/地区会显示在下方的图表中?

3)如何格式化sliderInput的内容,以便年份而不是显示“2,012”,它会在滑块的仪表板中显示“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)

1 个答案:

答案 0 :(得分: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

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)

-output

enter image description here