微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

发生错误时,可以安全地重新启动r闪亮的无功函数

如何解决发生错误时,可以安全地重新启动r闪亮的无功函数

我正在尝试获取反应式功能,以仅在JSON文件为NULL时给出消息,否则请完成反应式功能。 API随时返回null JSON文件的原因是应用必须重新启动/重新加载。从逻辑上捕获该错误会很好(例如if (is.null(JSON)) {return("retry")} else {finish reactive}。我已经看过其他选项,例如req(),validate()等,它们对于input $ value很有效,但不适用于交互式API。)例如,在我的情况下,用户单击地图上的一个位置并生成坐标。从那里开始,这些点将在API中用于获取数据。但是,有时JSON为null。我只想说“嘿,再次单击“。我会提供一个reprex,只需在美国单击即可,但会提示您大约需要一分钟的时间。

library(shinydashboard)
library(leaflet)
library(geojsonsf)
library(streamstats)
library(jsonlite)
library(AOI)
library(tidyverse)
library(shiny)

header = dashboardHeader(title = "Hydroclimatic Data")


# * sidebar ----
sidebar = dashboardSidebar(
  sidebarMenu(
    menuItem(
      "Map",tabName = "map",menuSubItem("Map",icon = icon("chart-line"))
    )))



body = dashboardBody(
  tabItems(
    tabItem(
      tabName = "map",tags$style(type = 'text/css','
      #swe_maps {
  cursor: crosshair !important;}'),fluidRow(
        tabBox(width = 12,id = "tab",tabPanel("Map",style = "height:92vh;",leafletoutput("swe_maps"))))
    )))
    
    

# UI ----------------------------------------------------------------------

ui <- dashboardPage(header = header,sidebar = sidebar,body = body)

# Server ------------------------------------------------------------------


server <- function(input,output,session) {
  
  output$swe_maps <- renderLeaflet({
    
    
    leaflet() %>% addTiles() %>% setView(lat = 48.91167,lng = -114.90246,zoom = 4)
    
    
    
    
  })
  
  observeEvent(input$swe_maps_click,{
    
    click <- input$swe_maps_click
    clat <- click$lat
    clng <- click$lng
    content <- paste(
      "<b>Lat: ",round(clat,5),"</b>","<br>","<b>Long: ",round(clng,"</b>"    )
    
    leaflet() %>% addTiles() %>% addCircles(lng=clng,lat=clat) %>%
      addPopups(lng = clng,lat = clat,content)
  
    ss_stats <- reactive({
      
      state <-   geocode_rev(c(clat,clng)) %>% dplyr::select(state)
      state <-  state.abb[grep(paste(state$state),state.name)]
      
      df1 <- streamstats::delineatewatershed(clng,clat,rcode = state,crs = 4326) #will through error if not in US
      
      df_poly <- df1 %>%
        streamstats::writeGeoJSON(.,file.path(tempdir(),"ss_tmp.json"))
      
      
      df_poly <- df_poly %>% geojsonsf::geojson_sf() %>% #this is where the code sometimes gets a NULL
        sf::st_as_sf() %>% dplyr::mutate(ID = state,long = clng,lat = clat)
      
      wkID <- df1$workspaceID
      
      incProgress(detail = paste("Computing Basin characteristics"))
      
      stats <- streamstats::computeChars(workspaceID = wkID,rcode = state)
      
      
      
      stats <- stats$parameters %>% mutate(ID = state,workspaceID = wkID)
      
      flow_stats <- stats %>% dplyr::select(ID,code,value) %>%
        pivot_wider(names_from = "code")
      
      df_poly <- df_poly %>% dplyr::select(Shape_Area,ID,lat,long) %>% left_join(flow_stats,by = "ID")

      list(stats = stats,df_poly = df_poly,state = state,wkID = wkID)
    })
    map_leaf <- reactive({
      
      leaflet() %>% addTiles() %>% addCircleMarkers(data = ss_stats()$df_poly,lng = ss_stats()$df_poly$long,lat = ss_stats()$df_poly$lat,layerId = ~paste("Drain Point"),color = "red") %>% 
        addpolygons(data = ss_stats()$df_poly)})
    
    output$swe_maps <- renderLeaflet({
      
      map_leaf()
      
    })
    
})

}

##### RUN APPLICATION #####
shinyApp(ui = ui,server = server)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。