r语言 - 如何在 shiny = 中连接多个过滤器(范围>选择输入>组复选框)



我最近开始使用Shiny进行实验,您可以想象出许多问题。我得到了输入和输出的概念,但是我们必须将它们连接起来,但是,我仍然渴望创建基于某些人生的过滤器。(或通常如何连接多个过滤器(我已经检查了已经相似的文章(例如这个r Shiny( - 如何通过CheckboxGroupInput过滤,但是如果我看不到数据,我很难找到想法,这就是为什么我希望某人能够能够帮助我以我的榜样。

因此,我的目标是创建一个基于多个条件的仍然非常简单的应用程序。我有一个有关城市的数据集,该城市根据其国籍(在过去的10年中(迁移到不同地区的迁移

因此看起来有点像:(我的数据集称为eu1(


Year Districts  Country  Amount
2018 District_1    UK     70
2017 District_1    UK     63
2016 District_1    UK     48
2015 District_1    UK     55
2018 District_1    Fr     35
2017 District_1    Fr     41
2016 District_1    Fr     39
2015 District_1    Fr     30
2018 District_1    Ger   2526
2017 District_1    Ger   2459
2016 District_1    Ger   2326
2015 District_1    Ger   2225
2018 District_2    Fr      8
2017 District_2    Fr      6
2016 District_2    Fr      7
2015 District_2    Fr     14
2018 District_2    UK     23
2017 District_2    UK     25
2016 District_2    UK     28
2015 District_2    UK     29
2018 District_2    Ger    734
2017 District_2    Ger    713
2016 District_2    Ger    696
2015 District_2    Ger    698

这是我的数据集的简化版本(我当然还有更多变量(

ui

ui <- fluidPage(
  setBackgroundColor(color = c("#66e0ff", "#00a3cc", "#003d4d")),
  titlePanel("Migration"),
  sidebarLayout(
    sidebarPanel(tags$style(".well {background-color:#e6f9ff;}"),
      sliderInput(inputId = "range",
                             label = "Chose the year range:",
                             min = 2002, max = 2020, value = c(2002,2020)),
      selectInput(inputId = "dis",
                  label = "Chose the district",
                  choices = unique(Eu1$District)),
      checkboxGroupInput(inputId = "con",
                  label = "Chose the place of birth",
                  choices = unique(Eu1$country))
    ),#sidebar panel
    mainPanel(plotOutput("graph1")) # closing main panel
  )#sidelayout panel
)#fluid page

UI的运作良好

我也开始构建服务器,但我目前被困。

server <- function(input, output){

  # reactive range 
  df_range <- reactive({
    cbind(input$range[1],input$range[2])
  })
  # first filter based on distric
  op1 <- reactive({
    Eu1 %>% filter(District == input$dis)
  })
  # second filter for countries
  op2 <- reactive({
    op1() %>% filter(country== input$con)
  })
  # filtering first graph
  df_range <- reactive({
    filter(Eu1, between(Year,input$range[1],input$range[2])) %>% 
      select(Year, input$dis,input$con) 
  })
  # Ensures that our filter works properly
  observe(print(str(df_range())))

  # create a graph 
  output$graph1 <- renderPlot({
  ggplot(df_range(), aes(x=input$range, y=Eu1$Amount)) +
    geom_line(aes(colour=input$con)) +
    geom_point()
  })
}

您可能会看到,在某个时候,我在那里完全迷失了。

我期望的结果是:1.我们有多年的范围(这应该始终像第一个过滤器一样反应性(2.我们有SelectInput(地区(,以便用户可以选择城市的欲望区(他指定的几年(3.所选择的地区可以选择应在线图中显示的所需国籍。

因此,结果基本上是一条线图,它显示了不同国籍向不同地区的迁移,用户应该有能力在一个图中比较所需的国籍(因此,F.I.看到Ger和FR的有多少人搬到dis1(,如果他们只想看到GER,则只需单击GroupClickbox中的GER。

我很抱歉,如果这很简单,但我仍然没有完全了解如何将不同过滤器的反应性连接起来,因此,我想在我的数据上看到它我会更好地了解一切如何连接。因此,非常欢迎详尽的解释!

非常感谢您!

我认为这应该做到:

library(shiny)
library(shinyWidgets)
library(ggplot2)
library(dplyr)
Eu1 <- data.frame(stringsAsFactors=FALSE,
                  Year = c(2018, 2017, 2016, 2015, 2018, 2017, 2016, 2015, 2018, 2017,
                           2016, 2015, 2018, 2017, 2016, 2015, 2018, 2017, 2016, 2015,
                           2018, 2017, 2016, 2015),
                  Districts = c("District_1", "District_1", "District_1", "District_1",
                                "District_1", "District_1", "District_1", "District_1",
                                "District_1", "District_1", "District_1", "District_1", "District_2",
                                "District_2", "District_2", "District_2", "District_2",
                                "District_2", "District_2", "District_2", "District_2", "District_2",
                                "District_2", "District_2"),
                  Country = c("UK", "UK", "UK", "UK", "Fr", "Fr", "Fr", "Fr", "Ger", "Ger",
                              "Ger", "Ger", "Fr", "Fr", "Fr", "Fr", "UK", "UK", "UK", "UK",
                              "Ger", "Ger", "Ger", "Ger"),
                  Amount = c(70, 63, 48, 55, 35, 41, 39, 30, 2526, 2459, 2326, 2225, 8, 6,
                             7, 14, 23, 25, 28, 29, 734, 713, 696, 698)
)

ui <- fluidPage(
  setBackgroundColor(color = c("#66e0ff", "#00a3cc", "#003d4d")),
  titlePanel("Migration"),
  sidebarLayout(
    sidebarPanel(tags$style(".well {background-color:#e6f9ff;}"),
                 sliderInput(inputId = "range",
                             label = "Chose the year range:",
                             min = 2002, max = 2020, value = c(2002,2020)),
                 selectInput(inputId = "dis",
                             label = "Chose the district",
                             choices = unique(Eu1$District)),
                 checkboxGroupInput(inputId = "con",
                                    label = "Chose the place of birth",
                                    choices = unique(Eu1$Country),
                                    selected = unique(Eu1$Country)[1])
    ),#sidebar panel
    mainPanel(plotOutput("graph1")) # closing main panel
  )#sidelayout panel
)#fluid page
server <- function(input, output){
  df_dat <- reactive({
    # make sure inputs are not NULL
    req(input$con, input$dis, input$range) 
    # filter data according to inputs made by the user
    df_dat <- filter(Eu1, between(Year, input$range[1], input$range[2]), Districts == input$dis, Country %in% input$con)
    return(df_dat)
  })
  # Ensures that our filter works properly
  observe(print(str(df_dat())))
  # create a graph 
  output$graph1 <- renderPlot({
    # use to debug:
    # browser()
    # make sure filtered data is not NULL
    req(df_dat())
    # plot filtered data
    ggplot(df_dat(), aes(x = Year, y = Amount)) +
      geom_line(aes(colour = Country)) +
      geom_point()
  })
}
shinyApp(ui = ui, server = server)

有几个问题 - 例如:

  • 严格对您的列名称的上部和下部案例
  • 尝试使用select,但需要filter
  • ReactiveVal df_range定义了两次

最新更新