繁体   English   中英

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

[英]Making a data frame column reactive in Shiny

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

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)

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

-输出

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM