Fantasy History

# set working directory to that of shiny app
setwd("/Users/robertsgrava/Documents/Coding/R/fplVirsnieks/inst/shiny/fpl-virsnieks")

fantasy_snapshot <- get_fantasy_snapshot (1920, 8)

fantasy_history <-
rbindlist(
  lapply(
    fantasy_snapshot
    , function(x)
        cbind(x$key[ , !("fantasy_team_name_original")], fantasy_team_name=x$team_name, x$history)
  )
)
fantasy_history
fwrite(fantasy_history, "../../data/fantasy_history.csv")

fantasy_picks <-
rbindlist(
  lapply(
    fantasy_snapshot
    , function(x)
        cbind(x$key[ , !("fantasy_team_name_original")], fantasy_team_name=x$team_name, x$picks)
  )
)
fantasy_picks
fwrite(fantasy_picks, "../../data/fantasy_picks.csv")

fantasy_transfers <-
rbindlist(
  lapply(
    fantasy_snapshot
    , function(x)
        cbind(x$key[ , !("fantasy_team_name_original")], fantasy_team_name=x$team_name, x$transfers)
  )
)
fantasy_transfers
fwrite(fantasy_picks, "../../data/fantasy_transfers.csv")
fpl_now <- jsonlite::fromJSON(fplVirsnieks::FPL_NOW_URL)
fpl_now$events

Player Fixture History

# set working directory to that of shiny app
setwd("/Users/robertsgrava/Documents/Coding/R/fplVirsnieks/inst/shiny/fpl-virsnieks")

fpl_now <- jsonlite::fromJSON(fplVirsnieks::FPL_NOW_URL)

fpl_roster <- fplVirsnieks::create_fpl_roster(fpl_now)
player_ids <- unique(fpl_roster$fpl_player_id)
system.time(
player_fixture_history <-
  rbindlist(
    lapply(
      player_ids
      , function(x)
        data.table(jsonlite::fromJSON(paste0("https://fantasy.premierleague.com/api/element-summary/", x, "/"))$history)
    )
  )
)
player_fixture_history[]

fwrite(player_fixture_history, "../../data/player_fixture_history.csv")

Player Gameweek Live

gameweek <- 8
fpl_live <- jsonlite::fromJSON(paste0(fplVirsnieks::FPL_LIVE_URL, gameweek, "/live/"))$elements
n <- seq(1, nrow(fpl_live))
player_gameweek_live <- 
  rbindlist(
    lapply(
      n
      , function(x)
        data.table(gw=gameweek, id=fpl_live[x, "id"], fpl_live[x, "stats"])[ , explain := list(list(fpl_live$explain[[x]]))]
    )
  )
player_gameweek_live

Player Gameweek History

# set working directory to that of shiny app
setwd("/Users/robertsgrava/Documents/Coding/R/fplVirsnieks/inst/shiny/fpl-virsnieks")

system.time({
gameweek <- 8
g <- seq(1, gameweek)
player_gameweek_history <- 
  rbindlist(
    lapply(
      g
      , function(x) {
        fpl_live <- jsonlite::fromJSON(paste0(fplVirsnieks::FPL_LIVE_URL, x, "/live/"))$elements
        n <- seq(1, nrow(fpl_live))
        rbindlist(
          lapply(
            n
            , function(y)
              data.table(gw=x, id=fpl_live[y, "id"], fpl_live[y, "stats"])[ , explain := list(list(fpl_live$explain[[y]]))]
          )
        )
      }
    )
  )

}) # system.time()
player_gameweek_history

save(player_gameweek_history, file="../../data/player_gameweek_history.Rdata")


slowdevel/fpl-virsnieks documentation built on Nov. 5, 2019, 9:10 a.m.