renderDT not showing the result table in DTOutput although the code seems ok

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)

Hello.
Thanks for providing code , but you could take further steps to make it more convenient for other forum users to help you.

Share some representative data that will enable your code to run and show the problematic behaviour.

You might use tools such as the library datapasta, or the base function dput() to share a portion of data in code form, i.e. that can be copied from forum and pasted to R session.

Reprex Guide

Hi,
I see that I cannot attach the input files here, and I would have to learn datapasta, so in the meantime I have put a copy of the input file in a WeTransfer link: WeTransfer - Send Large Files & Share Photos Online - Up to 2GB Free

I modified a bit the code below to reflect the names of these input files.
I hope that this is enough to get some help,
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")
SD = data.table::fread("occurrence_extract.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 {"Praslin.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)

This topic was automatically closed 54 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.