在 Shiny 中使数据框列具有反应性

Making a data frame column reactive in Shiny

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

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

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

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

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)

我们可以 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)

-输出