From ad96d721cd977736f696a9196a1c429e0ccba0dd Mon Sep 17 00:00:00 2001 From: Chris <53177842+ChristopherMarais@users.noreply.github.com> Date: Wed, 21 Feb 2024 14:06:43 -0500 Subject: [PATCH 1/9] Change working directories to find files --- 05_DASHBOARD/app.R | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/05_DASHBOARD/app.R b/05_DASHBOARD/app.R index a8c190a..225ea2a 100644 --- a/05_DASHBOARD/app.R +++ b/05_DASHBOARD/app.R @@ -18,21 +18,24 @@ if(!require(sf)){ install.packages("sf") } ; library(sf) # I am working in an Rproject) # NOTE: .RData restores the object to the name it had when you saved it as .RData -#load("03_Data_for_app/HAB.RData") +#load("03_Data_for_app/HAB.RData") + +setwd("..") +print(getwd()) #### HAB data ------------------------------------------------ -load("~/github/GTMNERR Science Transfer/App_dev/03_Data_for_app/HAB.RData") +load(paste0(getwd(), "/03_Data_for_app/HAB.RData")) HAB_data_locations <- HAB %>% select(Latitude, Longitude, Site, `Collection Agency`, County) %>% # took out `HAB ID` (otherwise 1172 instead of 17 locations) distinct() %>% st_as_sf(coords = c("Longitude", "Latitude"), crs = 4326) #### GTMNERR shapefile ------------------------------------------------ -GTMNERR <- st_read("~/github/GTMNERR Science Transfer/App_dev/03_Data_for_app/shapefiles_new/GTMNERR.shp") +GTMNERR <- st_read(paste0(getwd(), "/03_Data_for_app/shapefiles_new/GTMNERR.shp")) GTMNERR <- st_transform(GTMNERR, crs = 4326) #### county shapefiles ------------------------------------------------ -counties_select <- st_read("~/github/GTMNERR Science Transfer/App_dev/03_Data_for_app/shapefiles_new/counties_GTMNERR.shp") +counties_select <- st_read(paste0(getwd(), "/03_Data_for_app/shapefiles_new/counties_GTMNERR.shp")) counties_select <- st_transform(counties_select, crs = 4326) From cd2a90ddc3a0bc5a6dea05ae0ca9f0525bbc32e6 Mon Sep 17 00:00:00 2001 From: Chris <53177842+ChristopherMarais@users.noreply.github.com> Date: Sat, 24 Feb 2024 17:48:08 -0500 Subject: [PATCH 2/9] Test without getwd --- 05_DASHBOARD/app.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/05_DASHBOARD/app.R b/05_DASHBOARD/app.R index 225ea2a..e32941f 100644 --- a/05_DASHBOARD/app.R +++ b/05_DASHBOARD/app.R @@ -24,18 +24,18 @@ setwd("..") print(getwd()) #### HAB data ------------------------------------------------ -load(paste0(getwd(), "/03_Data_for_app/HAB.RData")) +load("./03_Data_for_app/HAB.RData") HAB_data_locations <- HAB %>% select(Latitude, Longitude, Site, `Collection Agency`, County) %>% # took out `HAB ID` (otherwise 1172 instead of 17 locations) distinct() %>% st_as_sf(coords = c("Longitude", "Latitude"), crs = 4326) #### GTMNERR shapefile ------------------------------------------------ -GTMNERR <- st_read(paste0(getwd(), "/03_Data_for_app/shapefiles_new/GTMNERR.shp")) +GTMNERR <- st_read("./03_Data_for_app/shapefiles_new/GTMNERR.shp") GTMNERR <- st_transform(GTMNERR, crs = 4326) #### county shapefiles ------------------------------------------------ -counties_select <- st_read(paste0(getwd(), "/03_Data_for_app/shapefiles_new/counties_GTMNERR.shp")) +counties_select <- st_read("./03_Data_for_app/shapefiles_new/counties_GTMNERR.shp") counties_select <- st_transform(counties_select, crs = 4326) From 2aff089e82bafd487513b0133e83d206559253d8 Mon Sep 17 00:00:00 2001 From: Chris <53177842+ChristopherMarais@users.noreply.github.com> Date: Sat, 24 Feb 2024 19:02:08 -0500 Subject: [PATCH 3/9] made dynamic working directory for app.R script --- 05_DASHBOARD/app.R | 47 +++++++++++++++++++++++++++++++++++++++++-- 05_DASHBOARD/deploy.R | 5 +++-- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/05_DASHBOARD/app.R b/05_DASHBOARD/app.R index e32941f..424750b 100644 --- a/05_DASHBOARD/app.R +++ b/05_DASHBOARD/app.R @@ -12,6 +12,7 @@ if(!require(tidyverse)){ install.packages("tidyverse") } ; library(tidyverse) if(!require(bslib)){ install.packages("bslib") } ; library(bslib) if(!require(leaflet)){ install.packages("leaflet") } ; library(leaflet) if(!require(sf)){ install.packages("sf") } ; library(sf) +if(!require(fs)){ install.packages("fs") } ; library(fs) ### READ IN DATA --------------------------------------------- # For some reason I need to specify my whole path to the data here (even though @@ -20,8 +21,50 @@ if(!require(sf)){ install.packages("sf") } ; library(sf) # NOTE: .RData restores the object to the name it had when you saved it as .RData #load("03_Data_for_app/HAB.RData") -setwd("..") -print(getwd()) +#### Get app.R file dir and set work dir --------------------- +# Function to find the directory of a file named app.R +find_directory_of_file <- function(file_name, start_dir=getwd()) { + # Recursively list all files starting from the start_dir + app_dir <- fs::dir_ls(start_dir, recurse = TRUE, glob=file_name) + + # Check if any file named app.R is found + if (length(app_dir) > 0) { + # Assuming you want the directory of the first matching file + file_dir <- fs::path_dir(app_dir[1]) + return(file_dir) + } else { + return(NULL) # Return NULL if the file is not found + } +} + +# find file_name from current working directory +# before trying from a shallower directory +file_name <- "*05_DASHBOARD/app.R" # The file you are searching for + +try({ + found_dir <- find_directory_of_file(file_name) + # Check if found_dir is NULL or empty, indicating the file was not found + if (is.null(found_dir) || length(found_dir) == 0) { + # print error + print("05_DASHBOARD/app.R not found from current working directory!") + print("trying again from shallower directory") + # trying again from great grandparent directory of working directory + setwd("../../..") + found_dir <- find_directory_of_file(file_name) + if (is.null(found_dir) || length(found_dir) == 0) { + print("05_DASHBOARD/app.R likely does not exist in filesystem!") + } + # Set working directory to parent directory of found dir + setwd(fs::path_dir(found_dir[1])) + # Print working directory + print(paste0("Working dir: ", getwd())) + } + # Set working directory to parent directory of found dir + setwd(fs::path_dir(found_dir[1])) + # Print working directory + print(paste0("Working dir: ", getwd())) +}, silent = FALSE) # Setting silent = FALSE will print the error message to the console + #### HAB data ------------------------------------------------ load("./03_Data_for_app/HAB.RData") diff --git a/05_DASHBOARD/deploy.R b/05_DASHBOARD/deploy.R index 580835f..1cbe136 100644 --- a/05_DASHBOARD/deploy.R +++ b/05_DASHBOARD/deploy.R @@ -9,6 +9,7 @@ rsconnect::setAccountInfo( # Deploy rsconnect::deployApp( - + appDir = "./05_DASHBOARD", appFiles = "app.R", - appName = "Dashboard") + appName = "Dashboard", + forceUpdate = TRUE) From ae6f261e0862887af14a3473152b528f9a5e0ab6 Mon Sep 17 00:00:00 2001 From: Chris <53177842+ChristopherMarais@users.noreply.github.com> Date: Sat, 24 Feb 2024 19:22:49 -0500 Subject: [PATCH 4/9] add files to be submitted to shinyapps.io --- 05_DASHBOARD/app.R | 2 +- 05_DASHBOARD/deploy-dev.R | 24 +++++++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/05_DASHBOARD/app.R b/05_DASHBOARD/app.R index 424750b..8d71a29 100644 --- a/05_DASHBOARD/app.R +++ b/05_DASHBOARD/app.R @@ -39,7 +39,7 @@ find_directory_of_file <- function(file_name, start_dir=getwd()) { # find file_name from current working directory # before trying from a shallower directory -file_name <- "*05_DASHBOARD/app.R" # The file you are searching for +file_name <- "*DASHBOARD/app.R" # The file you are searching for try({ found_dir <- find_directory_of_file(file_name) diff --git a/05_DASHBOARD/deploy-dev.R b/05_DASHBOARD/deploy-dev.R index 88239a0..1b235b9 100644 --- a/05_DASHBOARD/deploy-dev.R +++ b/05_DASHBOARD/deploy-dev.R @@ -1,6 +1,27 @@ # Load the rsconnect package library(rsconnect) +# a function to list all files in a directory and its subdirectories +list_all_files <- function(directories) { + all_files <- c() # Initialize an empty vector to store all file paths + + # Loop through each directory provided + for (dir in directories) { + if (!dir.exists(dir)) { + next # Skip if directory does not exist + } + + # Use list.files to recursively list all files in the directory and its subdirectories + # full.names = TRUE returns the full paths, which is what we want + files <- list.files(dir, recursive = TRUE, full.names = TRUE) + + # Append the files found to the all_files vector + all_files <- c(all_files, files) + } + + return(all_files) +} + # Authenticate rsconnect::setAccountInfo( name = Sys.getenv("SHINY_ACC_NAME"), @@ -8,8 +29,9 @@ rsconnect::setAccountInfo( secret = Sys.getenv("SHINY_SECRET")) # Deploy +req_files <- list_all_files(c("./04_Tests", "./03_Data_for_app")) # get all the required files rsconnect::deployApp( appDir = "./05_DASHBOARD", - appFiles = "app.R", + appFiles = c("app.R", req_files), appName = "Dashboard-dev", forceUpdate = TRUE) From f1df5f92f7ac20513bc4eb80552856d311c15f18 Mon Sep 17 00:00:00 2001 From: Chris <53177842+ChristopherMarais@users.noreply.github.com> Date: Sat, 24 Feb 2024 19:38:47 -0500 Subject: [PATCH 5/9] Test app.R in root standard for rsconnect --- 05_DASHBOARD/deploy-dev.R | 4 ++-- 05_DASHBOARD/deploy.R | 4 ++-- 05_DASHBOARD/app.R => app.R | 8 ++++---- 3 files changed, 8 insertions(+), 8 deletions(-) rename 05_DASHBOARD/app.R => app.R (96%) diff --git a/05_DASHBOARD/deploy-dev.R b/05_DASHBOARD/deploy-dev.R index 1b235b9..5fa025b 100644 --- a/05_DASHBOARD/deploy-dev.R +++ b/05_DASHBOARD/deploy-dev.R @@ -31,7 +31,7 @@ rsconnect::setAccountInfo( # Deploy req_files <- list_all_files(c("./04_Tests", "./03_Data_for_app")) # get all the required files rsconnect::deployApp( - appDir = "./05_DASHBOARD", - appFiles = c("app.R", req_files), + # appDir = "./05_DASHBOARD", + # appFiles = c("app.R", req_files), appName = "Dashboard-dev", forceUpdate = TRUE) diff --git a/05_DASHBOARD/deploy.R b/05_DASHBOARD/deploy.R index 1cbe136..2100d51 100644 --- a/05_DASHBOARD/deploy.R +++ b/05_DASHBOARD/deploy.R @@ -9,7 +9,7 @@ rsconnect::setAccountInfo( # Deploy rsconnect::deployApp( - appDir = "./05_DASHBOARD", - appFiles = "app.R", + # appDir = "./05_DASHBOARD", + # appFiles = "app.R", appName = "Dashboard", forceUpdate = TRUE) diff --git a/05_DASHBOARD/app.R b/app.R similarity index 96% rename from 05_DASHBOARD/app.R rename to app.R index 8d71a29..c4e3bfb 100644 --- a/05_DASHBOARD/app.R +++ b/app.R @@ -39,20 +39,20 @@ find_directory_of_file <- function(file_name, start_dir=getwd()) { # find file_name from current working directory # before trying from a shallower directory -file_name <- "*DASHBOARD/app.R" # The file you are searching for +file_name <- "*app.R" # The file you are searching for try({ found_dir <- find_directory_of_file(file_name) # Check if found_dir is NULL or empty, indicating the file was not found if (is.null(found_dir) || length(found_dir) == 0) { # print error - print("05_DASHBOARD/app.R not found from current working directory!") + print("app.R not found from current working directory!") print("trying again from shallower directory") # trying again from great grandparent directory of working directory setwd("../../..") found_dir <- find_directory_of_file(file_name) if (is.null(found_dir) || length(found_dir) == 0) { - print("05_DASHBOARD/app.R likely does not exist in filesystem!") + print("app.R likely does not exist in filesystem!") } # Set working directory to parent directory of found dir setwd(fs::path_dir(found_dir[1])) @@ -60,7 +60,7 @@ try({ print(paste0("Working dir: ", getwd())) } # Set working directory to parent directory of found dir - setwd(fs::path_dir(found_dir[1])) + setwd(found_dir[1]) # Print working directory print(paste0("Working dir: ", getwd())) }, silent = FALSE) # Setting silent = FALSE will print the error message to the console From 599e311da98864a871eefa12dafa0eac42a28504 Mon Sep 17 00:00:00 2001 From: Chris <53177842+ChristopherMarais@users.noreply.github.com> Date: Sat, 24 Feb 2024 19:44:37 -0500 Subject: [PATCH 6/9] copy app.R file into root --- 05_DASHBOARD/Dockerfile | 1 + 05_DASHBOARD/Dockerfile-dev | 1 + 2 files changed, 2 insertions(+) diff --git a/05_DASHBOARD/Dockerfile b/05_DASHBOARD/Dockerfile index e9769d1..df362d8 100644 --- a/05_DASHBOARD/Dockerfile +++ b/05_DASHBOARD/Dockerfile @@ -6,6 +6,7 @@ ENV BRANCH_NAME=${BRANCH_NAME} RUN install2.r rsconnect shiny tidyverse bslib leaflet sf WORKDIR /usr/src/dashboard-development # Copy the 'app' directory contents into the container +COPY ./app.R /usr/src/dashboard-development/ COPY ./03_Data_for_app /usr/src/dashboard-development/03_Data_for_app/ COPY ./04_Tests /usr/src/dashboard-development/04_Tests/ COPY ./05_DASHBOARD /usr/src/dashboard-development/05_DASHBOARD/ diff --git a/05_DASHBOARD/Dockerfile-dev b/05_DASHBOARD/Dockerfile-dev index 1a6f996..e10745d 100644 --- a/05_DASHBOARD/Dockerfile-dev +++ b/05_DASHBOARD/Dockerfile-dev @@ -6,6 +6,7 @@ ENV BRANCH_NAME=${BRANCH_NAME} RUN install2.r rsconnect shiny tidyverse bslib leaflet sf WORKDIR /usr/src/dashboard-development # Copy the 'app' directory contents into the container +COPY ./app.R /usr/src/dashboard-development/ COPY ./03_Data_for_app /usr/src/dashboard-development/03_Data_for_app/ COPY ./04_Tests /usr/src/dashboard-development/04_Tests/ COPY ./05_DASHBOARD /usr/src/dashboard-development/05_DASHBOARD/ From bfd60e02611447f6e1ea98d7172300ce8e005956 Mon Sep 17 00:00:00 2001 From: Chris <53177842+ChristopherMarais@users.noreply.github.com> Date: Sat, 24 Feb 2024 19:56:20 -0500 Subject: [PATCH 7/9] Renamed files to have only one app.R file as is standard. --- 04_Tests/HAB_test/{app.R => HAB_test_app.R} | 0 04_Tests/leaflet_test1/{app.R => leaflet_test_app.R} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename 04_Tests/HAB_test/{app.R => HAB_test_app.R} (100%) mode change 100755 => 100644 rename 04_Tests/leaflet_test1/{app.R => leaflet_test_app.R} (100%) diff --git a/04_Tests/HAB_test/app.R b/04_Tests/HAB_test/HAB_test_app.R old mode 100755 new mode 100644 similarity index 100% rename from 04_Tests/HAB_test/app.R rename to 04_Tests/HAB_test/HAB_test_app.R diff --git a/04_Tests/leaflet_test1/app.R b/04_Tests/leaflet_test1/leaflet_test_app.R similarity index 100% rename from 04_Tests/leaflet_test1/app.R rename to 04_Tests/leaflet_test1/leaflet_test_app.R From 91303d07a15eae9f2298b9aed1c1bf68cd4c6989 Mon Sep 17 00:00:00 2001 From: Chris <53177842+ChristopherMarais@users.noreply.github.com> Date: Sat, 24 Feb 2024 20:03:45 -0500 Subject: [PATCH 8/9] update search string for app.R file in setting dir --- app.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app.R b/app.R index c4e3bfb..028b0ed 100644 --- a/app.R +++ b/app.R @@ -39,14 +39,14 @@ find_directory_of_file <- function(file_name, start_dir=getwd()) { # find file_name from current working directory # before trying from a shallower directory -file_name <- "*app.R" # The file you are searching for +file_name <- "*/app.R" # The file you are searching for try({ found_dir <- find_directory_of_file(file_name) # Check if found_dir is NULL or empty, indicating the file was not found if (is.null(found_dir) || length(found_dir) == 0) { # print error - print("app.R not found from current working directory!") + print("/app.R not found from current working directory!") print("trying again from shallower directory") # trying again from great grandparent directory of working directory setwd("../../..") From daf79afe172bc701d1ce7fb9a76a3a11e1196ab0 Mon Sep 17 00:00:00 2001 From: Chris <53177842+ChristopherMarais@users.noreply.github.com> Date: Sat, 24 Feb 2024 20:10:59 -0500 Subject: [PATCH 9/9] Clean up testing functions --- 05_DASHBOARD/deploy-dev.R | 24 ------------------------ 05_DASHBOARD/deploy.R | 2 -- 2 files changed, 26 deletions(-) diff --git a/05_DASHBOARD/deploy-dev.R b/05_DASHBOARD/deploy-dev.R index 5fa025b..42cdb42 100644 --- a/05_DASHBOARD/deploy-dev.R +++ b/05_DASHBOARD/deploy-dev.R @@ -1,27 +1,6 @@ # Load the rsconnect package library(rsconnect) -# a function to list all files in a directory and its subdirectories -list_all_files <- function(directories) { - all_files <- c() # Initialize an empty vector to store all file paths - - # Loop through each directory provided - for (dir in directories) { - if (!dir.exists(dir)) { - next # Skip if directory does not exist - } - - # Use list.files to recursively list all files in the directory and its subdirectories - # full.names = TRUE returns the full paths, which is what we want - files <- list.files(dir, recursive = TRUE, full.names = TRUE) - - # Append the files found to the all_files vector - all_files <- c(all_files, files) - } - - return(all_files) -} - # Authenticate rsconnect::setAccountInfo( name = Sys.getenv("SHINY_ACC_NAME"), @@ -29,9 +8,6 @@ rsconnect::setAccountInfo( secret = Sys.getenv("SHINY_SECRET")) # Deploy -req_files <- list_all_files(c("./04_Tests", "./03_Data_for_app")) # get all the required files rsconnect::deployApp( - # appDir = "./05_DASHBOARD", - # appFiles = c("app.R", req_files), appName = "Dashboard-dev", forceUpdate = TRUE) diff --git a/05_DASHBOARD/deploy.R b/05_DASHBOARD/deploy.R index 2100d51..3543d46 100644 --- a/05_DASHBOARD/deploy.R +++ b/05_DASHBOARD/deploy.R @@ -9,7 +9,5 @@ rsconnect::setAccountInfo( # Deploy rsconnect::deployApp( - # appDir = "./05_DASHBOARD", - # appFiles = "app.R", appName = "Dashboard", forceUpdate = TRUE)