0

我一般是使用 Shiny 和 R 编程的新手。我试图使用雄蕊图创建一个闪亮的应用程序,并将函数 get_stamenmap 与边界框和其他参数一起使用。我将此函数分配给 ggmap 函数中的变量“map”。

我收到一个错误:

监听http://127.0.0.1:5907 警告:ggmap 中的错误:找不到对象“地图”52:ggmap 49:服务器 [#2] ggmap(地图)中的错误:找不到对象“地图”

我使用的代码:

ui <- fluidPage(
  mainPanel("Siebar"),
  sidebarLayout(
    sidebarPanel("Options",
                 radioButtons(inputId = "radio",
                              label = "Type of Offense",
                              choices = list("Murder" = 'murder',
                                             "Robbery" = 'robbery',
                                             "Assault" = 'aggravated assault',
                                             "Burglary" = 'burglary',
                                             "Auto-Theft" = 'auto theft',
                                             "Theft" = 'theft',
                                             "Rape" = 'rape'),
                              selected = 'murder'),
                 
                 
                 ),
  mainPanel(
    plotOutput('plot')
  ),
)
)
server <- function(input, output){
  output$plot <- renderPlot(
    map <- get_stamenmap(bbox = c(left=-95.8, bottom=29.4, right=-95.0, top=30.0), 
                         zoom = 10, source = "stamen", maptype = "terrain"),
    ggmap(map) + stat_density_2d(data = subset(crime, offense == input$radio)),
    aes(x = lon, y = lat, fill = ..level.., alpha = ..level..), geom = 'polygon') +
    ggtitle("Crimes in Houston, TX")
  
}
4

1 回答 1

0

很可能您缺少/放错了括号。尝试这个

server <- function(input, output){
  output$plot <- renderPlot({
    map <- get_stamenmap(bbox = c(left=-95.8, bottom=29.4, right=-95.0, top=30.0), 
                         zoom = 10, source = "stamen", maptype = "terrain")
    
    ggmap(map) + 
      stat_density_2d(data = subset(crime, offense == input$radio),
                      aes(x = lon, y = lat, fill = ..level.., alpha = ..level..), geom = 'polygon') +
      ggtitle("Crimes in Houston, TX")
  })
}

输出

于 2021-09-03T12:40:53.463 回答