【问题标题】:Leafpop Popupgraph doens't work with Plumber - RLeafpop Popupgraph 不适用于 Plumber - R
【发布时间】:2020-11-25 13:44:40
【问题描述】:

如果我在没有管道工功能的情况下运行我的代码,弹出图确实可以正常工作。但如果我使用管道工功能运行,我会收到错误 404 的 web。 知道如何解决这个问题吗? popupgraph 与水管工 api 一起工作?


library(dplyr)
library(ggplot2)
library(plotly)
library(leaflet)
library(htmltools)
library(leafpop)
library(leafem)
library(plumber)
library(htmlwidgets)

#* Return interactive plot 
#* @serializer htmlwidget
#* @get /map

function(){

  dataset <- data.frame(station = c('CARATORIA', 'CENTRO', 'FONTE GRANDE', 'ILHA DO PRINCIPE',
                                    'MOSCOSO', 'PIEDADE', 'SANTA CLARA', 'VILA RUBIM'),
                        lon = c(-40.35804, -40.34229, -40.33978, -40.35260, -40.34895,
                                -40.34157, -40.34717, -40.34976),
                        lat = c(-20.31472, -20.32030, -20.31325, -20.32188, -20.31468,
                                -20.31404, -20.31850, -20.31958),
                        value_no2 = c(23.93333, 123.06250, 40.00000, 10.93750, 46.38462,
                                      36.66667, 27.69231, 56.00000),
                        value_pm10 = c(10.238095, NaN, 13.842105, 9.318182, 17.842105,
                                       NaN, 9.000000, 15.333333))
                        
                         
   my_list <- list()  
   loop<-for (i in unique(dataset$station)) {
       name <- dataset %>% filter(station == i)
       plot <- ggplot(name, aes(x = value_no2, y = station)) + 
          geom_col()+labs(title = i)
       my_list[[i]] <- plot
    }
                        
                        
    my_list2 <- list()  
    loop<-for (i in unique(dataset$station)) {
       name <- dataset %>% filter(station == i)
        plot <- ggplot(name, aes(x = value_pm10, y = station)) + 
           geom_col()+labs(title = i)
        my_list2[[i]] <- plot
    }
                        
    list_comb <- list()
    list_comb[[1]] <- my_list
    list_comb[[2]] <- my_list2
                        
    test <- list()
    grafico_correto <- for (i in 1:max(length(my_list),length(my_list2))){
       test[[i]] <- subplot(do.call( rbind, list_comb)[,i], nrows = 2)
    }
          
    leaflet() %>%
       addTiles() %>%
       addCircleMarkers(data = dataset, ~lon,~lat, popup = popupGraph(test, type = "html"))
                        
}

没有管道工功能:

Without Plumber function: Popup does work

带管道工功能:

With Plumber function: Popup error 404

【问题讨论】:

    标签: r leaflet plumber


    【解决方案1】:

    管道工不知道如何路由/popup_graphs/&lt;tmp_file&gt;。这个特定的绘图使用 iframe 引用其他对象。我添加了一个端点来告诉管道工如何路由这些请求。

    编辑:我查看了popupGraph 的源代码,您可以将静态资产路径安装到预先计算的弹出窗口中,这样它就可以尽可能快。

    library(dplyr)
    library(ggplot2)
    library(plotly)
    library(leaflet)
    library(htmltools)
    library(leafpop)
    library(leafem)
    library(plumber)
    library(htmlwidgets)
    
    dataset <- data.frame(station = c('CARATORIA', 'CENTRO', 'FONTE GRANDE', 'ILHA DO PRINCIPE',
                                      'MOSCOSO', 'PIEDADE', 'SANTA CLARA', 'VILA RUBIM'),
                          lon = c(-40.35804, -40.34229, -40.33978, -40.35260, -40.34895,
                                  -40.34157, -40.34717, -40.34976),
                          lat = c(-20.31472, -20.32030, -20.31325, -20.32188, -20.31468,
                                  -20.31404, -20.31850, -20.31958),
                          value_no2 = c(23.93333, 123.06250, 40.00000, 10.93750, 46.38462,
                                        36.66667, 27.69231, 56.00000),
                          value_pm10 = c(10.238095, NaN, 13.842105, 9.318182, 17.842105,
                                         NaN, 9.000000, 15.333333))
    
    
    my_list <- list()
    loop<-for (i in unique(dataset$station)) {
      name <- dataset %>% filter(station == i)
      plot <- ggplot(name, aes(x = value_no2, y = station)) +
        geom_col()+labs(title = i)
      my_list[[i]] <- plot
    }
    
    
    my_list2 <- list()
    loop<-for (i in unique(dataset$station)) {
      name <- dataset %>% filter(station == i)
      plot <- ggplot(name, aes(x = value_pm10, y = station)) +
        geom_col()+labs(title = i)
      my_list2[[i]] <- plot
    }
    
    list_comb <- list()
    list_comb[[1]] <- my_list
    list_comb[[2]] <- my_list2
    
    test <- list()
    grafico_correto <- for (i in 1:max(length(my_list),length(my_list2))){
      test[[i]] <- subplot(do.call( rbind, list_comb)[,i], nrows = 2)
    }
    
    p <- leaflet() %>%
      addTiles() %>%
      addCircleMarkers(data = dataset, ~lon,~lat, popup = popupGraph(test, type = "html"))
    
    #* Return interactive plot
    #* @serializer htmlwidget
    #* @get /map
    function(){
      p
    }
    
    #* @plumber
    function(pr) {
      pr_static(pr, "/popup_graphs", file.path(tempdir(), "popup_graphs"))
    }
    

    【讨论】:

    • 尝试在路由处理程序之外尽可能多地预先计算,以便您的 api 具有更快的响应时间。
    • 它的工作。谢谢 :-) 并感谢预计算提示
    • 我更新了代码以反映预先计算的图形保存在file.path(tempdir(), "popup_graphs")
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-17
    • 2020-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多