You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Shiny App中使用googleway绘制途经点路线:空白途经点异常问题

Fixing Empty Waypoint Issues in googleway Shiny Apps

Hey there! I’ve run into this exact issue with googleway in Shiny before—those empty waypoint inputs can throw a real wrench in your route rendering. Let’s get your app to handle partial (or fully empty) waypoint entries smoothly:

Step 1: Clean Up Empty Waypoint Inputs First

The core problem is that google_directions() can’t process empty strings or whitespace-only entries as waypoints. So before passing your inputs to the function, we’ll filter out any invalid entries.

Suppose your Shiny app has two waypoint inputs named waypoint1 and waypoint2. Here’s how to clean them:

# Gather all waypoint inputs into a single vector
all_waypoints <- c(input$waypoint1, input$waypoint2)

# Filter out empty strings and entries that are just spaces
clean_waypoints <- all_waypoints[all_waypoints != "" & !grepl("^\\s*$", all_waypoints)]

This ensures only valid, non-empty addresses make it to the directions API.

Step 2: Update Your google_directions Call

Now pass the cleaned waypoints to google_directions(). If all waypoints were empty, this vector will be length 0, and the API will just return the direct route between origin and destination—perfect!

# Fetch directions with cleaned waypoints
route <- google_directions(
  origin = input$origin,
  destination = input$destination,
  waypoints = clean_waypoints,
  key = Sys.getenv("GOOGLE_MAPS_API_KEY"), # Store your key safely in environment vars!
  mode = "driving" # Adjust to walking/bicycling/transit as needed
)

Step 3: Add Shiny Validation for Edge Cases

To make your app more user-friendly, add validation to ensure the origin and destination are always filled (since those are mandatory for any route):

output$route_map <- renderGoogle_map({
  # Validate required inputs first
  validate(
    need(input$origin != "", "Please enter an origin address!"),
    need(input$destination != "", "Please enter a destination address!")
  )
  
  # Reuse the waypoint cleaning code here
  all_waypoints <- c(input$waypoint1, input$waypoint2)
  clean_waypoints <- all_waypoints[all_waypoints != "" & !grepl("^\\s*$", all_waypoints)]
  
  # Fetch and render the route
  route <- google_directions(
    origin = input$origin,
    destination = input$destination,
    waypoints = clean_waypoints,
    key = Sys.getenv("GOOGLE_MAPS_API_KEY"),
    mode = "driving"
  )
  
  google_map() %>%
    add_directions(directions = route)
})

How This Works

  • If both waypoints are filled: You get the optimal route through both stops, just like before.
  • If one waypoint is empty: The API uses only the filled waypoint to calculate the route.
  • If both waypoints are empty: You get the direct route between origin and destination, no errors.

This approach keeps your app robust and user-friendly, handling all combinations of filled/empty waypoints without breaking.

内容的提问来源于stack exchange,提问作者user9534023

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:05:04