我在 shiny 的应用程序中使用了传单,并且希望该应用程序在用户单击 map后打开一个包含绘图的窗口。我写了下面的代码:
library(shiny)
library(leaflet)
library(plotly)
library(dplyr)
ui <- fluidPage(
leafletOutput("mymap"),
)
server <- function(input, output, session) {
points <- eventReactive(input$recalc, {
cbind(rnorm(40) * 2 + 13, rnorm(40) + 48)
}, ignoreNULL = FALSE)
output$mymap <- renderLeaflet({
leaflet() %>%
addProviderTiles(providers$Stamen.TonerLite,
options = providerTileOptions(noWrap = TRUE)
) %>%
addMarkers(data = points())
})
observeEvent(input$mymap_marker_click,{
df <- data.frame(x = 1:10, y = 1:10)
plt <- ggplot(df, aes(x,y)) + geom_line()
pltly <- ggplotly(plt)
showModal(modalDialog(
title = "Important message", size = "l",
pltly ))
})
}
shinyApp(ui, server)
这类代码可以完成工作,但它显示出图形非常压缩!如果你将窗口的边界稍微向左或向右拖动,它将被修复,但是我希望它可以正常运行而无需修复图!有谁知道更好的方法吗?
你可以使用renderPlotly
和plotlyOutput
制作并显示图。
output$my_plotly <- renderPlotly({
df <- data.frame(x = 1:10, y = 1:10)
plt <- ggplot(df, aes(x,y)) + geom_line()
ggplotly(plt)
})
observeEvent(input$mymap_marker_click,{
showModal(modalDialog(plotlyOutput("my_plotly")))
})
上述解决方案有效,但是存在问题。在我的真实示例中,该图使用来自地图中单击的信息(例如(input $ mymap_marker_click $ id))来创建,因此我必须在“ renderPlotly”之前使用另一个“ observeEvent”获取ID,然后生成相应的情节。这种方法的问题是,当您单击其他位置时,第二个“ observeEvent”运行得更快,并显示前一个区域的图,直到第一个“ observeEvent + rednerPlotly”运行完毕并生成新图为止,并且只有在此模态之后自我更新。
随时发布有关该问题的新问题