I wrote a shiny code that reads a shape file uploaded by the user and uses st_join to show them points from my csv that is within the area given by their shape file. The app is not working for one of the shape files uploaded by a user. I tried running the app locally with their shape file and it works fine, but it can't run in the deployed app. I think the issue could be in server where the app reads their shape file or in the creation of the AIMdata function.
Here is a condensed version of my code:
library(tidyr)
library(ggplot2)
library(dplyr)
library(shiny)
library(RODBC)
library(tidyverse)
library(rgdal)
library(units)
library(sp)
library(sf)
library(raster)
library(DBI)
library(rgeos)
library(rgdal)
library(plotly)
library(openxlsx)
library(writexl)
library(gridExtra)
library(utils)
# Define UI
ui <- fluidPage(
# Application title
titlePanel("DATA- Data Analysis Tool for AIM"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
helpText("Note: This app does not test for normality and therefore displays
a variety of summary statistics"),
fileInput(inputId = "file1",
label = "Upload all files within unzipped shapefile",
multiple = TRUE),
helpText("Drop down menus will appear once your shapefile is uploaded"),
uiOutput("selectcolumn"),
uiOutput("selectstratum"),
helpText("Click start after selecting stratum-only click once!"),
actionButton("gobutton","Start"),
helpText("It will take a minute to load after clicking start")
),
mainPanel(
tabsetPanel(
tabPanel("Surface Cover",verbatimTextOutput("surfacecoverplt"),verbatimTextOutput("sumsurfacecover"))
))
))
# server
server <- function(input, output) {
projbounddata<-reactive({
if(is.null(input$file1)){return()}
shpdf <- input$file1
tempdirname <- dirname(as.character(shpdf$datapath[1]))
for (i in 1:nrow(shpdf)) {
file.rename(
shpdf$datapath[i],
paste0(tempdirname, "/", shpdf$name[i])
)
}
projboundary <- readOGR(paste(tempdirname,
shpdf$name[grep(pattern = "*.shp$", shpdf$name)],
sep = "/"
))
})
output$selectcolumn<-renderUI({
projboundary1 <- req(projbounddata())
selectInput("Select","Select Column Containing Stratum of Interest",choices=unique(names(projboundary1)) )
})
output$selectstratum<-renderUI({
req(input$Select)
projboundary2 <- projbounddata()
projboundary3<-as.data.frame(projboundary2)
r <- projboundary3[,input$Select]
selectInput("name","Select Stratum of Interest",choices=unique(r))
})
AIMdata <- eventReactive(input$gobutton,{
if(is.null(input$name)){
return()
}
stboundary<-st_as_sf(projbounddata())%>% st_transform(26919) %>%st_set_crs(26919)
shape_sf <- st_transform(x=stboundary, crs="+proj=longlat +datum=NAD83 +no_defs")
AIMdatadb<-read.csv("./Data/data.csv")
coordinates(AIMdatadb)<-~Longitude + Latitude
AIMdatadf<-st_as_sf(AIMdatadb)
st_crs(AIMdatadf)<-"+proj=longlat +datum=NAD83 +no_defs"
newdf<-st_join(AIMdatadf,shape_sf)
Dataframe2 <- newdf[,c(input$Select,"PrimaryKey")]
df3<-na.omit(Dataframe2)
plots<-df3$PrimaryKey
AIMdata<-newdf %>% dplyr::filter(PrimaryKey %in% plots)
AIMdata2<-as.data.frame(AIMdata)
AIMdata2<-AIMdata2 %>%
dplyr::mutate_if(is.numeric, round, digits=2)
strataname<-as.character(input$name)
AIMdata2<- AIMdata2 %>% dplyr::filter(get(input$Select) %in% strataname)
AIMdata2
})
#Surface Cover data
output$surfacecoverplt<-renderPrint({
if(is.null(input$name)){return()}
nameN<-count(AIMdata(),PrimaryKey)
AIMdata()
})
output$sumsurfacecover<-renderPrint({
str(projbounddata())})
}
# Run the application
shinyApp(ui = ui, server = server)
Can an error be seen within my code that would cause an issue with the deployed app being able to read the shape file? I am also wondering if this error would be caused by something the user did to the shape file (used aliases, how they subset the data in gis, etc).
Also, the logs show the shape file being read. I checked str() of the shapefile and that seemed in order.
Thank you so much for the help!