这里有Shiny的新用户。我正在尝试创建一个交互式地图,根据2组用户输入添加圆圈标记:每组有3个可能的输入。我似乎无法弄清楚如何编写代码来正确过滤数据,然后将过滤后的数据添加为圆形标记。过滤逻辑应该是这样的:
1)如果宗教/宗派/世界="全部",请跳过该特定过滤器。 (例如,如果宗教1 ="全部",不按宗教1过滤,但如果不是"全部")仍然按其余字段过滤。
2)如果面额1/2是"无",只返回宗教信息,并排除面额信息。 (例如,如果宗教1 ="基督徒",面额="无",地理="英国",只应映射英国的基督徒总计)。
3)对于所有其他特定输入,请应用&运营商。 (例如,如果宗教1 ="基督徒",命名="天主教",地理="英国",它应该只显示英国的天主教徒。)
我想我可以为每个可能的排列编写多个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())
})
}
更新:为了解决来自无功过滤器的子集化问题,我只是清理了我的数据而不是解决方法。它现在有效。
答案 0 :(得分:0)
我想如果没有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())
})
}