Error when running a spatial model in Shiny app

SOLVED: It turns out the geojson functions a bit differently than the shapefile with regards to the spatial reference. Long story short, I had to use st_transform() instead of st_set_crs(). Thanks @nirgrahamuk for taking a look.

Forgive me for any bad posting practices, as this is my first post on Posit. I'm fairly new to Shiny but have a decent working knowledge of R.

I am working on a Shiny App to model and display Dendritic Connectivity Index (DCI) from the dci package. This is a package for ecological river analysis that takes 3 spatial files (in this case GeoJSON files to avoid the issues with ESRI shapefiles)- river network, locations of dams/culverts, and the outlet of the river network.

Outside of the app, I have no issues getting an output and displaying the results. The issue with the app comes after uploading the three spatial files and clicking "Calculate DCI."

I receive the following error:

Warning in join_attributes(net, user_nodes, tolerance) :
Nodes found too close together, removing duplicates.
Warning: Error in stopifnot: :information_source: In argument: node.label = tidygraph::map_bfs(...).
Caused by error in path$result[[length(path$result)]]:
! attempt to select less than one element in integerOneIndex

I've used browser() to narrow the issue down to the dci::river_net() function. Does anyone have an idea of what is going on here? Is there a better place to post this question? Thanks in advance for the help!

library(shiny)
library(devtools)
#> Loading required package: usethis
library(tidyverse)
library(sf)
#> Linking to GEOS 3.11.2, GDAL 3.6.2, PROJ 9.2.0; sf_use_s2() is TRUE
library(dci)

ui <- fluidPage(
  titlePanel("Segmental DCI Calculator"),
  
  sidebarLayout(
    sidebarPanel(
      fileInput("rivers_file", "Upload Rivers GeoJSON"),
      fileInput("outlet_file", "Upload Outlet GeoJSON"),
      fileInput("culverts_file", "Upload Culverts GeoJSON"),
      actionButton("calculate_dci", "Calculate DCI")
    ),
    mainPanel(
      tabsetPanel(
        tabPanel("Map", plotOutput("river_map")),
        tabPanel("Segmental DCI", tableOutput("segmental_dci_table"))
      )
    )
  )
)

server <- function(input, output) {
  
  observeEvent(input$calculate_dci, {
    req(input$rivers_file, input$outlet_file, input$culverts_file)
    
    rivers_st <- st_read(input$rivers_file$datapath)
    outlet_st <- st_read(input$outlet_file$datapath)
    culverts_st <- st_read(input$culverts_file$datapath)
    
    rivers <- import_rivers(rivers_st)
    outlet <- import_points(outlet_st, type = "outlet")
    culverts <- import_points(culverts_st, type = "barriers")
    
    crs <- 3338
    rivers_crs <- st_set_crs(rivers, crs)
    outlet_crs <- st_set_crs(outlet, crs)
    culverts_crs <- st_set_crs(culverts, crs)
    
    river_net <- dci::river_net(rivers_crs, culverts_crs, outlet_crs, poi = NULL, check = TRUE, tolerance = 50)
  })
}

shinyApp(ui, server)
Shiny applications not supported in static R Markdown documents

Created on 2023-08-11 with reprex v2.0.2

if your relace input$ etc. with file paths to your various shape files; does the same error get produced ?

  rivers_st <- st_read(input$rivers_file$datapath)
    outlet_st <- st_read(input$outlet_file$datapath)
    culverts_st <- st_read(input$culverts_file$datapath)
    
    rivers <- import_rivers(rivers_st)
    outlet <- import_points(outlet_st, type = "outlet")
    culverts <- import_points(culverts_st, type = "barriers")
    
    crs <- 3338
    rivers_crs <- st_set_crs(rivers, crs)
    outlet_crs <- st_set_crs(outlet, crs)
    culverts_crs <- st_set_crs(culverts, crs)
    
    river_net <- dci::river_net(rivers_crs, culverts_crs, outlet_crs, poi = NULL, check = TRUE, tolerance = 50)

?
You imply that it does not; which would be strange.

If it indeed works fine and with no errors as a plain script outside of shiny , we likely can't make progress in addressing your shiny issue without a reprex.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.