Skip to content

Commit

Permalink
run styler::style_pkg(), add action for doc-and-style-r
Browse files Browse the repository at this point in the history
  • Loading branch information
iantaylor-NOAA committed Dec 10, 2024
1 parent 1cada34 commit 01d399b
Show file tree
Hide file tree
Showing 23 changed files with 240 additions and 226 deletions.
1 change: 1 addition & 0 deletions .github/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.html
*.rds
10 changes: 10 additions & 0 deletions .github/workflows/call-doc-and-style-r.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# document and style R code using a reusable workflow
name: call-doc-and-style-r
# on specifies the build triggers. See more info at https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows
on:
push:
branches: [main]
jobs:
call-workflow:
uses: nmfs-fish-tools/ghactions4r/.github/workflows/doc-and-style-r.yml@main

5 changes: 3 additions & 2 deletions R/calc_index_areas.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,14 @@
#' list("test" = c(39, 38))
#' # run the function
#' index <- calc_index_areas(
#' data, fit, grid, dir = getwd(), boundaries = list("test" = c(39, 38))
#' data, fit, grid,
#' dir = getwd(), boundaries = list("test" = c(39, 38))
#' )
#' # look at the index, which will be in "test" because that is what we
#' # named the boundary
#' index[["test"]][["index"]]
#' }
#'
#'
calc_index_areas <- function(data,
fit,
prediction_grid,
Expand Down
2 changes: 1 addition & 1 deletion R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@
#' * South of Cape Mendocino
#' * North of Monterey Bay
#' * South of Monterey Bay
"boundaries_data"
"boundaries_data"
2 changes: 1 addition & 1 deletion R/diagnose.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ diagnose <- function(dir,
fit[["data"]][["residuals2"]] <- stats::residuals(
fit,
model = 2,
type = "mle-mvn"
type = "mle-mvn"
)
}

Expand Down
4 changes: 2 additions & 2 deletions R/filter_grid.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#' @return A data frame with the same structure as `grid`.
#' @author Kelli F. Johnson
filter_grid <- function(grid, boundary_north, boundary_south) {
sub_grid <- grid %>%
sub_grid <- grid %>%
dplyr::filter(latitude < boundary_north) %>%
dplyr::filter(latitude >= boundary_south)
dplyr::filter(latitude >= boundary_south)
}
122 changes: 60 additions & 62 deletions R/filter_odfw.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@


# ##### AT SEA OBSERVER CPUE DATA PREP/FILTERING

# # created by A. Whitman
# # updated 06/21/2021
# # created by A. Whitman
# # updated 06/21/2021

# # last update to exclude 2020 due to sample size.

# # purpose: explore drift level data for the At Sea charter observer CPUE index
# # purpose: explore drift level data for the At Sea charter observer CPUE index

# # load some libraries - not sure if I'll need all these.

Expand Down Expand Up @@ -51,29 +49,29 @@
# dat<-dat[!is.na(dat$Total_lingcod),]
# summary(dat)

# # adding a p/a variable for lingcod
# # adding a p/a variable for lingcod
# dat$ling.pa<-ifelse(dat$Total_lingcod==0,0,1)

# # adding a megaregion variable
# # adding a megaregion variable
# dat$Megaregion<-as.factor(ifelse(dat$Port<30,"North","South"))

# # positive rate of overall dataset
# dim(dat)[1] # total records
# length(which(dat$ling.pa==1)) # total positive drifts
# length(which(dat$ling.pa==1))/dim(dat)[1] # positive rate
# # positive rate of overall dataset
# dim(dat)[1] # total records
# length(which(dat$ling.pa==1)) # total positive drifts
# length(which(dat$ling.pa==1))/dim(dat)[1] # positive rate

# # positive rate by year without any filtering
# pos_rate_year<-dat %>%
# pos_rate_year<-dat %>%
# dplyr::group_by(Year)%>%
# dplyr::summarise(No.Drifts=n(),Pos.Drifts=round((sum(ling.pa)/n()),3)) %>%
# dplyr::summarise(No.Drifts=n(),Pos.Drifts=round((sum(ling.pa)/n()),3)) %>%
# as.data.frame()
# pos_rate_year
# pos_rate_year

# # ouch on 2020, might need to throw that year out, otherwise, annual rate
# # ouch on 2020, might need to throw that year out, otherwise, annual rate
# # ranges from approx. 0.203 to 0.611

# #looking at potential filters
# # drift minutes, drifts without depth, depth bins, closed season or depths
# #looking at potential filters
# # drift minutes, drifts without depth, depth bins, closed season or depths
# # look at drifts by megaregion and by depth bin, filter by percent midwater

# ### FILTER: DRIFT MINUTES
Expand All @@ -91,7 +89,7 @@
# dat<-dat[dat$DriftMinutes<=28,]

# #'
# #'
# #'
# filter_odfw_drift_quantile <- function(x, percent = 0.95, verbose = FALSE) {
# limit <- stats::quantile(x, percent)
# remove <- which(x > limit)
Expand All @@ -108,110 +106,110 @@
# }
# test %>%
# dplyr::mutate(filter_drift = filter_odfw_drift_quantile(DriftMinutes))
# # positive rate of overall dataset
# dim(dat)[1] # total records
# length(which(dat$ling.pa==1)) # total positive drifts
# length(which(dat$ling.pa==1))/dim(dat)[1] # positive rate
# # positive rate of overall dataset
# dim(dat)[1] # total records
# length(which(dat$ling.pa==1)) # total positive drifts
# length(which(dat$ling.pa==1))/dim(dat)[1] # positive rate

# ### FILTER: DEPTHS

# summary(dat$SDEPTH_fm)
# # remove the NAs
# # remove the NAs
# dat<-dat[!is.na(dat$SDEPTH_fm),]
# histogram(dat$SDEPTH_fm)
# quantile(dat$SDEPTH_fm,0.99) #99 percentile is 60, so I will cut it off there
# quantile(dat$SDEPTH_fm,0.99) #99 percentile is 60, so I will cut it off there
# dat<-dat[dat$SDEPTH_fm<=60,]

# # positive rate of overall dataset
# dim(dat)[1] # total records
# length(which(dat$ling.pa==1)) # total positive drifts
# length(which(dat$ling.pa==1))/dim(dat)[1] # positive rate
# # positive rate of overall dataset
# dim(dat)[1] # total records
# length(which(dat$ling.pa==1)) # total positive drifts
# length(which(dat$ling.pa==1))/dim(dat)[1] # positive rate

# #creating a depth bin from the start depth in FMs
# dat<- dat %>%
# mutate(DepthBin=cut(dat$SDEPTH_fm,breaks = c(seq(0,60,by = 5)))) %>%
# dat<- dat %>%
# mutate(DepthBin=cut(dat$SDEPTH_fm,breaks = c(seq(0,60,by = 5)))) %>%
# as.data.frame()

# # look at drifts by depth bin
# # look at drifts by depth bin
# drifts_by_depth<-dat %>%
# group_by(DepthBin) %>%
# group_by(DepthBin) %>%
# summarise(drifts=n())
# drifts_by_depth
# histogram(dat$DepthBin)

# ### FILTER: PERCENT MIDWATER FISH
# ### FILTER: PERCENT MIDWATER FISH

# summary(dat$percent_midwater)
# quantile(dat$percent_midwater) # so 50% is 0.75 midwater, and 75% is 1.0! this would be a pretty heavy filter
# histogram(dat$percent_midwater) # lots of zeros and lots of 1.0, no clear break in between
# length(which(dat$percent_midwater<=0.75))
# length(which(dat$percent_midwater<=0.75))

# #just to check, are there lingcod caught on any 100% midwater trips?
# #just to check, are there lingcod caught on any 100% midwater trips?
# length(which(dat$percent_midwater==1.0 & dat$ling.pa==1)) # yay

# # if I filter out all the 1.0's, what do the quantiles look like then?
# # if I filter out all the 1.0's, what do the quantiles look like then?
# dat_test<-dat[dat$percent_midwater<1.0,]
# dim(dat_test)[1]
# quantile(dat_test$percent_midwater,0.99) # that's 0.92 percent midwater, so I think that's reasonable
# # it's also similar to the last round (which was 95% midwater)

# dat<-dat[dat$percent_midwater<=0.92,]

# # positive rate of overall dataset
# dim(dat)[1] # total records
# length(which(dat$ling.pa==1)) # total positive drifts
# length(which(dat$ling.pa==1))/dim(dat)[1] # positive rate
# # positive rate of overall dataset
# dim(dat)[1] # total records
# length(which(dat$ling.pa==1)) # total positive drifts
# length(which(dat$ling.pa==1))/dim(dat)[1] # positive rate

# #### CHECKING OPEN DEPTHS
# #### CHECKING OPEN DEPTHS
# summary(dat$GF_OpenDepth)

# # so lingcod is not a LL species, so those get recategorized
# # so lingcod is not a LL species, so those get recategorized
# levels(dat$GF_OpenDepth)[levels(dat$GF_OpenDepth)=="<30 fm & >40 fm (LL)"] <- "<30 fm"
# levels(dat$GF_OpenDepth)[levels(dat$GF_OpenDepth)=="<40 fm & >40 fm (LL)"] <- "<40 fm"
# histogram(dat$GF_OpenDepth)
# table(dat$GF_OpenDepth,dat$ling.pa > 0) # a few trips outside of 40fm

# #checking if fishing in legal depths
# #checking if fishing in legal depths
# #add a legal depth from the GF_OpenDepth
# dat$legal.dep<-as.numeric(ifelse(dat$GF_OpenDepth=="All Depth",1000,substring(dat$GF_OpenDepth,2,3)))
# dat$dep.diff<-dat$legal.dep-dat$SDEPTH_fm # so if it's negative, they were fishing in depths that were deeper than legal
# length(which(dat$dep.diff<0))

# #### FILTER: FISHING OUTSIDE LEGAL DEPTHS
# #### FILTER: FISHING OUTSIDE LEGAL DEPTHS

# # remove drifts outside legal depths, with a buffer
# # remove drifts outside legal depths, with a buffer
# dat<-dat[dat$dep.diff>-5,]
# # positive rate of overall dataset
# dim(dat)[1] # total records
# length(which(dat$ling.pa==1)) # total positive drifts
# length(which(dat$ling.pa==1))/dim(dat)[1] # positive rate

# # positive rate of overall dataset
# dim(dat)[1] # total records
# length(which(dat$ling.pa==1)) # total positive drifts
# length(which(dat$ling.pa==1))/dim(dat)[1] # positive rate

# # positive rate by year without any filtering
# pos_rate_year<-dat %>%
# pos_rate_year<-dat %>%
# dplyr::group_by(Year)%>%
# dplyr::summarise(No.Drifts=n(),Pos.Drifts=sum(ling.pa),Pos.Perc=round((sum(ling.pa)/n()),3)) %>%
# dplyr::summarise(No.Drifts=n(),Pos.Drifts=sum(ling.pa),Pos.Perc=round((sum(ling.pa)/n()),3)) %>%
# as.data.frame()
# pos_rate_year
# pos_rate_year

# ### FILTER - REMOVE 2020 BECAUSE OF SAMPLE SIZE
# ### FILTER - REMOVE 2020 BECAUSE OF SAMPLE SIZE

# dat<-dat[dat$Year %in% c(2001,2003:2019),]

# # positive rate of overall dataset
# dim(dat)[1] # total records
# length(which(dat$ling.pa==1)) # total positive drifts
# length(which(dat$ling.pa==1))/dim(dat)[1] # positive rate
# # positive rate of overall dataset
# dim(dat)[1] # total records
# length(which(dat$ling.pa==1)) # total positive drifts
# length(which(dat$ling.pa==1))/dim(dat)[1] # positive rate

# pos_rate_year<-dat %>%
# pos_rate_year<-dat %>%
# dplyr::group_by(Year)%>%
# dplyr::summarise(No.Drifts=n(),Pos.Drifts=sum(ling.pa),Pos.Perc=round((sum(ling.pa)/n()),3)) %>%
# dplyr::summarise(No.Drifts=n(),Pos.Drifts=sum(ling.pa),Pos.Perc=round((sum(ling.pa)/n()),3)) %>%
# as.data.frame()
# pos_rate_year

# # okay, I think that's it for filters.
# # okay, I think that's it for filters.

# # exporting final dataset and positive rate table
# # exporting final dataset and positive rate table

# #write.csv(dat,"Final_filtered_ORAtSea_updated.csv",row.names = F)
# #write.csv(pos_rate_year,"PositivesByYear_updated.csv",row.names = F)
2 changes: 1 addition & 1 deletion R/format_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
format_data <- function(data, ...) {
if (all(
c("Latitude_dd", "cpue_kg_per_ha_der", "Datetime_utc_iso") %in%
colnames(data)
colnames(data)
)) {
class(data) <- c("nwfscSurvey", class(data))
}
Expand Down
2 changes: 1 addition & 1 deletion R/format_strata.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#' format_strata(nwfscSurvey::GetStrata.fn("sablefish"))
format_strata <- function(strata = nwfscSurvey::GetStrata.fn("coast"),
min_depth = 55,
max_depth = 1280){
max_depth = 1280) {
strata_temp <- strata %>%
tidyr::separate(name, into = c("dname", "STRATA"), sep = "_") %>%
dplyr::group_by(STRATA) %>%
Expand Down
2 changes: 1 addition & 1 deletion R/lookup_grid.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ lookup_grid <- function(x,

out <- dplyr::mutate(
.data = data,
area_km2 = {{column}},
area_km2 = {{ column }},
vessel_year = "0",
depth_scaled = scale(depth, center = mean_depth, scale = sd_depth),
depth_scaled_squared = depth_scaled^2
Expand Down
10 changes: 5 additions & 5 deletions R/map_base.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ map_base <- function(x_min = -155,
# Crop the polygon for plotting and efficiency:
# sf::st_bbox(map_data) to find the rough coordinates
coast <- suppressWarnings(suppressMessages(
sf::st_crop(
map_data,
c(xmin = x_min, ymin = y_min, xmax = x_max, ymax = y_max)
)
))
sf::st_crop(
map_data,
c(xmin = x_min, ymin = y_min, xmax = x_max, ymax = y_max)
)
))
coast_proj <- sf::st_transform(coast, crs = utm_zone_10)
gg <- ggplot2::ggplot(coast_proj) +
ggplot2::geom_sf() +
Expand Down
3 changes: 1 addition & 2 deletions R/map_density.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ map_density <- function(predictions,
n_col = 2,
column_grep = "^est[2]*$",
tile_size = c(2000, 2000)) {

column <- grep(column_grep, colnames(predictions), value = TRUE)
predictions <- dplyr::rename(
.data = predictions,
Expand Down Expand Up @@ -66,7 +65,7 @@ map_density <- function(predictions,
)
names(x) <- split_names

gg <- map_base() +
gg <- map_base() +
ggplot2::geom_tile(
data = purrr::list_rbind(x, names_to = "year"),
mapping = aes(x * 1000, y * 1000, fill = layer)
Expand Down
Loading

0 comments on commit 01d399b

Please sign in to comment.