Setting working directory for Shiny

rstudio

#1

When I try to launch my shiny io app, I get the following error:

Error in value[3L] : cannot change working directory
Calls: local … tryCatch -> tryCatchList -> tryCatchOne ->
Execution halted

I’m not sure if it’s because my data has an absolute path.

My code is below:

Data - housingvan R


#Set Working Directory
getwd()
setwd("/Users/soraiyasalemohamed/Documents/UCL/HousingVan/")


#2016 - 

#Read CT Shapefile for the whole of Canada
CensusTract.shape2016 <- readOGR(dsn="/Users/soraiyasalemohamed/Documents/UCL/HousingVan/data/lct_000b16a_e/", layer = "lct_000b16a_e")

#Grab just Vancouver's CTs
CensusTractVan16 <- CensusTract.shape2016[CensusTract.shape2016$CMANAME == "Vancouver",]

#Project shapefile into WGS
CensusTractVan16@proj4string
CensusTractVan16wgs <- spTransform(CensusTractVan16,"+proj=longlat +datum=WGS84 +no_defs")
CTVan16 <- CensusTractVan16wgs
CTVan16@proj4string

#Clean attribute data
CensusTractVan16df <-read.table("/Users/soraiyasalemohamed/Documents/UCL/HousingVan/data/CensusTractVan2016df0.csv", sep=",", dec = ".", stringsAsFactors = FALSE)
CensusTractVan16dfsemiclean <- CensusTractVan16df[,-c(1:9)]
names(CensusTractVan16dfsemiclean) <- c("geo_uid",	"ct_name",	"Average Rent",	"Average Dwelling Cost",	"Median Total Income in Private Households",	"Average Income per Month","Percentage of Income Spent on Rent",	"Years Needed to Save",	"# of Avocados Needed to Pay Rent")
CensusTractVan16dfclean<- CensusTractVan16dfsemiclean[-c(1),] 
CensusTractVan16dfclean[,c(3:9)] <- lapply(CensusTractVan16dfclean[,c(3:9)], as.numeric)



#join attribute data to boundaries
CTVan16@data <- data.frame(CTVan16@data, CensusTractVan16dfclean[match(CTVan16@data[,"CTUID"], CensusTractVan16dfclean[,"geo_uid"]),])
class(CTVan16@data$pct_income_rent_2016)
names(CTVan16@data)
summary(CTVan16@data)

#Set data from factor to numeric data
CTVan16@data[,c(11:17)] <- lapply(CTVan16@data[,c(11:17)], as.numeric)
names(CTVan16@data)

App R

library(rsconnect)
deployApp(appName = "myapp")


ui <-tabsetPanel(
    tabPanel("2016",fluidPage(theme = "bootstrap.css",
  
 h1("2016 Housing Landscape",  align = "center"),
 
leafletOutput("map1", height = "600px", width = "100%"),
h3("Here I've mapped out blah blah blah"),
  #gimme a title
  absolutePanel(id = "controls", class = "panel panel-default", fixed = TRUE,
                draggable = TRUE, top = 60, left = 20, right = 20, bottom = "auto",
                width = 330, height = "auto", style='padding:15px',
                #gimme a drop down to select variables from the shapefile 
                #boundaries dataset
                selectInput("variable", "Variable",
                            names(CTVan16@data)[11:17]),
                #gimme some colour options from colourbrewer
                selectInput("colourbrewerpalette", "Color Scheme",
                            rownames(subset(brewer.pal.info, category %in% c("seq", "div")))
                ),
                #selectInput("colourbrewerpalette", "Color Scheme",
                           # rownames(subset(brewer.pal.info, category %in% c("seq", "div")))
                #),
                selectInput("classIntStyle", "Interval Style",
                            c("Jenks Natural Breaks" = "jenks",
                              "Quantile" = "quantile",
                              "Equal Interval" = "equal",
                              "Pretty" = "pretty")),
                radioButtons('format', 'Document format', c('PDF', 'HTML', 'Word'),
                             inline = TRUE),
                downloadButton('downloadReport')
                
                
            
          
      )
    )
  )







server <- function(input, output, session){
  
  #2016  
  output$map1 <- renderLeaflet({
    leaflet(CTVan16) %>% addProviderTiles("CartoDB.Positron") %>%
      setView(-123.116226, 49.246292, zoom = 10)
  })
  
  #observer for the map elements to redraw
  observe({
    breaks <- classIntervals(CTVan16[[input$variable]], n=5, style=input$classIntStyle)
    breaks <- unique(breaks$brks)
    
    pal <- colorBin(palette = input$colourbrewerpalette, 
                    domain = CTVan16@data[[input$variable]]
                    #create bins using the breaks object from earlier
                    
    )
    leafletProxy("map1", data = CTVan16) %>%
      clearShapes() %>% 
      addPolygons(stroke = F, 
                  fillOpacity = 0.5,
                  smoothFactor = 0.5,
                  opacity = 1,
                  color = ~pal(CTVan16@data[[input$variable]]),
                  fillColor = ~pal(CTVan16@data[[input$variable]]),
                  popup = paste("CTUID",CTVan16@data$CTUID, "<br>", "Variable Value:",
                                CTVan16@data[[input$variable]]),
                  popupOptions = popupOptions(maxWidth ="100%", closeOnClick = TRUE)
                  
                  
      )
  })
  
  
  #observer for the legend to redraw
  observe({
    breaks <- classIntervals(CTVan16@data[[input$variable]], n=5, style=input$classIntStyle)
    breaks <- unique(breaks$brks)
    
    pal <- colorBin(palette = input$colourbrewerpalette, 
                    domain = CTVan16@data[[input$variable]],
                    #create bins using the breaks object from earlier
                    bins = breaks
    )
    
    proxy <- leafletProxy("map1", data = CTVan16)
    proxy %>% clearControls() %>%
      addLegend("bottomright", 
                pal= pal, 
                values = ~CTVan16@data[[input$variable]], 
                title = input$variable, 
                labFormat = labelFormat(prefix = ""),
                opacity = 1
      )
  })
  
  #Download PDF
  output$downloadReport <- downloadHandler(
    filename = function() {
      paste('my-report', sep = '.', switch(
        input$format, PDF = 'pdf', HTML = 'html', Word = 'docx'
      ))
    },
    
    content = function(file) {
      src <- normalizePath('report.Rmd')
      
      # temporarily switch to the temp dir, in case you do not have write
      # permission to the current working directory
      owd <- setwd(tempdir())
      on.exit(setwd(owd))
      file.copy(src, 'report.Rmd', overwrite = TRUE)
      
      library(rmarkdown)
      out <- render('report.Rmd', switch(
        input$format,
        PDF = pdf_document(), HTML = html_document(), Word = word_document()
      ))
      file.rename(out, file)
    }
  )
  
   
  

}




# Run the application 
shinyApp(ui = ui, server = server)```

Sorry for the endless lines of code. Thanks in advance :slight_smile:

#2

Did you get any response from the community? If not how did you solve the problem, a short update & closure will be helpful to others. thanks.