|
1 | 1 | #' Load tree data from a file or database connection |
2 | 2 | #' |
3 | | -#' `load_tree_data()` fetches tree records from a data source, most commonly |
4 | | -#' a comma-separated values (CSV) file, a SQLite database file (.db, .sqlite, |
| 3 | +#' `load_tree_data()` fetches tree records from a data source, typically a |
| 4 | +#' comma-separated values (CSV) file, a SQLite database file (.db, .sqlite, |
5 | 5 | #' .gpkg), or a PostgreSQL database connection. Other data sources are also |
6 | 6 | #' possible. File-based sources can be read from compressed archives without |
7 | | -#' prior extraction (e.g., .zip), and network-hosted files can be read directly |
8 | | -#' without prior download (see Details). |
| 7 | +#' prior extraction if desired (e.g., .zip), and network-hosted files can be |
| 8 | +#' read directly without prior download (see Details). This function provides |
| 9 | +#' certain conveniences, but tree data could also be loaded in various other |
| 10 | +#' ways (e.g., `read.csv()`). |
9 | 11 | #' |
10 | 12 | #' @details |
11 | | -#' A data source is typically specified as one of the following: |
| 13 | +#' A data source is most commonly specified as one of the following: |
12 | 14 | #' |
13 | 15 | #' CSV file\cr |
14 | 16 | #' Path to a text file with `".csv"` extension. For files structured as CSV, |
15 | 17 | #' but not ending with the `".csv"` extension, a `"CSV:"` prefix can be added |
16 | 18 | #' before the filename to force loading as CSV format. |
17 | 19 | #' |
18 | 20 | #' SQLite database\cr |
19 | | -#' Path to a SQLite file. File extensions are typically `".db"` or `".sqlite"`. |
20 | | -#' GeoPackage SQLite files with `".gpkg"` extension are also supported. |
| 21 | +#' Path to a SQLite file. The file extension is generally either `".db"` or |
| 22 | +#' `".sqlite"`, but GeoPackage files with the `".gpkg"` extension are also |
| 23 | +#' supported. |
21 | 24 | #' |
22 | 25 | #' PostgreSQL database\cr |
23 | 26 | #' A connection string in one of the following formats: |
24 | 27 | #' ``` |
25 | | -#' src <- "PG:dbname=databasename" |
| 28 | +#' "PG:dbname=databasename" |
26 | 29 | #' |
27 | | -#' src <- "PG:dbname='db' host='addr' port='5432' user='x' password='y'" |
| 30 | +#' "PG:dbname='db' host='addr' port='5432' user='x' password='y'" |
28 | 31 | #' |
29 | | -#' src <- "PG:service=servicename" |
| 32 | +#' "PG:service=servicename" |
30 | 33 | #' |
31 | | -#' src <- "postgresql://[usr[:pwd]@][netloc][:port][/db][?param1=val1&...]" |
| 34 | +#' "postgresql://[user[:pwd]@][netloc][:port][/dbname][?param1=val1&...]" |
32 | 35 | #' ``` |
33 | 36 | #' |
34 | 37 | #' GDAL Virtual File Systems are also supported. This allows, for example, |
35 | 38 | #' reading from compressed archives such as `".zip"` without prior extraction. |
36 | 39 | #' The syntax in that case uses the `"/vsizip/"` prefix: |
37 | 40 | #' ``` |
38 | 41 | #' # relative path to the .zip: |
39 | | -#' src <- "/vsizip/MT_CSV.zip/MT_TREE.csv" |
| 42 | +#' f <- "/vsizip/MT_CSV.zip/MT_TREE.csv" |
40 | 43 | #' |
41 | 44 | #' # absolute path to the .zip: |
42 | | -#' src <- "/vsizip//home/ctoney/data/MT_CSV.zip/MT_TREE.csv" |
| 45 | +#' f <- "/vsizip//home/ctoney/data/MT_CSV.zip/MT_TREE.csv" |
43 | 46 | #' |
44 | 47 | #' # on Windows: |
45 | | -#' src <- "/vsizip/c:/users/ctoney/MT_CSV.zip/MT_TREE.csv" |
| 48 | +#' f <- "/vsizip/c:/users/ctoney/MT_CSV.zip/MT_TREE.csv" |
46 | 49 | #' ``` |
47 | 50 | #' |
48 | 51 | #' Network-hosted files can also be read without prior download using the |
49 | 52 | #' `"/vsicurl/"` prefix: |
50 | 53 | #' ``` |
51 | | -#' src <- "/vsicurl/https://apps.fs.usda.gov/fia/datamart/CSV/MT_TREE.csv" |
| 54 | +#' vsi_f <- "/vsicurl/https://apps.fs.usda.gov/fia/datamart/CSV/MT_TREE.csv" |
52 | 55 | #' ``` |
53 | 56 | #' |
54 | 57 | #' For more details, including supported VSI prefixes for cloud storage services |
|
59 | 62 | #' database connection string (see Details). |
60 | 63 | #' @param table Optional character string giving the name of a table in `src` |
61 | 64 | #' from which tree records will be fetched. Generally needed with database |
62 | | -#' sources containing multiple tables, as opposed to a single-table source such |
63 | | -#' as a CSV file. |
| 65 | +#' sources containing multiple tables (as opposed to a single-table source such |
| 66 | +#' as a CSV file). |
64 | 67 | #' @param columns Optional character vector specifying a subset of column names |
65 | | -#' in the source table to include in the result set. |
| 68 | +#' in the source table to include in the result set. Defaults to |
| 69 | +#' [DEFAULT_TREE_COLUMNS]. Can also be set to `NULL` or empty string (`""`) to |
| 70 | +#' read all columns in the source table. |
66 | 71 | #' @param sql Optional character string containing a SQL SELECT statement to |
67 | | -#' execute on `src` (instead of selecting all records potentially from a subset |
| 72 | +#' execute on `src` (instead of selecting all records, potentially from a subset |
68 | 73 | #' of columns, i.e., mutually exclusive with `table` and/or `columns`). |
69 | 74 | #' @return |
70 | | -#' A data frame containing the tree records fetched from `src`. |
| 75 | +#' A data frame containing tree records fetched from `src`. |
71 | 76 | #' |
72 | 77 | #' @note |
73 | 78 | #' `src` can be any GDAL supported dataset. A full list of formats supported by |
|
79 | 84 | #' |
80 | 85 | #' For more details: \url{https://gdal.org/en/stable/drivers/vector/index.html} |
81 | 86 | #' |
| 87 | +#' @seealso |
| 88 | +#' [DEFAULT_TREE_COLUMNS] |
| 89 | +#' |
82 | 90 | #' @examples |
83 | 91 | #' # Lolo NF, single-condition forest plots, INVYR 2022, from public FIADB |
84 | 92 | #' f <- system.file("extdata/mt_lnf_2022_1cond_tree.csv", package="FIAstemmap") |
@@ -143,7 +151,7 @@ load_tree_data <- function(src, table = NULL, columns = DEFAULT_TREE_COLUMNS, |
143 | 151 | } |
144 | 152 |
|
145 | 153 | if (!methods::is(ds, "Rcpp_GDALVector")) { |
146 | | - cli::cli_alert_danger("failed to access tree data in {.path {src}}") |
| 154 | + cli::cli_alert_danger("Failed to access tree data in {.path {src}}") |
147 | 155 | if (!is.null(sql)) |
148 | 156 | stop("execute SQL failed on 'src'", call. = FALSE) |
149 | 157 | else |
|
0 commit comments