0

I am trying to deploy a very simple app to Shinyapps.io.

My app is the following, and the structure of the folder is mines_peru/app.R (the file below) and mines_peru/data/clean_active_mines.RDS.

library(shiny)    # for shiny apps
library(leaflet)  # renderLeaflet function

library(sf)
library(dplyr)
library(tmap)
library(maptools)
library(ggplot2)
library(reshape2)
library(rstudioapi)

setwd(dirname(rstudioapi::getActiveDocumentContext()$path))

data_m <- readRDS('data/clean_active_mines.RDS')
data_m <- data_m[!is.na(data_m$LongitudeD),]
ui = fillPage(
  sliderInput(inputId = "yeari", "Year", 1995, 2020, value = 1995),
  leafletOutput(outputId = "map", width = "100%", height = "100%")

)
server = function(input, output) {
  output$map = renderLeaflet({tmap_leaflet(tm_shape(data_m[data_m$year == input$yeari,] %>% st_as_sf()) + tm_dots(), in.shiny = TRUE) %>% setView(-76, -10, 6)})
}

shinyApp(ui, server)

I am not allowed to share the true data, so the following snippet may be enough to generate a similar dataset.

library(sf)
set.seed(1)
lat <- sample(-10:-40, 200, replace  = T)
lon <- sample(-60:-90, 200, replace = T)
year <- sample(1995:2020, 200, replace = T)

df <- data.frame(lat,lon,year)
df_sf <- st_as_sf(df, coords = c('lon','lat'))

saveRDS(df_sf, "mines_peru/data/clean_active_mines.RDS')

My deployment code is the following:

library(rsconnect)
rsconnect::setAccountInfo(name='******',
                          token='******',
                          secret='*******')
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))

rsconnect::deployApp('mines_peru/')

My guess is that I made some mistake on file indexing. The line using the dirname definition within the app.R is probably wrong. I could not find enough information on how to fix it up, though. The errors I am getting are

✔ Re-deploying "mines_peru" using "server: shinyapps.io / username: *******"
ℹ Looking up application with id "12211070"...
✔ Found application <https://******.shinyapps.io/mines_peru/>
ℹ Bundling 2 files: app.R and data/clean_active_mines.RDS
ℹ Capturing R dependencies with renv
Error in read.dcf(file = tmpf) : cannot open the connection

so I understood the deployment is able to find and pack the files.

As a second check, I remove the setwd(dirname(rstudioapi::getActiveDocumentContext()$path)) from the app.R code. I still get the same error.

2
  • What if you take out the setwd(dirname(rstudioapi::getActiveDocumentContext()$path)) part in your app.R code. That seems unlikely to run successfully on the server.
    – MrFlick
    Commented Jun 20 at 15:18
  • Even if I remove it, nothing changes regarding the output.
    – catatau
    Commented Jun 20 at 15:28

1 Answer 1

1

Your code works for me. I created the data and app.R as you did and ran:

rsconnect::setAccountInfo(name='****', token='****', secret='****')
rsconnect::deployApp(appDir = "C:/Users/Victor/Desktop/test/", appName="Test_SOF")

It works fine except that I could not load package maptools (deprecated) so the app loads online but does not work. So my two suggestions would be: try specifying an absolute path for appDir and try removing maptools library. If this is not the issue, I'd say it's something related to your shinyapps.io account; do you have other apps running there?

1
  • I believe that the problem was related to the maptools app and the the path. I fixed both, and now have just been able to deploy.
    – catatau
    Commented Jun 25 at 14:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.