Hi all,
I am relatively new to Shiny, and this my first attempt at getting help.
My code below seems to work but when I run the app, it only shows me the leaflet output, which is reactive as I wanted.
Although the code for the second output is really just a variant of the first output, no table is displayed and no error message is shown, so I really do not understand what is wrong.
Any help would be greatly appreciated.
Best wishes,
Bruno
library(shiny)
library(mapview)
library(leaflet)
library(RJSONIO)
library(rjson)
#library(DT)
library(geojsonio)
library(sf)
library(dplyr)#
library(RColorBrewer)
library(ggplot2)
library(data.table)#
library(DT)
library(stringr)
library(tidyverse)
R code that runs only once goes here e.g. import data, run calculations
SD = data.table::fread(unzip("dwca-seychecklist-v1.0.zip", "occurrence.txt"), header = TRUE, sep = "\t", dec = ".", encoding = "UTF-8")
#Filter data that can be mapped
SD <- SD[decimalLatitude < 0 & !is.na(decimalLatitude) & decimalLongitude > 0 & !is.na(decimalLongitude),]
#SD <- SD[1:1000,]
#Rename the lat long field for use with leaflet
SD <- dplyr::rename(SD, latitude = decimalLatitude, longitude = decimalLongitude)
#Filter the first 5000 records to make it easier for testing this, and pick needed columns
SD <- dplyr::select(SD, kingdom, scientificName, latitude, longitude, locality,
informationWithheld, coordinatePrecision, recordedBy, eventDate)
#Get a list of sspecies names as given in the input file
SDList <- sort(unique(SD$scientificName))
SD2 <- sf::st_as_sf(SD, coords = c("longitude", "latitude"), crs = 4326 )
ui <- fluidPage(
titlePanel("GEOJson example"),
sidebarLayout(
sidebarPanel(
fileInput(inputId = "filemap",
label = "Upload map. Choose GEOJson file",
multiple = FALSE,
accept = '.geojson',
mainPanel(
leafletOutput("mymap"),
DTOutput("mytable")#DT::dataTableOutput("mytable")
)
)
)
)
)
server = function(input, output, session) {
myshape <- reactive({if (!is.null(input$GEOJson)) {
df <- input$GEOJson
df1 <- df[1,]
directory <- dirname(df$datapath[1])
paste0(directory, "/", "0.geojson")
}
else {"aoi_civine.geojson"} })
observe({myshape()}) #to make sure my reactive function is not lazy
output$mymap <- renderLeaflet({
AOI <- geojson_read(myshape(), what = "sp")
clip <- st_as_sf(geojson_read(myshape(), what = "sp"), crs = 4326) #SDcrop <- st_intersection(SD2, clip) %>% as.data.table()
SDclip <- st_filter(SD2,clip)
mapviewOptions(basemaps = c("OpenStreetMap", "Esri.WorldTopoMap", "Esri.WorldImagery"))
map_mapview <- mapview(AOI) + mapview(SDclip)
map_mapview@map
})
#BUT WHY does it not show the tablea after the map?????
output$mytable <- renderDT({#DT::renderDataTable({
clip2 <- st_as_sf(geojson_read(myshape(), what = "sp"), crs = 4326)
SDtab <-st_filter(SD2,clip2) %>% as.data.table() #SDcrop <- st_intersection(SD2, clip) %>% as.data.table()
mytable <- DT::datatable(SDtab[,c('scientificName', 'informationWithheld', 'coordinatePrecision', 'recordedBy', 'eventDate')])
mytable
})
}
shinyApp(ui = ui, server = server)