Skip to content

Commit

Permalink
Add soe_path() and friends
Browse files Browse the repository at this point in the history
  • Loading branch information
ateucher committed Jan 10, 2019
1 parent f08d54f commit a39d05c
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 7 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export(popup_create_row)
export(report_percent)
export(set_bc_view)
export(set_bc_view_on_close)
export(set_soe_root)
export(soe_path)
export(soft)
export(svg_px)
export(theme_soe)
Expand Down
20 changes: 13 additions & 7 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
# envreportutils 0.8.0

* Added new function `add_bc_home_button()`
* Added new function `set_bc_view`
* Added new function `set_bc_view_on_close`
* Added new function `css_caaqs_copy`
* Added new function `popup_caaqs`
* Added new function `popup_combine_rows`
* Added new function `popup_create_row`
* Added a set of functions to help create/customize leaflet maps:
* Added new function `add_bc_home_button()`
* Added new function `set_bc_view`
* Added new function `set_bc_view_on_close`
* Added new function `css_caaqs_copy`
* Added new function `popup_caaqs`
* Added new function `popup_combine_rows`
* Added new function `popup_create_row`

* Added new function `soe_path()` to set the path to a file or folder on the SOE
shared drive to enable platform/user agnostic reading/writing to the SOE shared
drive. Also added `set_soe_root()` so each user can set their path to the root
of the SOE shared drive.

# envreportutils 0.7

Expand Down
70 changes: 70 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,73 @@ to_titlecase <- function(x) {
if (!is.character(x)) stop("x must be a character string", call. = FALSE)
tools::toTitleCase(tolower(x))
}

#' Specify a path to a folder or file in the SOE folder on the network drive
#'
#' @param x character. Path to a file or folder, relative to `pickaxe/SOE`
#'
#' @return full path to the file or folder
#' @export
#'
#' @examples
#' \dontrun{
#' soe_path("Operations ORCS/indicators/air")
#' # In a function call:
#' read.csv(soe_path("Operations ORCS/data/foo.csv"))
#' }
soe_path <- function(x) {
if (!is.character(x))
stop("x must be a character string denoting a
path relative to the SOE root folder (e.g. Operations ORCS/indicators/air/.")
root <- get_soe_root()
file.path(root, gsub("^/", "", x))
}

#' Set the path to the root of your SoE network folder
#'
#' This well add an environment variable called `ENVREPORTUTILS_SOE_PATH` to
#' your `.Renviron` file and set it to the path on your computer to the
#' `pickaxe/SOE` folder.
#'
#' @param x character. A path to the SOE folder on the network (pickaxe) drive.
#' For Mac users it will look something like `"/Users/username/Volumes/pickaxe/SOE"`.
#' For Windows users, use the mapped drive letter rather than the UNC path
#' (e.g., `"P:/pickaxe/SOE"`)
#'
#' @return logical (invisibly)
#' @export
#'
#' @examples
#' \dontrun{
#' # Mac
#' set_soe_root("/Users/username/Volumes/pickaxe/SOE")
#' # Windows
#' set_soe_root("P:/pickaxe/SOE")
#' }
set_soe_root <- function(x) {
home_dir <- Sys.getenv("HOME")
renviron_file <- file.path(home_dir, ".Renviron")
renviron_lines <- readLines(renviron_file)
soe_path_env <- grepl(paste0("^", soe_path_envvar_name()), renviron_lines)
if (any(soe_path_env)) {
overwrite <- utils::askYesNo("Your soe path has already been set. Overwrite?")
if (!overwrite) return(invisible(FALSE))
renviron_lines <- renviron_lines[!soe_path_env]
}
renviron_lines <- c(renviron_lines, "", "## envreportutils soe path",
paste0(soe_path_envvar_name(), "=", x), "")

writeLines(renviron_lines, con = renviron_file)
message("Restart R for changes to take effect")
invisible(TRUE)
}

get_soe_root <- function() {
soe_root <- Sys.getenv(soe_path_envvar_name())
if (!nzchar(soe_root)) {
stop("You need to set your soe root path. Use set_soe_root()", call. = FALSE)
}
soe_root
}

soe_path_envvar_name <- function() "ENVREPORTUTILS_SOE_PATH"
30 changes: 30 additions & 0 deletions man/set_soe_root.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions man/soe_path.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a39d05c

Please sign in to comment.