[英]How do I render either Leaflet or Plot in Flexdashboard basd on input filtered in a reactive function?
提示:本站为国内最大中英文翻译问答网站,提供中英文对照查看,鼠标放在中文字句上可显示英文原文。
我正在构建一个 Flexdashboard,如果选择了一个月,它应该呈现 plot,如果选择“全部”,则应该呈现 Leaflet map。 我使用反应式 function 来过滤我的输入 $month 并尝试使用“If else”function,如下所示,但它不起作用。
---
title: "Demo Dashboard"
#subtitle: "Dashboard still under development"
output:
flexdashboard::flex_dashboard:
source_code: embed
theme: lumen
vertical_layout: fill
runtime: shiny
---
{r setup, include=FALSE}
library(flexdashboard)
library(htmlwidgets)
library(tidyverse)
library(plotly)
library(shiny)
library(scales)
library(leaflet)
library(sf)
library(st)
{r global, include=FALSE}
rr <- read.csv("reporting_rate.csv")
sf <- read_sf("Enugu_LGA/u_boundary_lgas.shp")
reporting_rate <- rr %>%
select(LGA, Year, Month, Dataset, Reporting, Completeness, Timeliness, Rate)
左连接 (sf) 中的 shapefile 返回“NA”,所以我改用右连接。 我对 R 和 Flexdashboard 比较陌生。 我还尝试添加输入控件并创建一个响应式 function 以根据输入过滤我的数据。
reporting_rate <- sf %>%
right_join(r_rate,
by = c("lga_name" = "LGA"))
Month <- unique(reporting_rate$Month)
shinyWidgets::prettyRadioButtons(
inputId = "month",
label = "Month",
choices = c(Month, "All"),
selected = "Dec")
slicer <- reactive({
filter(reporting_rate, if(input$month == "All" ){Month == reporting_rate$Month}
else{Month == input$month}
})
如果从输入中选择“全部”,我现在希望呈现一个条形图 plot,如果选择了特定月份,则呈现一个 Leaflet map。
szn <- colorFactor(c("darkgreen", "orange", "navyblue"),
c("Enugu North", "Enugu East", "Enugu West"))
if(slicer()$month == "All"){
plotly::renderPlotly({
p <- slicer() %>%
ggplot(aes(x = reorder(LGA, -Rate), y = Rate, fill = dataSet))+
geom_col(stat = " identity", position = "dodge")+
theme(legend.position = "bottom")
plotly::ggplotly(p) %>%
layout(xaxis = list(
title = 'LGA',
tickangle=310))
})
}else
{renderLeaflet({
slicer() %>%
leaflet() %>%
addPolygons(fillColor = ~szn(LGA),
fillOpacity = 1,
color = "white",
weight = 1)
上面的代码导致此错误消息“警告:错误 in.getReactiveEnvironment()$currentContext:没有活动的反应上下文不允许操作。 • 你试图做一些只能从反应消费者内部完成的事情。”
我不知道还能做什么。 请帮忙。
我曾尝试使用if(input$month == "All")
而不是if(slicer()$input == "All")
,如下所示,但它仍然无效。
if input$month == "All"){
plotly::renderPlotly({
p <- slicer() %>%
ggplot(aes(x = reorder(LGA, -Rate), y = Rate, fill = dataSet))+
geom_col(stat = " identity", position = "dodge")+
theme(legend.position = "bottom")
plotly::ggplotly(p) %>%
layout(xaxis = list(
title = 'LGA',
tickangle=310))
})
}else
{renderLeaflet({
slicer() %>%
leaflet() %>%
addPolygons(fillColor = ~szn(LGA),
fillOpacity = 1,
color = "white",
weight = 1)
实现所需结果的一种选择是使用由input$month
触发的observeEvent
。 除此之外,使用observeEvent
有条件地呈现plotly
图表或leaflet
map 需要一个额外的步骤,即我们必须通过 uiOutput 将图表包装在renderUI
和uiOutput
中:
这是使用一些简单的假图表的这种方法的最小可重现示例:
---
title: "Demo Dashboard"
output:
flexdashboard::flex_dashboard:
source_code: embed
theme: lumen
vertical_layout: fill
runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
library(tidyverse)
library(plotly)
library(shiny)
library(leaflet)
```
```{r}
shinyWidgets::prettyRadioButtons(
inputId = "month",
label = "Month",
choices = c("Jan", "Dec", "All"),
selected = "Dec"
)
```
```{r}
observeEvent(input$month, {
if (!input$month == "All") {
p <- ggplot(mtcars, aes(hp, mpg)) +
geom_point()
output$plot <- renderUI({
renderPlotly({
p
})
})
} else {
output$plot <- renderUI({
renderLeaflet({
leaflet() %>%
addTiles() %>%
addMarkers(lng = 174.768, lat = -36.852, popup = "The birthplace of R")
})
})
}
})
uiOutput("plot")
```
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.