如何在R Shiny的Leaflet弹窗中创建链接打开专属仪表板?
Solution: Create Links to Dedicated Dashboards from Leaflet Selected Items in Shiny
Absolutely! You can add links in your Leaflet popups that open a dedicated dashboard for each selected country. The core idea is to pass the country's unique identifier as a parameter in the link, so your dashboard can load content specific to that country. Here's a modified version of your code with clear explanations of the key changes:
library(shiny) library(leaflet) library(sf) # Don't forget this package for `read_sf`! # Download and prepare country data (your original code, kept intact) download.file(url = "http://thematicmapping.org/downloads/TM_WORLD_BORDERS-0.3.zip", destfile = "TM_WORLD_BORDERS-0.3.zip") unzip(zipfile = "TM_WORLD_BORDERS-0.3.zip") world.borders <- read_sf(dsn = getwd(), layer = "TM_WORLD_BORDERS-0.3") world.borders <- world.borders[world.borders$NAME %in% c("Australia","United States"),] ui <- fluidPage( # Add a custom JS function to handle opening the dashboard with country parameter tags$script(HTML(" function openDashboard(country) { // Construct the URL to your dashboard (adjust this to match your actual dashboard location) // For an external dashboard: const url = 'https://your-dashboard-site.com?country=' + encodeURIComponent(country); // For an internal Shiny tab (new window): const url = window.location.origin + '/dashboard?country=' + encodeURIComponent(country); const url = window.location.origin + '/dashboard?country=' + encodeURIComponent(country); window.open(url, '_blank'); } ")), # Use a tabset if you want the dashboard within the same app (optional) tabsetPanel(id = "mainTabs", tabPanel("Map", leafletOutput("mymap")), tabPanel("Dashboard", uiOutput("dashboardContent")) ) ) server <- function(input, output, session) { output$mymap <- renderLeaflet({ leaflet(options = leafletOptions(maxZoom = 18)) %>% addTiles() %>% addPolygons( data = world.borders, fill = "#D24618", color = "#D24618", popup = paste0( "<b>Country: </b>", world.borders$NAME, "<br>", # Link that triggers our custom JS function with the country name "<a href='#' onclick='openDashboard(\"", world.borders$NAME, "\")' target='_blank'>Go to dedicated dashboard</a>" ), label = ~NAME ) }) # Render dynamic dashboard content based on the selected country output$dashboardContent <- renderUI({ # Extract the country parameter from the URL selected_country <- parseQueryString(session$clientData$url_search)$country if(is.null(selected_country)) { p("Select a country from the map to view its dedicated dashboard.") } else { tagList( h2(paste("Dashboard for", selected_country)), # Add your charts/data visualizations here (e.g., ggplot, plotly, etc.) p(paste("This is where you'd display charts, metrics, and insights specific to", selected_country)), # Example placeholder chart (replace with your actual content) plotOutput("countryChart") ) } }) # Example chart for the dashboard (customize this as needed) output$countryChart <- renderPlot({ selected_country <- parseQueryString(session$clientData$url_search)$country # Generate a simple plot based on the country plot(1:10, main = paste("Sample Chart for", selected_country)) }) } shinyApp(ui, server)
Key Details to Note:
- Link Construction: The popup link uses a custom JavaScript function (
openDashboard) to safely pass the country name as a URL parameter. This avoids issues with special characters in country names. - New Window Behavior: The
target='_blank'attribute ensures the dashboard opens in a new browser window, just as you requested. - Dynamic Dashboard Content: The
dashboardContentoutput loads content based on thecountryparameter from the URL. You can replace the placeholder text and plot with your actual charts, tables, or metrics. - Flexible URL Target: If your dashboard is a separate app or website, just update the
urlin the JS function to point to that external address with the country parameter.
内容的提问来源于stack exchange,提问作者JeanBertin
相关产品推荐
相关产品推荐

