You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: NEWS.md
+3-1
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,6 @@
1
-
# NPSutils 0.3.2 (in development)
1
+
# NPSutils 0.3.2 "Lost Coast"
2
+
* Add new functions, `load_data_packages()` and `load_data_package()`, which can load data packages (EML in .xml and data in .csv) similarly to the deprecated `load_data_package_deprecated()` function but also allows the data types in the tibbles loaded to be specified based on the information in the metadata.
3
+
* Deprecate `load_data_package()` and rename it to `load_data_package_deprecated()`.
2
4
* Update readme to us pak for package installation instead of devtools.
3
5
* Update _pkgdown.yml to use bootstrap 5
4
6
* added helper functions for API requests and user input to facilitate unit testing.
Copy file name to clipboardexpand all lines: R/load_data_package.R
+7-2
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,7 @@
1
1
#' Read contents of data package and constructs a list of tibbles based on the data file(s)
2
2
#'
3
-
#' @description \code{load_data_package} reads the data file(s) from a package and loads it into a list of tibbles. Current implementation only supports .csv data files.
3
+
#' `r lifecycle::badge("deprecated")`
4
+
#' @description `load_data_package_deprecated()` reads the data file(s) from a package and loads it into a list of tibbles. Current implementation only supports .csv data files.
4
5
#'
5
6
#' @param reference_id is a 6-7 digit number corresponding to the reference ID of the data package.
# Look for the zipped data package and attempt to unzip it. If the zipped file exists but cannot be unzipped, give the user a warning. If neither the unzipped nor zipped data packages exist, suggest the user check their working directory or use getDataPackage() to get the data package.
#' Read contents of data package(s) and return a tibble with a tibble for each data file.
2
-
#'
3
-
#' `r lifecycle::badge("experimental")`
1
+
#' Read contents of data package(s) and return a list of tibbles list of tibbles based on the data file(s). Can use metadata to specify data types.
4
2
#'
5
-
#' @description `load_data_packages()` loads one to may data packages and returns a tibble of tibbles where each data package is a tibble and within that each data file is it's own tibble. `load_data_packages()` will only work with .csv data files and EML metadata. `load_data_packages()` can also utilize the metadata to assign attributes to each data column.
3
+
#' @description `load_data_packages()` loads one to many data packages and returns a list. If only one data package is loaded, the list will be a list of tibbles where each tibble is a data (.csv) file from the data package. If multiple data packages are loaded, the list will be a list of lists where each nested list contains a list of tibble and each tibble is a data file (.csv). See `simplify` below for details on handling these lists.
6
4
#'
7
-
#' @details currently `load_data_packages()` only supports EML metadata and .csv files. To take advantage of the default settings in load_data_packages, use the default settings in `get_data_package()` or `get_data_packages()`. Archived (.zip) files must be extracted before `load_data_packages()` will work properly. Again, `get_data_package()` or `get_data_packages()` will accomplish this for you.
5
+
#' @details currently `load_data_packages()` only supports EML metadata and .csv files. The reference_id
8
6
#' '
9
-
#' @param reference_id is a list of 6-7 digit numbers corresponding to the DataStore reference ID of the datapackage(s) to load. Alternatively, you can set `reference_id` to "load_all", which will load all the data packages in your /data folder.
10
-
#' @param directory is the location of a folder, 'data' (created during `get_data_packages()`) which contains sub-directories where each sub-directory is the DataStore referenceId of the data package. Again, this file structure is all set up using `get_data_packages()`. Defaults to the current working directory (which is the default location for `get_data_packages()`).
11
-
#' @param assign_attributes Logical. Defaults to FALSE. Data will be loaded using `readr::read_csv()` guessing algorithm for calling column types. If set to TRUE, column types will be set using metadata attributes via the yet-to-be written `load_metadata()` function. `r lifecycle::badge('experimental')`
12
-
#' @param simplify Logical. Defaults to TRUE. If there is only a single data package loaded, the function will return a simple list of tibbles (where each tibble reflects a data file from within the data package). If set to FALSE, the function will return a list that contains a list of tibbles. This structure mirrors the object structure returned if multiple data packages are simultaneously loaded (a list of data packages with each data package containing a list of tibbles where each tibble corresponds to a data file in the given data package).
7
+
#' @param reference_id the immediate directory/directories where your data packages reside. For data packages downloaded from DataStore using `get_data_package()` or `get_data_packages()` default settings, this is the DataStore reference ID for your data package(s). Alternatively, you can set `reference_id` to "`load_all`", which will load all the data packages in the directory specified in via `directory` (typically ./data).
8
+
#' @param directory is the location of a folder that contains all of the data packages (where data packages are a folder containing .csv data files and a single .xml EML metadata file). If these data packages were downloaded from DataStore using the default settings for `get_data_packages`, this folder is "./data" and you can use the default settings for `directory`.
9
+
#' @param assign_attributes Logical. Defaults to FALSE. Data will be loaded using `readr::read_csv()` guessing algorithm for calling column types. If you set to `assign_attributes = TRUE`, column types will be set using the data types specified in the metadata. Currently supported data types include string, dateTime, float, double, integer, and categorical (factor in R). This assignment is very stringent: for instance if you did not specify date-time formats using ISO-8601 notation (i.e. "YYYY", not "yyyy"), your data will import as NAs. If you have undefined missing values or blank cells, your data will not import at all. If you run into problems consider using the default settings and letting `read_csv` guess the column types.
10
+
#' @param simplify Logical. Defaults to TRUE. If `simplify = TRUE`, the function will return a list of tibbles where each tibble is a data file from the data package(s) specified. The tibbles are named using the following format: "pkg_<reference_id.filename" (without the filename extension). If you want to load each individual data file into R for further processing, use `simplify = TRUE` and then run `list2env(x, envir=.GlobalEnv)`. If you set `simplify = FALSE`, the object returned will either be a list of tibbles identical to that returned by `simplify = TRUE` (if only one data package is loaded) or will be a list of lists where each nested list is a contains one tibble for each data file in each data package.Setting `simplify = FALSE` may make it easier to do post-processing on a package-by-package level rather than a tibble-by-tibble level.
13
11
#'
14
12
#' @return a list of (of lists of) tibbles.
15
13
#'
@@ -21,61 +19,172 @@
21
19
#' }
22
20
#'
23
21
load_data_packages<-function(reference_id,
24
-
directory=here::here(),
22
+
directory=here::here("data"),
25
23
assign_attributes=FALSE,
26
24
simplify=TRUE){
27
-
#capture original working directory
28
-
orig_wd<- getwd()
29
-
#set directory back to original working directory on exit.
30
-
on.exit(setwd(orig_wd))
31
-
#set wd to path; defaults to wd.
32
-
setwd(directory)
25
+
33
26
34
27
#is user specifies "allData" get all directories from the data folder:
35
-
if (reference_id=="all_data") {
28
+
if (length(seq_along(reference_id))==1&&reference_id=="load_all") {
36
29
reference_id<- list.dirs(path=directory,
37
30
full.names=FALSE,
38
31
recursive=FALSE)
39
32
}
40
33
41
-
### fix how single data packages are handled later:
0 commit comments