Allow User to Set Path to Shapefile Files

ok, I adjusted your code to make a working example.
I borrowed ideas from Suggestions for fileInput to work on Shiny ??
I find its easier to require a zip of the relates shp files so user loads a single zip, than multi files.
So try this by loading a zip of the shapefiles

library(shiny)
library(sf)
library(shinydashboardPlus)
library(shinydashboard)
library(leaflet)
library(DT)
library(raster)
ui <- fluidPage(
  dashboardPagePlus(
    dashboardHeaderPlus(
      title = "Inspection Analysis", titleWidth = "250px",
      tags$li(a(
        href = "http://www.pipeline-risk.com", img(
          src = "Logo.png",
          title = "Pipeline-Risk Web Site", height = "30px"
        ),
        style = "padding-top:10px; padding-bottom:10px;"
      ),
      class = "dropdown"
      )
    ),
    footer = dashboardFooter(left_text = h6("Copyright Pipeline-Risk 2020")),
    dashboardSidebar(
      width = "250px",
      sidebarMenu(
        menuItem("Anomaly Analysis", tabName = "INSP", icon = icon("dashboard")),
        menuItem("Administration", tabName = "ADMIN", icon = icon("dashboard"))
      )
    ),
    dashboardBody(
      tabItems(
        tabItem(
          "INSP",
          fluidRow(
            tabBox(
              id = "INSP_1", width = 12,
              tabPanel(
                "Results",
                fluidRow(
                  box(
                    width = 12, collapsible = TRUE,
                    column(
                      width = 4,
                      fileInput("file_1", "Select Results Data Set to View")
                    )
                  )
                ),
                fluidRow(
                  box(
                    width = 12, collapsible = TRUE, collapsed = FALSE,
                    column(
                      width = 12,
                      dataTableOutput("T_1b")
                    )
                  )
                ),
                fluidRow(
                  box(
                    width = 12, collapsible = TRUE, collapsed = FALSE,
                    column(
                      width = 12,
                      leafletOutput("M_1", height = "800px")
                    )
                  )
                )
              )
            )
          )
        ),
        tabItem(
          tabName = "ADMIN",
          fluidRow(
            tabBox(
              id = " admin_1", width = 12,
              tabPanel(
                "Project Set-Up",
                fluidRow(
                  box(
                    width = 12, collapsible = T,
                    column(
                      width = 5,
                      fileInput("admin_sf", "Load Shapefiles", multiple = T)
                    )
                  )
                )
              )
            )
          )
        )
      )
    )
  )
)

server <- function(input, output, session) {

  # the results set in a table which links to the map M_1
  results <- eventReactive(
    input$file_1$datapath,
    {
      if (is.null(input$file_1$datapath)) {
        return()
      } else {
        results <- read.table(input$file_1$datapath, header = TRUE, sep = ",")
      }
    }
  )

  output$T_1b <- renderDataTable({
    datatable(results(),
      extensions = "FixedHeader", selection = "single", filter = "top", rownames = F,
      options = list(fixedHeader = TRUE, scrollX = T, lengthMenu = c(10, 100, 200), autoWidth = TRUE)
    ) %>%
      formatStyle(names(results()), lineHeight = "70%")
  })


  shp <- reactive({
    req(input$admin_sf)
    mytempdir <- tempdir()
    unzip(input$admin_sf$datapath, exdir = mytempdir)
    shape_path <- dir(path = mytempdir, pattern = ".shp$")
    shape_path_full <- file.path(mytempdir, shape_path)
    print("shape_path")
    print(shape_path)
    print("shape_path_full")
    print(shape_path_full)

    shape_path_full
  })

  sf <- eventReactive(input$T_1b_rows_selected, {
    shapefile(shp()) %>% sf::st_as_sf()
  })

  # Leaflet Map
  observeEvent(
    input$T_1b_rows_selected,

    output$M_1 <- renderLeaflet({
      sf <- sf::st_transform(sf(), 4326)
      results <- results()[input$T_1b_rows_selected, c("Long", "Lat")]

      map <- leaflet() %>%
        addTiles(group = "Open Street") %>%
        addProviderTiles(providers$Esri.WorldImagery, group = "World Imagery", options = providerTileOptions(opacity = .7)) %>%
        addPolylines(data = sf, group = "Pipeline", color = "blue", opacity = 1, label = sf$Route) %>%
        addMarkers(results, lng = results$Long, lat = results$Lat, group = "Results", label = "test") %>%
        setView(lng = results$Long, lat = results$Lat, zoom = 20) %>%
        addMeasure() %>%
        addLayersControl(
          baseGroups = c("Open Street", "World Imagery"),
          overlayGroups = c("Pipeline", "Results"),
          options = layersControlOptions(collapsed = FALSE)
        ) %>%
        addMiniMap(
          toggleDisplay = TRUE,
          tiles = providers$Stamen.TonerLite
        )
      map
    })
  )
}

shinyApp(ui = ui, server = server)
1 Like