在这里使用 Shiny 的新用户。我正在尝试创建一个交互式地图,它根据 2 组用户输入添加圆形标记:其中每组有 3 个可能的输入。我似乎无法弄清楚如何编写代码来正确过滤数据,然后将过滤后的数据添加为圆形标记。过滤逻辑应该是这样的:
1) 如果 Religion / Denomination / World = "All", 跳过那个特定的过滤器。(例如,如果宗教 1 =“全部”,则不按宗教 1 过滤,但如果它们不是“全部”,仍按其余字段过滤)。
2) 如果1 / 2 面额为“无”,则只返回宗教信息,不包括面额信息。(例如,如果 Religion 1 = "Christians", Denomination = "None", Geography = "United Kingdom",则只应映射英国的基督教总数)。
3) 对于所有其他特定输入,应用 & 运算符。(例如,如果 Religion 1 = "Christians", Denomination = "Catholics", Geography = "United Kingdom", 它应该只显示英国的天主教徒)。
我想我可以为每个可能的排列编写多个 if / else 语句,但是会有很多条件,我将不得不重复它们两次,以便它适用于第 1 组和第 2 组。
年份滑块输入过滤器应该是反应式的,而其余的 selectInputs 应该只在用户单击按钮后工作。
这是我的用户界面代码:
ui <- fluidPage (
titlePanel("Religious Affiliations from 1900", windowTitle = "Browser"),
sidebarLayout(
sidebarPanel(width = 3,
conditionalPanel(
condition = "input.conditionedPanels == 1",
sliderTextInput("years_map", "Select Year:",
choices = years, selected = 2000,
animate = TRUE, grid = TRUE),
selectInput("religion_1_map", "Religion 1",
choices = c("All", religion_names),
selectize = TRUE),
selectInput("denom_1_map", "Denomination 1",
choices = c("All", "None", denomination_names),
selectize = TRUE),
selectInput("geography_1_map", "Geography 1",
choices = c("World", all_geography, names(all_regions)), selectize = TRUE),
selectInput("religion_2_map", "Religion 2",
choices = c("All", religion_names),
selectize = TRUE),
selectInput("denom_2_map", "Denomination 2",
choices = c("All", "None", denomination_names),
selectize = TRUE),
selectInput("geography_2_map", "Geography 2",
choices = c("World", all_geography), selectize = TRUE),
actionButton("map_button", "Map my choices!")
),
mainPanel(width = 9,
tabsetPanel(
tabPanel("Maps",
leafletOutput("mymap"),
value = 1),
tabPanel("Stacked Area Chart",
"test2",
value = 2),
tabPanel("Pie Chart", value = 3),
tabPanel("Line Chart", value = 4),
tabPanel("Statistics", value = 5),
tabPanel("Data", value = 6),
id = "conditionedPanels"
)
server <- function(input, output) {
output$mymap <- renderLeaflet({
leaflet() %>% addTiles()
#Make the map dependent on the button
input$map_button
})
year <- reactive({
all_cleaned %>% filter(year == input$years_map)
})
religion_1 <- reactiveValues({
if(input$religion_1_map == "All") {
year()
}
else if(input$religion_1_map == "None") {
return()
}
else {
year() %>% filter(religion == input$religion_1_map)
}
})
denom_1 <- reactive({
if(input$denom_1_map == "All") {
religion_1()
}
else if(input$denom_1_map == "None") {
religion_1[!duplicated(religion_1[,'religion']), ]
}
else {
religion_1() %>% filter(denom == input$denom_1_map)
}
})
geography_1 <- reactive({
if(input$geography_1_map == "All") {
denom_1()
}
else if(input$geography_1_map == "None") {
return()
}
else {
denom_1() %>% filter(country_name %in% input$geography_1_map)
}
})
religion_2 <- reactiveValues({
if(input$religion_2_map == "All") {
year()
}
else if(input$religion_2_map == "None") {
return()
}
else {
year() %>% filter(religion == input$religion_2_map)
}
})
denom_2 <- reactive({
if(input$denom_2_map == "All") {
religion_2()
}
else if(input$denom_2_map == "None") {
religion_2[!duplicated(religion_2[,'religion']), ]
}
else {
religion_2() %>% filter(denom == input$denom_2_map)
}
})
geography_2 <- reactive({
if(input$geography_2_map == "All") {
denom_2()
}
else if(input$geography_2_map == "None") {
return()
}
else {
denom_2() %>% filter(country_name %in% input$geography_2_map)
}
})
proxy <- observeEvent(input$map_button, {
leafletProxy("mymap") %>%
addCircleMarkers(data = geography_1()) %>%
addCircleMarkers(data = geography_2())
})
}
更新:为了解决反应式过滤器的子集问题,我只是简单地清理了我的数据,而不是一种解决方法。它现在可以工作了。
我想没有你的server.R
文件很难给你一个正确的答案。在我看来,这是非常简单的reactive
和/或eventReactive
编程。最简单的方法是按照您希望在 UI 中显示的方式标记数据(条目和列)。这使得反应式编程成为可能。请上传您当前的server.R
文件或可复制的示例。
编辑:从reactive
闪亮的方式调用过滤数据集:year
-->year()
改变第二个反应:
religion_1 <- reactive({
if(input$religion_1_map == "All") {
year
}
else if(input$religion_1_map == "None") {
return()
}
else {
year %>% filter(religion == input$religion_1_map)
}
})
对此:
religion_1 <- reactive({
if(input$religion_1_map == "All") {
year()
}
else if(input$religion_1_map == "None") {
return()
}
else {
year() %>% filter(religion == input$religion_1_map)
}
})
以及其他人。
更新服务器 <- 函数(输入,输出){
output$mymap <- renderLeaflet({
leaflet() %>% addTiles()
#Make the map dependent on the button
input$map_button
})
year <- reactive({
all_cleaned %>% filter(year == input$years_map)
})
religion_1 <- reactiveValues({
if(input$religion_1_map == "All") {
year()
}
else if(input$religion_1_map == "None") {
return()
}
else {
year() %>% filter(religion == input$religion_1_map)
}
})
denom_1 <- reactive({
if(input$denom_1_map == "All") {
religion_1()
}
else if(input$denom_1_map == "None") {
unique(religion_1())
}
else {
religion_1() %>% filter(denom == input$denom_1_map)
}
})
geography_1 <- reactive({
if(input$geography_1_map == "All") {
denom_1()
}
else if(input$geography_1_map == "None") {
return()
}
else {
denom_1() %>% filter(country_name %in% input$geography_1_map)
}
})
religion_2 <- reactive({
if(input$religion_2_map == "All") {
year()
}
else if(input$religion_2_map == "None") {
return()
}
else {
year() %>% filter(religion == input$religion_2_map)
}
})
denom_2 <- reactive({
if(input$denom_2_map == "All") {
religion_2()
}
else if(input$denom_2_map == "None") {
unique(religion_2())
}
else {
religion_2() %>% filter(denom == input$denom_2_map)
}
})
geography_2 <- reactive({
if(input$geography_2_map == "All") {
denom_2()
}
else if(input$geography_2_map == "None") {
return()
}
else {
denom_2() %>% filter(country_name %in% input$geography_2_map)
}
})
proxy <- observeEvent(input$map_button, {
leafletProxy("mymap") %>%
addCircleMarkers(data = geography_1()) %>%
addCircleMarkers(data = geography_2())
})
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句