我最近开始使用Shiny进行实验,您可以想象会出现许多问题。我得到了输入和输出的概念,我们必须将它们连接起来,但是,我仍在努力创建基于某些等级的过滤器。 (或者通常如何连接多个过滤器)我检查了已经类似的文章,例如R Shiny - How to filter by 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
用户界面效果很好
我也已经开始构建服务器,但此刻我被困住了。
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.选择地区,用户可以选择要显示在折线图中的所需国籍。
因此,结果基本上是一个折线图,显示了不同国籍向不同地区的迁移,并且用户应该能够在一张图中比较所需的国籍(因此,请查看有多少人来自Ger和FR Dis1),如果他们只想查看Ger,则可以在Groupclick框中单击Ger。
很抱歉这很简单,但是我仍然没有完全了解如何将不同过滤器的这种反应性相互联系,因此,我想在我的数据上看到它,因为那样的话就不太抽象了,我会更好地理解一切之间的联系。因此,非常欢迎进行详尽的解释!
非常感谢您!
答案 0 :(得分:1)
我认为应该这样做:
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
df_range
被定义了两次