Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

revamp sitrep and nflverse_data class #199

Merged
merged 6 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 18 additions & 37 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
@@ -1,70 +1,51 @@
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
branches:
- main
- master
schedule:
- cron: 15 15 1,15 * *
branches: [main, master]
pull_request:
branches:
- main
- master
- dev
branches: [main, master]
workflow_dispatch:

name: R-CMD-check

jobs:
R-CMD-check:
runs-on: ${{ matrix.config.os }}
if: "!contains(github.event.head_commit.message, 'ci skip')"

name: ${{ matrix.config.os }} (${{ matrix.config.r }})

strategy:
fail-fast: false
matrix:
config:
- {os: windows-latest, r: 'release', rspm: "https://packagemanager.rstudio.com/all/latest"}
#- {os: windows-latest, r: '3.6', rspm: "https://packagemanager.rstudio.com/cran/latest"}
- {os: macOS-latest, r: 'release', rspm: "https://packagemanager.rstudio.com/all/latest"}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mrcaseb maybe it was that we were installing mac pkgs from RSPM instead of whatever the default is?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah that's what I figured and why I decided to test a newer workflow. I hope it resolves our problems and stops annoying us

- {os: ubuntu-22.04, r: 'release', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"}
- {os: ubuntu-22.04, r: 'devel', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"}
- {os: ubuntu-20.04, r: 'oldrel', rspm: "https://packagemanager.rstudio.com/cran/__linux__/bionic/latest"}
- {os: ubuntu-20.04, r: '3.6', rspm: "https://packagemanager.rstudio.com/cran/__linux__/bionic/latest"}
- {os: macos-latest, r: 'release'}
- {os: windows-latest, r: 'release'}
- {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'}
- {os: ubuntu-latest, r: 'release'}
- {os: ubuntu-latest, r: 'oldrel-1'}

env:
R_REMOTES_NO_ERRORS_FROM_WARNINGS: true
RSPM: ${{ matrix.config.rspm }}
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
NFLVERSE_GH_TOKEN: ${{ secrets.NFLVERSE_GH_TOKEN }}
R_KEEP_PKG_SOURCE: yes

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- uses: r-lib/actions/setup-pandoc@v2

- uses: r-lib/actions/setup-r@v2
with:
r-version: ${{ matrix.config.r }}
http-user-agent: ${{ matrix.config.http-user-agent }}
use-public-rspm: true

- uses: r-lib/actions/setup-pandoc@v2

- uses: r-lib/actions/setup-r-dependencies@v2
with:
cache-version: 2
extra-packages: |
rcmdcheck
ropensci/piggyback

- name: Check
env:
_R_CHECK_CRAN_INCOMING_REMOTE_: false
run: rcmdcheck::rcmdcheck(args = c("--no-manual", "--as-cran"), error_on = "warning", check_dir = "check")
shell: Rscript {0}
extra-packages: any::rcmdcheck, ropensci/piggyback
needs: check

- name: Upload check results
if: failure()
uses: actions/upload-artifact@main
- uses: r-lib/actions/check-r-package@v2
with:
name: ${{ runner.os }}-r${{ matrix.config.r }}-results
path: check
upload-snapshots: true
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by roxygen2: do not edit by hand

S3method(print,nflverse_data)
S3method(print,nflverse_sitrep)
export(.clear_cache)
export(.for_cran)
export(.sitrep)
Expand Down Expand Up @@ -50,4 +51,5 @@ export(raw_from_url)
export(rbindlist_with_attrs)
export(rds_from_url)
exportClasses(nflverse_data)
exportClasses(nflverse_sitrep)
importFrom(data.table,`:=`)
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ etc falsely returned last season after March 15th early in the month. (1.3.2.03)
- `nflreadr::nflverse_game_id()` accepts old team abbreviations and outputs useful errors. (1.3.2.05) (#191)
- Added `dictionary_player_stats_def` (thank you @mpcen!) (1.3.2.06) (#192)
- Added `.for_cran()` to limit parallelization in CRAN examples and tests (1.3.2.07)
- `nflverse_sitrep()` and friends now return a list of sitrep attributes instead of just printing to console.
- `make_nflverse_data_class()` now uses `data.table::setattr()` to set attributes and retain data.table pointer (v1.3.2.08)

# nflreadr 1.3.2

Expand Down
22 changes: 0 additions & 22 deletions R/from_url.R
Original file line number Diff line number Diff line change
Expand Up @@ -243,25 +243,3 @@ loader <- function(url){
detect_filetype <- function(url){
tools::file_ext(gsub(x = url, pattern = ".gz$", replacement = ""))
}

make_nflverse_data <- function(dataframe, nflverse_type = NULL, ...){

class(dataframe) <- c("nflverse_data","tbl_df","tbl","data.table","data.frame")

dots <- rlang::dots_list(..., .named = TRUE)

for(i in seq_along(dots)){
attr(dataframe, names(dots)[[i]]) <- dots[[i]]
}

if(!is.null(nflverse_type)) attr(dataframe, "nflverse_type") <- nflverse_type

if(
(length(dots) > 0 | !is.null(nflverse_type)) &&
is.null(attr(dataframe, "nflverse_timestamp"))
){
attr(dataframe, "nflverse_timestamp") <- Sys.time()
}

return(dataframe)
}
8 changes: 5 additions & 3 deletions R/load_participation.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ load_participation <- function(seasons = most_recent_season(),
by.x = c("nflverse_game_id","play_id"),
by.y = c("game_id","play_id"))

pbp_participation <- make_nflverse_data(pbp_participation,
nflverse_type = "play-by-play participation",
nflverse_timestamp = attr(participation,"nflverse_timestamp"))
pbp_participation <- make_nflverse_data(
pbp_participation,
nflverse_type = "play-by-play participation",
nflverse_timestamp = attr(participation, "nflverse_timestamp")
)

return(pbp_participation)
}
26 changes: 0 additions & 26 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,6 @@ NULL
ifelse(!is.na(x),x,y)
}

#' @export
#' @noRd
print.nflverse_data <- function(x,...){

cli::cli_rule("{.emph nflverse {attr(x,'nflverse_type')}}")

if(!is.null(attr(x,'nflverse_timestamp'))) {
cli::cli_alert_info(
"Data updated: {.field {format(attr(x,'nflverse_timestamp'), tz = Sys.timezone(), usetz = TRUE)}}"
)
}

NextMethod(print,x)
invisible(x)
}

#' rbindlist but maintain attributes of last file
#'
#' @export
Expand All @@ -43,16 +27,6 @@ rbindlist_with_attrs <- function(dflist){
out
}

#' nflverse data class
#'
#' This class has a special S3 print method that tries to read attached metadata and
#' provide timestamps and source attributes. It otherwise will dispatch to the
#' `data.frame` class.
#'
#' @name nflverse_data-class
#' @exportClass nflverse_data
methods::setOldClass(c("nflverse_data", "data.frame"))

.ignore_unused_imports <- function(){
curl::curl_fetch_memory
methods::setOldClass
Expand Down
49 changes: 49 additions & 0 deletions R/utils_nflverse_data_class.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#' nflverse data class
#'
#' This class has a special S3 print method that tries to read attached metadata and
#' provide timestamps and source attributes. It otherwise will dispatch to the
#' `data.frame` class.
#'
#' @name nflverse_data-class
#' @keywords internal
#' @exportClass nflverse_data
methods::setOldClass(c("nflverse_data", "data.frame"))

#' @export
#' @noRd
print.nflverse_data <- function(x,...){

cli::cli_rule("{.emph nflverse {attr(x,'nflverse_type')}}")

if(!is.null(attr(x,'nflverse_timestamp'))) {
cli::cli_alert_info(
"Data updated: {.field {format(attr(x,'nflverse_timestamp'), tz = Sys.timezone(), usetz = TRUE)}}"
)
}

NextMethod(print,x)
invisible(x)
}

make_nflverse_data <- function(df, nflverse_type = NULL, ...){

df <- data.table::as.data.table(df)
data.table::setattr(df, "class", c("nflverse_data", "tbl_df", "tbl", "data.table","data.frame"))

dots <- rlang::dots_list(..., .named = TRUE)

for(i in seq_along(dots)){
data.table::setattr(df, names(dots)[[i]], dots[[i]])
}

if(!is.null(nflverse_type)) data.table::setattr(df, "nflverse_type", nflverse_type)

if(
(length(dots) > 0 | !is.null(nflverse_type)) &&
is.null(attr(df, "nflverse_timestamp"))
){
data.table::setattr(df, "nflverse_timestamp", Sys.time())
}

return(df)
}
Loading
Loading