Skip to content

Commit f165c94

Browse files
committed
+ cc_db_catalog()
1 parent 5e95085 commit f165c94

File tree

79 files changed

+13221
-573
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+13221
-573
lines changed

DESCRIPTION

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: calcofi4r
22
Title: CalCOFI R helper functions
3-
Version: 0.8.0
3+
Version: 0.8.1
44
URL: https://calcofi.io/calcofi4r
55
BugReports: https://github.com/calcofi/calcofi4r/issues
66
Authors@R: c(
@@ -17,7 +17,7 @@ Description: The California Cooperative Oceanic Fisheries Investigations (CalCOF
1717
License: MIT + file LICENSE
1818
Encoding: UTF-8
1919
Roxygen: list(markdown = TRUE)
20-
RoxygenNote: 7.2.3
20+
RoxygenNote: 7.3.2
2121
Depends:
2222
R (>= 2.10)
2323
LazyData: true

NAMESPACE

+13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
export(cc_db_catalog)
34
export(cc_db_connect)
45
export(create_index)
56
export(get_cruises)
@@ -22,20 +23,32 @@ import(leaflet)
2223
import(readr)
2324
import(sf)
2425
import(stringr)
26+
importFrom(DT,datatable)
2527
importFrom(RPostgres,Postgres)
2628
importFrom(classInt,classIntervals)
29+
importFrom(dplyr,filter)
30+
importFrom(dplyr,left_join)
31+
importFrom(dplyr,select)
2732
importFrom(glue,glue)
33+
importFrom(htmltools,HTML)
34+
importFrom(htmltools,div)
35+
importFrom(htmltools,em)
36+
importFrom(htmltools,span)
37+
importFrom(htmltools,strong)
2838
importFrom(isoband,iso_to_sfg)
2939
importFrom(isoband,isobands)
3040
importFrom(mapview,mapView)
41+
importFrom(markdown,mark)
3142
importFrom(mgcv,gam)
3243
importFrom(plotly,ggplotly)
3344
importFrom(plotly,highlight)
3445
importFrom(plotly,highlight_key)
46+
importFrom(purrr,pmap_chr)
3547
importFrom(raster,extent)
3648
importFrom(raster,projectExtent)
3749
importFrom(raster,raster)
3850
importFrom(raster,values)
51+
importFrom(readr,read_csv)
3952
importFrom(scales,extended_breaks)
4053
importFrom(sf,st_bbox)
4154
importFrom(sf,st_buffer)

NEWS.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# calcofi4r 0.8.1
2+
3+
* Added `cc_db_catalog()` to list tables and columns in the database with descriptions (possibly formatted in markdown) by reading from new CalCOFI API endpoints: [api.calcofi.io/db_tables](https://api.calcofi.io/db_tables), [api.calcofi.io/db_columns](https://api.calcofi.io/db_columns).
4+
15
# calcofi4r 0.8.0
26

37
* Removed non-ASCII characters to allow install of package on Windows.

R/database.R

+92
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,98 @@ cc_db_connect <- function(path_pw = "~/.calcofi_db_pass.txt"){
4141
password = readLines(path_pw))
4242
}
4343

44+
#' Show CalCOFI database catalog as interactive table
45+
#'
46+
#' Read the tables and columns from the following [CalCOFI API](https://api.calcofi.io)
47+
#' and display as an interactive table (as `DT::datatable()`):
48+
#' - [api.calcofi.io/db_tables](https://api.calcofi.io/db_tables)
49+
#' - [api.calcofi.io/db_columns](https://api.calcofi.io/db_columns)
50+
#'
51+
#' @param tables optional character vector of table names to filter; default: `NULL`
52+
#'
53+
#' @return DT::datatable() of tables and columns
54+
#' @importFrom DT datatable
55+
#' @importFrom htmltools div em span strong HTML
56+
#' @importFrom markdown mark
57+
#' @importFrom purrr pmap_chr
58+
#' @importFrom readr read_csv
59+
#' @importFrom dplyr left_join filter select
60+
#' @importFrom glue glue
61+
#' @export
62+
#' @concept database
63+
#'
64+
#' @examples
65+
#' # full catalog
66+
#' cc_db_catalog()
67+
#' # only certain tables
68+
#' cc_db_catalog(tables = c("larvae_counts","nets","tows","stations","cruises"))
69+
cc_db_catalog <- function(tables = NULL){
70+
71+
d_tbls <- readr::read_csv(
72+
"https://api.calcofi.io/db_tables", show_col_types = F)
73+
d_cols <- readr::read_csv(
74+
"https://api.calcofi.io/db_columns", show_col_types = F)
75+
76+
d <- d_tbls |>
77+
dplyr::left_join(
78+
d_cols,
79+
by = c("schema","table_type","table"))
80+
81+
if (!is.null(tables))
82+
d <- d |>
83+
dplyr::filter(table %in% tables)
84+
85+
d |>
86+
mutate(
87+
Table = purrr::pmap_chr(
88+
list(table_type, table, table_description),
89+
function(table_type, table, table_description, ...){
90+
ifelse(
91+
is.na(table_description),
92+
htmltools::div(
93+
htmltools::span(style = "font-weight: 400;", htmltools::em(table_type), ":"), htmltools::strong(table)) |>
94+
as.character(),
95+
htmltools::div(
96+
htmltools::span(style = "font-weight: 400;", htmltools::em(table_type), ":"), htmltools::strong(table),
97+
htmltools::div(
98+
style = "font-size: 0.75rem; font-weight: 400;",
99+
htmltools::HTML(markdown::mark(table_description)))) |>
100+
as.character() ) } ),
101+
Column = purrr::pmap_chr(
102+
list(column, column_type, column_description),
103+
function(column, column_type, column_description, ...){
104+
ifelse(
105+
is.na(column_description),
106+
htmltools::div(
107+
htmltools::strong(column), htmltools::span(style = "font-weight: 400;", "(", htmltools::em(column_type), ")")) |>
108+
as.character(),
109+
htmltools::div(
110+
htmltools::strong(column), htmltools::span(style = "font-weight: 400;", "(", htmltools::em(column_type), ")"),
111+
htmltools::div(
112+
style = "font-size: 0.75rem; font-weight: 400;",
113+
htmltools::HTML(markdown::mark(column_description)))) |>
114+
as.character() ) } ) ) |>
115+
dplyr::select(Table, Column) |>
116+
DT::datatable(
117+
# extensions = c("RowGroup", "Buttons"),
118+
extensions = c("RowGroup"),
119+
options = list(
120+
# dom = "Blfrtip",
121+
# buttons = c("copy", "csv", "excel", "pdf", "print"),
122+
dom = "lfrtip",
123+
pageLength = 10,
124+
lengthMenu = c(10, 100, 1000),
125+
rowGroup = list(
126+
dataSrc = c(0, 1) ),
127+
columnDefs = list(
128+
list(
129+
visible = F,
130+
targets = c(0,1) ) ) ),
131+
escape = F,
132+
rownames = F)
133+
}
134+
135+
44136
#' Create index in database
45137
#'
46138
#' @param con database connection object from `DBI::dbConnect()`, e.g. from `cc_db_connect()`

docs/404.html

+8-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/CODE_OF_CONDUCT.html

+8-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)