Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
RobLBaker committed Oct 4, 2022
1 parent 778ac9d commit ef4d48f
Show file tree
Hide file tree
Showing 10 changed files with 243 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
^NPSdataverse\.Rproj$
^\.Rproj\.user$
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.Rproj.user
.Rhistory
.RData
.Ruserdata
28 changes: 28 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Package: NPSdataverse
Title: What the Package Does (One Line, Title Case)
Version: 0.0.0.9000
Authors@R:
person("Rob", "Baker", "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "YOUR-ORCID-ID"))
Description: What the package does (one paragraph).
License: `use_mit_license()`, `use_gpl3_license()` or friends to pick a
license
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.1
Imports:
cli,
crayon,
DPchecker (>= 0.0.0.9000),
EML,
EMLassemblyline,
EMLeditor (>= 0.0.0.9000),
NPSutils (>= 0.1.0),
QCkit (>= 0.1.0),
rstudioapi,
utils
Remotes:
nationalparkservice/DPchecker,
nationalparkservice/EMLeditor,
nationalparkservice/NPSutils,
nationalparkservice/QCkit
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Generated by roxygen2: do not edit by hand

export(NPSdataverse_packages)
22 changes: 22 additions & 0 deletions NPSdataverse.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Version: 1.0

RestoreWorkspace: No
SaveWorkspace: No
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX

AutoAppendNewline: Yes
StripTrailingWhitespace: Yes
LineEndingConversion: Posix

BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd,collate,namespace
83 changes: 83 additions & 0 deletions R/attach.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@

pkgs <- c("DPchecker", "EMLeditor", "NPSutils", "QCkit", "EML", "EMLassemblyline")

NPSdataverse_attach <- function() {
# Create `to_load` which is a character vector of all NPSdataverse
# packages not loaded in the current R session.
to_load <- check_loaded()

# If to_load has length 0, all main packages are loaded.
# Nothing will be attached.
if (length(to_load) == 0) {
return(invisible())
}

# Create a line rule with two text labels:
# "Attaching packages" on the left-hand side and
# NPSdataverse with the package version on the right-hand side
load_header <- cli::rule(
left = crayon::bold("Attaching packages"),
right = paste0("NPSdataverse ", package_version("NPSdataverse"))
)

# Return a character string containing the package version for each of NPSdataverse's constituents
versions <- vapply(to_load, package_version, character(1))

packages <- paste0(
crayon::green(cli::symbol$tick), " ", crayon::blue(format(to_load)), " ",
crayon::col_align(versions, max(crayon::col_nchar(versions)))
)

# Format for two columns
# if there is an odd number of packages, add ""
if (length(packages) %% 2 == 1) {
packages <- append(packages, "")
}
# Divide the packages into column 1 and 2
col1 <- seq_len(length(packages) / 2)
# paste the packages in column one with a space and those not in column 1
info <- paste0(packages[col1], " ", packages[-col1])

# display the message!
msg(load_header)
msg(paste(info, collapse = "\n"))

# Load the constituent packages!
# character.only = TRUE must be used in order to
# supply character strings to `library()`
suppressPackageStartupMessages(
lapply(to_load, library, character.only = TRUE)
)

# Thanks for playing
invisible()

}

# Detach all loaded packages for seeing the pretty startup message (:
NPSdataverse_detach <- function() {
pak <- paste0("package:", c(pkgs, "NPSdataverse"))
lapply(pak[pak %in% search()], detach, character.only = TRUE)
invisible()
}

#' List all packages imported by NPSdataverse
#'
#' @export
#'
#' @examples
#' NPSdataverse_packages()
NPSdataverse_packages <- function() {
# get all imports from NPSdataverse's package description file
raw <- utils::packageDescription("NPSdataverse")$Imports
# return a character vector of all the imports
imports <- strsplit(raw, ",")[[1]]
# "^\\s+" matches white space at the beginning of a character string
# "\\s+$ matches white space at the end of a character string
parsed <- gsub("^\\s+|\\s+$", "", imports)
# for each import, take only the first complete word (i.e. the package name)
names <- vapply(strsplit(parsed, "\\s+"), "[[", 1, FUN.VALUE = character(1))

return(names)

}
55 changes: 55 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

text_col <- function(x) {

# If RStudio API is not available and/or does not have the getThemeInfo
# button, exit function leaving default color of black
if (!rstudioapi::isAvailable() || !rstudioapi::hasFun("getThemeInfo")) {
return(x)
}

# Get theme information for RStudio
theme <- rstudioapi::getThemeInfo()

# If it's a dark theme, make the text color white; otherwise black.
if (isTRUE(theme$dark)) crayon::white(x) else crayon::black(x)

}

# Format the package version to indicate development versions when printed on
# the command line
package_version <- function(x) {
# packageVersion returns an object with class 'package_version' and 'numeric_version'
# unclass removes those and it becomes a numeric
version <- unclass(utils::packageVersion(x))[[1]]

# if the length of the numeric version vector is > 3,
# as happens with development packages, coerce those
# dev version numbers to red
if (length(version) > 3) {
version[4:length(version)] <- crayon::red(as.character(version[4:length(version)]))
}

# concatenate the result
paste0(version, collapse = ".")

}

# Create a message function for start-up that dynamically changes text color
msg <- function(...) {
packageStartupMessage(text_col(...))
}


# Check which of the main NPSdataverse packages
# are currently loaded
# The search() function returns a character vector containing packages
# attached to the current R session.
check_loaded <- function() {
paks <- paste0("package:", pkgs)
pkgs[!paks %in% search()]
}

# Is a package attached?
is_attached <- function(x) {
paste0("package:", x) %in% search()
}
12 changes: 12 additions & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.onAttach <- function(...) {
# See if any packages are needed
needed <- pkgs[!is_attached(pkgs)]
# If no packages are needed, return
if (length(needed) == 0) {
return()
# Otherwise, attach any needed packages
} else {
NPSdataverse_attach()
}

}
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,21 @@
# NPSdataPackageR
# NPSdataverse
Loads a suite of R packages for creating and manipulating data packages including interacting with DataStore.

## Installation

You can install the development version of NPSdataverse from [GitHub](https://github.com/) with:

``` r
# install.packages("devtools")
devtools::install_github("nationalparkservice/NPSdataverse")
```

NPSdataverse will install the following packages:

DPchecker
EMLeditor
NPSutils
QCkit
EML
EMLassemblyline

14 changes: 14 additions & 0 deletions man/NPSdataverse_packages.Rd

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

0 comments on commit ef4d48f

Please sign in to comment.