Skip to content

Commit 4242a8f

Browse files
committed
removed dependency on magrittr from package to be replaced with native pipe
1 parent 039a898 commit 4242a8f

10 files changed

Lines changed: 119 additions & 101 deletions

DESCRIPTION

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ Authors@R:
55
person(given = "Andy",family = "Beet", role = c("aut", "cre"),
66
email = "andrew.beet@noaa.gov",
77
comment = c(ORCID = "0000-0001-8270-7090"))
8-
Description: A data package serving all data found on NOAAs stock SMART website
8+
Description: Provides access to data found on the NOAA Stock SMART website.
9+
The package serves as a data repository for fisheries assessment
10+
information and includes utilities for data retrieval.
911
URL: https://github.com/NOAA-EDAB/stocksmart, https://noaa-edab.github.io/stocksmart/
1012
BugReports: https://github.com/NOAA-EDAB/stocksmart/issues
1113
License: file LICENSE
@@ -18,7 +20,6 @@ Depends:
1820
Imports:
1921
dplyr,
2022
tidyr,
21-
magrittr,
2223
rlang,
2324
ggplot2,
2425
tibble,

NAMESPACE

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@ export(get_latest_metrics)
66
export(get_species_itis)
77
export(get_species_stock_data)
88
export(plot_ts)
9-
importFrom(magrittr,"%>%")
109
importFrom(rlang,.data)

R/get_available_ts.R

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,54 +14,56 @@
1414
#' Fmort). Some assessments have differing lengths of time series. }
1515
#'
1616
#'
17-
#' @importFrom magrittr "%>%"
1817
#' @importFrom rlang .data
1918
#'
2019
#' @export
2120

2221
get_available_ts <- function(itis = NULL, jurisdiction = NULL) {
23-
24-
2522
# Error check for metric names
2623
if (is.null(itis) || is.null(jurisdiction)) {
27-
stop("If you do not know the ITIS code or jurisdiction then please
28-
use get_species_itis function")
24+
stop(
25+
"If you do not know the ITIS code or jurisdiction then please
26+
use get_species_itis function"
27+
)
2928
}
3029

3130
# Pull filter the data to determine if present
32-
tsData <- stocksmart::stockAssessmentData %>%
33-
dplyr::filter(.data$ITIS == itis,
34-
.data$Jurisdiction == jurisdiction)
31+
tsData <- stocksmart::stockAssessmentData |>
32+
dplyr::filter(.data$ITIS == itis, .data$Jurisdiction == jurisdiction)
3533

3634
if (nrow(tsData) == 0) {
37-
stop("No data found. Either the itis or jurisdiction is not entered
35+
stop(
36+
"No data found. Either the itis or jurisdiction is not entered
3837
correctly. Please check your inputs or use get_species_itis() to find the
39-
correct ITIS code and jurisdiction.")
38+
correct ITIS code and jurisdiction."
39+
)
4040
}
4141

42-
4342
# find the first and last year of each assessment
44-
res <- tsData %>%
45-
46-
dplyr::group_by(.data$StockName,
47-
.data$CommonName,
48-
.data$Jurisdiction,
49-
.data$StockArea,
50-
.data$ITIS,
51-
.data$Metric,
52-
.data$AssessmentYear) %>%
53-
dplyr::summarise(FirstYear = min(.data$Year), LastYear = max(.data$Year),
54-
numYears = .data$LastYear - .data$FirstYear + 1,
55-
.groups = "drop") %>%
56-
dplyr::group_by(.data$StockName,
57-
.data$Jurisdiction,
58-
.data$ITIS,
59-
.data$AssessmentYear) %>%
60-
dplyr::summarise(nYrs = mean(.data$numYears),
61-
.groups = "drop")
43+
res <- tsData |>
6244

45+
dplyr::group_by(
46+
.data$StockName,
47+
.data$CommonName,
48+
.data$Jurisdiction,
49+
.data$StockArea,
50+
.data$ITIS,
51+
.data$Metric,
52+
.data$AssessmentYear
53+
) |>
54+
dplyr::summarise(
55+
FirstYear = min(.data$Year),
56+
LastYear = max(.data$Year),
57+
numYears = .data$LastYear - .data$FirstYear + 1,
58+
.groups = "drop"
59+
) |>
60+
dplyr::group_by(
61+
.data$StockName,
62+
.data$Jurisdiction,
63+
.data$ITIS,
64+
.data$AssessmentYear
65+
) |>
66+
dplyr::summarise(nYrs = mean(.data$numYears), .groups = "drop")
6367

6468
return(res)
65-
66-
6769
}

R/get_latest_full_assessment.r

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,15 @@
1616
#' AssessmentYear the data came from, the FirstYear and LastYear of the data
1717
#' and the number of years (numYear) of data retrieved}
1818
#'
19-
#' @importFrom magrittr "%>%"
2019
#' @importFrom rlang .data
2120
#'
2221
#' @export
2322

2423
get_latest_full_assessment <- function(itis = NULL) {
25-
26-
result <- get_latest_metrics(itis = itis,
27-
metrics = c("Catch", "Abundance", "Fmort",
28-
"Recruitment"))
24+
result <- get_latest_metrics(
25+
itis = itis,
26+
metrics = c("Catch", "Abundance", "Fmort", "Recruitment")
27+
)
2928

3029
return(result)
31-
3230
}

R/get_latest_metrics.r

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
#' the data came from, the FirstYear and LastYear of the data and the number
3232
#' of years (numYear) of data retrieved}
3333
#'
34-
#' @importFrom magrittr "%>%"
3534
#' @importFrom rlang .data
3635
#'
3736
#' @export

R/get_species_stock_data.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#' Pull species data directly
1+
#' Pull real-time species stock data directly from StockSMART API
22
#'
33
#' Pulls data defined by stock directly to obtain current data. For those who
44
#' don't want to reinstall the package to get most recent data

R/plot_ts.R

Lines changed: 67 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -24,34 +24,36 @@
2424
#' \item{plot}{ggplot object}
2525
#' \item{data}{dataframe used in the plotting}
2626
#'
27-
#' @importFrom magrittr "%>%"
2827
#' @importFrom rlang .data
2928
#'
3029
#' @export
3130

32-
plot_ts <- function(itis = NULL, stock = NULL, metric = "Catch",
33-
facetplot = FALSE, printfig = TRUE) {
34-
35-
31+
plot_ts <- function(
32+
itis = NULL,
33+
stock = NULL,
34+
metric = "Catch",
35+
facetplot = FALSE,
36+
printfig = TRUE
37+
) {
3638
#error check for metric names
3739
if (is.null(itis)) {
3840
stop("If you do not know the ITIS code then please use get_species_itis()")
3941
}
4042
if (!metric %in% c("Catch", "Fmort", "Recruitment", "Abundance", "Index")) {
41-
stop("Please use of the defined metrics to plot. Catch, Fmort, Recruitment,
42-
Abundance, Index")
43+
stop(
44+
"Please use of the defined metrics to plot. Catch, Fmort, Recruitment,
45+
Abundance, Index"
46+
)
4347
}
4448

45-
46-
4749
# filter by ITIS and Metric
48-
dataToPlot <- stocksmart::stockAssessmentData %>%
49-
dplyr::filter(.data$ITIS == itis,
50-
.data$Metric == metric) %>%
50+
dataToPlot <- stocksmart::stockAssessmentData |>
51+
dplyr::filter(.data$ITIS == itis, .data$Metric == metric) |>
5152
dplyr::mutate(AssessmentYear = as.factor(.data$AssessmentYear))
5253

53-
if (!is.null(stock)) { # filter by stock
54-
dataToPlot <- dataToPlot %>%
54+
if (!is.null(stock)) {
55+
# filter by stock
56+
dataToPlot <- dataToPlot |>
5557
dplyr::filter(.data$StockName == stock)
5658
}
5759

@@ -61,83 +63,103 @@ plot_ts <- function(itis = NULL, stock = NULL, metric = "Catch",
6163
}
6264

6365
# select stocks for this ITIS
64-
stocks <- dataToPlot %>%
66+
stocks <- dataToPlot |>
6567
dplyr::distinct(.data$StockName)
6668

6769
# if multiple stocks and user argument - NULL
6870
if ((nrow(stocks) > 1) && (is.null(stock))) {
69-
message(paste0("There are multiple stocks for ITIS = ",
70-
itis,
71-
". Please specify which stock you'd like to plot"))
71+
message(paste0(
72+
"There are multiple stocks for ITIS = ",
73+
itis,
74+
". Please specify which stock you'd like to plot"
75+
))
7276
stop(stocks)
7377
}
7478
# if multiple stocks and user argument doesn't match
75-
if ((nrow(stocks) > 1) && (!stock %in% (stocks %>% dplyr::pull()))) {
79+
if ((nrow(stocks) > 1) && (!stock %in% (stocks |> dplyr::pull()))) {
7680
message(paste0("Please specify a valid stock to plot "))
7781
stop(stocks)
7882
}
7983

8084
# if only a single stock and user argument doesn't match
8185
if (nrow(stocks) == 1) {
82-
if (is.null(stock)) { # user left blank
86+
if (is.null(stock)) {
87+
# user left blank
8388
stock <- stocks
84-
} else { # user entered a string
85-
if (!stock %in% (stocks %>% dplyr::pull())) {
89+
} else {
90+
# user entered a string
91+
if (!stock %in% (stocks |> dplyr::pull())) {
8692
message(paste0("Please specify a valid stock to plot "))
8793
stop(stocks)
8894
}
8995
}
90-
9196
}
9297

9398
# filter out stock to plot
9499
if (length(unique(dataToPlot$StockArea)) > 1) {
95100
# filter out stock
96-
stockToPlot <- dataToPlot %>%
97-
dplyr::filter(.data$StockName == stock) %>%
98-
dplyr::mutate(AssmtYrUnits = as.factor(paste0(.data$AssessmentYear,
99-
" (", .data$Units, ")")))
101+
stockToPlot <- dataToPlot |>
102+
dplyr::filter(.data$StockName == stock) |>
103+
dplyr::mutate(
104+
AssmtYrUnits = as.factor(paste0(
105+
.data$AssessmentYear,
106+
" (",
107+
.data$Units,
108+
")"
109+
))
110+
)
100111
} else {
101112
# no need to filter out stock (else causes error in dplyr 1.1.0)
102-
stockToPlot <- dataToPlot %>%
103-
dplyr::mutate(AssmtYrUnits = as.factor(paste0(.data$AssessmentYear,
104-
" (", .data$Units, ")")))
113+
stockToPlot <- dataToPlot |>
114+
dplyr::mutate(
115+
AssmtYrUnits = as.factor(paste0(
116+
.data$AssessmentYear,
117+
" (",
118+
.data$Units,
119+
")"
120+
))
121+
)
105122
}
106123

107124
# units check and standardize
108125
# eg if units change over time from say mt to thousands mt
109126

110-
111127
# plot data
112128
if (facetplot == TRUE) {
113129
p <- ggplot2::ggplot(stockToPlot) +
114-
ggplot2::geom_line(ggplot2::aes(x = .data$Year,
115-
y = .data$Value)) +
116-
ggplot2::facet_wrap(~.data$AssmtYrUnits, scales = "free_y") +
130+
ggplot2::geom_line(ggplot2::aes(x = .data$Year, y = .data$Value)) +
131+
ggplot2::facet_wrap(~ .data$AssmtYrUnits, scales = "free_y") +
117132
ggplot2::ylab(metric)
118133
} else {
119-
if ((stockToPlot %>% dplyr::distinct(.data$Units) %>% nrow()) > 1) {
120-
message("The units change over time. Plotting as a facet plot
121-
will be better")
134+
if ((stockToPlot |> dplyr::distinct(.data$Units) |> nrow()) > 1) {
135+
message(
136+
"The units change over time. Plotting as a facet plot
137+
will be better"
138+
)
122139
}
123140
p <- ggplot2::ggplot(stockToPlot) +
124-
ggplot2::geom_line(ggplot2::aes(x = .data$Year,
125-
y = .data$Value,
126-
color = .data$AssessmentYear)) +
127-
ggplot2::ylab(paste0(metric, " (", stockToPlot %>%
128-
dplyr::distinct(.data$Units), ")")) +
141+
ggplot2::geom_line(ggplot2::aes(
142+
x = .data$Year,
143+
y = .data$Value,
144+
color = .data$AssessmentYear
145+
)) +
146+
ggplot2::ylab(paste0(
147+
metric,
148+
" (",
149+
stockToPlot |>
150+
dplyr::distinct(.data$Units),
151+
")"
152+
)) +
129153
ggplot2::labs(color = "Assessment Year")
130154
}
131155

132156
p <- p +
133-
ggplot2::ggtitle(stockToPlot %>% dplyr::distinct(.data$StockName))
157+
ggplot2::ggtitle(stockToPlot |> dplyr::distinct(.data$StockName))
134158

135159
# print figure to window
136160
if (printfig) {
137161
print(p)
138162
}
139163

140164
return(list(plot = p, data = stockToPlot))
141-
142-
143165
}

man/get_species_stock_data.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkgdown/_pkgdown.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ reference:
1919
- title: "Filtering functions"
2020
desc: "Functions used to help user extract information from the bundled data"
2121
- contents:
22-
- get_latest_full_assessment
23-
- get_latest_metrics
24-
- get_available_ts
25-
- get_species_itis
22+
- starts_with("get_")
2623
- title: "Plotting functions"
2724
desc: "Functions used to help user visualize the bundled data"
2825
- contents:

vignettes/stocksmart.Rmd

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ knitr::opts_chunk$set(
1616

1717
```{r setup}
1818
library(stocksmart)
19-
library(magrittr)
2019
```
2120

2221

@@ -27,8 +26,8 @@ Time series data for all federally managed stocks are bundled with the package
2726
The [stockAssessmentData](../reference/stockAssessmentData.html) looks like this:
2827

2928
```{r sad, echo = FALSE}
30-
stockAssessmentData %>%
31-
tibble::as_tibble()
29+
stockAssessmentData |>
30+
dplyr::as_tibble()
3231
3332
```
3433

@@ -104,12 +103,13 @@ cod$data
104103
We can then filter the the data by the Georges Bank stock and plot it.
105104

106105
``` {r GB, echo = TRUE, fig.alt = "Catch data for Atlantic cod in Georges Bank from the latest operational assessment"}
107-
cod$data %>%
108-
dplyr::filter(StockArea == "Georges Bank") %>%
109-
{ . ->> filteredData } %>%
110-
ggplot2::ggplot(.) +
106+
filteredData <- cod$data |>
107+
dplyr::filter(StockArea == "Georges Bank")
108+
cod$data |>
109+
dplyr::filter(StockArea == "Georges Bank") |>
110+
ggplot2::ggplot() +
111111
ggplot2::geom_line(ggplot2::aes(x = Year, y = Value)) +
112-
ggplot2::ylab(filteredData %>% dplyr::distinct(Units)) +
113-
ggplot2::ggtitle(paste0("Assessment Year = ", filteredData %>%
112+
ggplot2::ylab(filteredData |> dplyr::distinct(Units)) +
113+
ggplot2::ggtitle(paste0("Assessment Year = ", filteredData |>
114114
dplyr::distinct(AssessmentYear)))
115115
```

0 commit comments

Comments
 (0)