Problem: I have a Shiny app in R including a leaflet OSM map that runs perfectly fine without warning message locally, but crashes online - and the log file output is of little help.
Question: How can I fix the issue that the Shiny app crashes online?
Here is the app code:
# 0 Preparations ####
library(shiny)
library(shinymanager)
#library(shinyjs)
library(leaflet)
library(SUNGEO)
library(RJSONIO)
#library(reticulate)
ui <- fluidPage(
# Here are the input variables:
column(6,
h4("Location"),
div(id="location",
textInput("place","Place",
value=NA,
placeholder="leave empty if NA"),
actionButton("showmap","Show on map", class = "btn-primary")
)
),
# Here comes the map:
column(6,
div(id="map",
leafletOutput("uk")))
)
server <- function(input, output, session) {
# What the map should show in the beginning:
output$uk <- renderLeaflet({
leaflet() %>%
addTiles() %>%
fitBounds(-6, 60, 0, 50)
})
# What the map should show once we click on "Show on map":
placeQuery <- eventReactive(input$showmap, {
geocode_osm(input$place, user_agent = "lea.kaftan@uni-wh.de")
}
)
observe({
if (!is.null(placeQuery()$longitude) & ! is.null(placeQuery()$latitude)) {
placeLng <- placeQuery()$longitude
placeLat <- placeQuery()$latitude
leafletProxy("uk") %>%
clearMarkers() %>%
addMarkers(lng=placeLng, lat=placeLat)
}
})
}
The log file using rsconnect::showLogs(appName="app-osm",streaming=TRUE)
reads as follows, the shinyapps.io log file reads very similar, but not showing the last line:
2023-05-16T14:09:32.633727+00:00 shinyapps[9119732]: Running on host: b7e245a1bc93
2023-05-16T14:09:32.636159+00:00 shinyapps[9119732]: Running as user: uid=10001(shiny) gid=10001(shiny) groups=10001(shiny)
2023-05-16T14:09:32.636185+00:00 shinyapps[9119732]: Connect version: 2023.03.0
2023-05-16T14:09:32.636190+00:00 shinyapps[9119732]: LANG: C.UTF-8
2023-05-16T14:09:32.636194+00:00 shinyapps[9119732]: Working directory: /srv/connect/apps/app-osm
2023-05-16T14:09:32.636361+00:00 shinyapps[9119732]: Using R 4.1.3
2023-05-16T14:09:32.636376+00:00 shinyapps[9119732]: R.home(): /opt/R/4.1.3/lib/R
2023-05-16T14:09:32.636648+00:00 shinyapps[9119732]: Content will use current R environment
2023-05-16T14:09:32.636656+00:00 shinyapps[9119732]: R_LIBS: (unset)
2023-05-16T14:09:32.636663+00:00 shinyapps[9119732]: .libPaths(): /opt/R/4.1.3/lib/R/library
2023-05-16T14:09:32.643960+00:00 shinyapps[9119732]: shiny version: 1.7.4
2023-05-16T14:09:32.643980+00:00 shinyapps[9119732]: httpuv version: 1.6.11
2023-05-16T14:09:32.643984+00:00 shinyapps[9119732]: rmarkdown version: 2.21
2023-05-16T14:09:32.643995+00:00 shinyapps[9119732]: knitr version: 1.42
2023-05-16T14:09:32.644005+00:00 shinyapps[9119732]: jsonlite version: 1.8.4
2023-05-16T14:09:32.644010+00:00 shinyapps[9119732]: RJSONIO version: 1.3.1.8
2023-05-16T14:09:32.644015+00:00 shinyapps[9119732]: htmltools version: 0.5.5
2023-05-16T14:09:32.644042+00:00 shinyapps[9119732]: reticulate version: (none)
2023-05-16T14:09:32.644250+00:00 shinyapps[9119732]: Using pandoc: /opt/connect/ext/pandoc/2.16
2023-05-16T14:09:33.021968+00:00 shinyapps[9119732]: Starting R with process ID: '93'
2023-05-16T14:09:33.022354+00:00 shinyapps[9119732]: Shiny application starting ...
2023-05-16T14:09:39.997112+00:00 shinyapps[9119732]: Listening on http://127.0.0.1:34751
2023-05-16T14:09:46.470476+00:00 shinyapps[9119732]: Warning: Error in : lexical error: invalid char in json text.
2023-05-16T14:09:46.470520+00:00 shinyapps[9119732]: <html> <head> <title>Access blo
2023-05-16T14:09:46.470526+00:00 shinyapps[9119732]: (right here) ------^
2023-05-16T14:09:46.483350+00:00 shinyapps[9119732]: 109: parse_string
2023-05-16T14:09:46.483386+00:00 shinyapps[9119732]: 108: parseJSON
2023-05-16T14:09:46.483394+00:00 shinyapps[9119732]: 107: parse_and_simplify
2023-05-16T14:09:46.483399+00:00 shinyapps[9119732]: 106: jsonlite::fromJSON
2023-05-16T14:09:46.483404+00:00 shinyapps[9119732]: 105: geocode_osm
2023-05-16T14:09:46.483409+00:00 shinyapps[9119732]: 104: eventReactiveValueFunc [/srv/connect/apps/app-osm/app.R#46]
2023-05-16T14:09:46.483427+00:00 shinyapps[9119732]: 60: townQuery
2023-05-16T14:09:46.483436+00:00 shinyapps[9119732]: 59: observe [/srv/connect/apps/app-osm/app.R#53]
2023-05-16T14:09:46.483440+00:00 shinyapps[9119732]: 58: <observer>
2023-05-16T14:09:46.483445+00:00 shinyapps[9119732]: 15: <Anonymous>
2023-05-16T14:09:46.483451+00:00 shinyapps[9119732]: 13: fn
2023-05-16T14:09:46.483454+00:00 shinyapps[9119732]: 8: retry
2023-05-16T14:09:46.483457+00:00 shinyapps[9119732]: 7: connect$retryingStartServer
2023-05-16T14:09:46.483460+00:00 shinyapps[9119732]: 6: eval
2023-05-16T14:09:46.483466+00:00 shinyapps[9119732]: 5: eval
2023-05-16T14:09:46.483470+00:00 shinyapps[9119732]: 4: eval
2023-05-16T14:09:46.483474+00:00 shinyapps[9119732]: 3: eval
2023-05-16T14:09:46.483476+00:00 shinyapps[9119732]: 2: eval.parent
2023-05-16T14:09:46.483480+00:00 shinyapps[9119732]: 1: local
The app is online here: https://leakaftan.shinyapps.io/app-osm/ (enter the name of a location and click on "Show on map"). [update: Since I found the solution (see below), the app runs smoothly now - at least it should.]
I have tried
Since there is no warning message when running the app locally from within RStudio, and the pin actually appears on the right map when clicking the button in the local version, I have no clue what could make this app crash online, but not offline.
Specify the user_agent when submitting a query through the shiny app:
placeQuery <- eventReactive(input$showmap, {
geocode_osm(input$place, user_agent = "name@provider.com")
}
The solution to this issue is related to the line Warning: Error in : lexical error: invalid char in json text
, indicating that geocode_osm
does not result in a json file, but in an html file, most likely stating that access has been denied.
See also a stackoverflow post concerning the json file message and the Nominatim Usage Policy.