我正在开发一个使用传单包的闪亮应用程序。我的传单地图嵌入在 tabsetPanel 中,其中第一个选项卡显示绘图,当您单击第二个选项卡时,您将看到带有标记的传单地图。只要我在电脑上,一切都工作正常。
但是,当我在移动设备上打开闪亮的应用程序时,我可以单击第二个选项卡并查看带有标记的地图。但是,当我切换回第一个选项卡(plot1)并再次向下和向上滚动,然后单击第二个选项卡时,传单标记已消失,地图仅显示部分。
这是一个错误吗?
这是一个非常简单的示例脚本:
library(shiny)
library(dplyr)
library(ggplot2)
library(leaflet)
df <- read.csv(textConnection(
"name,Lat,Long,total
Item1,36.879872,-85.335353,231
Item2,35.445454,-84.384953,123
Item3,36.395842,-85.452312,321
Item4,37.989796,-86.233434,123
Item5,38.857421,-87.342342,213"
))
ui <- fluidPage(
fluidRow(
column(8,
h1("First Headline"),
tabsetPanel(type = "tabs",
tabPanel("plot1", plotOutput("plot1")),
tabPanel("view map", leafletOutput("map"))
)
), # col end
), # row end
fluidRow(
column(8,
h1("Another Headline"), plotOutput("plot2"),
), # col end
), # row end
) # fluidpage end
server <- function(input, output, session) {
# render plot1
output$plot1 <- renderPlot({
df %>% ggplot(aes(x = name, y = total, fill = total)) + geom_col()
}, res = 96)
# render leaflet
output$map <- renderLeaflet({
leaflet(df) %>% addProviderTiles(providers$OpenStreetMap) %>%
addCircleMarkers(lng = ~Long, lat = ~Lat)
})
# render plot2
output$plot2 <- renderPlot({
df %>% ggplot(aes(y = Long, x = Lat)) + geom_point()
}, res = 96, height = 700)
}
shinyApp(ui, server)
我通过添加observeEvent解决了这个问题。现在切换到第二个选项卡时会呈现传单。
library(shiny)
library(dplyr)
library(ggplot2)
library(leaflet)
df <- read.csv(textConnection(
"name,Lat,Long,total
Item1,36.879872,-85.335353,231
Item2,35.445454,-84.384953,123
Item3,36.395842,-85.452312,321
Item4,37.989796,-86.233434,123
Item5,38.857421,-87.342342,213"
))
ui <- fluidPage(
fluidRow(
column(8,
h1("First Headline"),
tabsetPanel(id = "tabs", type = "tabs",
tabPanel(value = "tab1", "plot1", plotOutput("plot1")),
tabPanel(value = "tab2", "view map", leafletOutput("map"))
)
), # col end
), # row end
fluidRow(
column(8,
h1("Another Headline"), plotOutput("plot2"),
), # col end
), # row end
) # fluidpage end
server <- function(input, output, session) {
# render leaflet second tab is active
observeEvent(input$tabs,{
if(input$tabs == "tab2")
output$map <- renderLeaflet({
leaflet(df) %>% addProviderTiles(providers$OpenStreetMap) %>%
addCircleMarkers(lng = ~Long, lat = ~Lat)
})
})
# render plot1
output$plot1 <- renderPlot({
df %>% ggplot(aes(x = name, y = total, fill = total)) + geom_col()
}, res = 96)
# render plot2
output$plot2 <- renderPlot({
df %>% ggplot(aes(y = Long, x = Lat)) + geom_point()
}, res = 96, height = 700)
}
shinyApp(ui, server)