#' Read RCG file #' #' @param path RCG file path #' @return Parsed tibble of RCG file in \code{path} #' @examples #' read_rcg("data/20220405162804-HELIOS_base_3-vs-enemy_2.rcg") read_rcg <- function(path) { rcg <- path |> readr::read_file() |> jsonlite::parse_json(simplifyVector = TRUE, flatten = TRUE) |> tibble::as_tibble() |> dplyr::filter(type == "show") |> dplyr::select(time, players, ball.x, ball.y, ball.vx, ball.vy) |> tidyr::unnest(players) |> dplyr::select(time:unum, x:neck, ball.x:ball.vy) |> dplyr::rename(step = time) |> dplyr::rename_with(stringr::str_replace, pattern = "\\.", replacement = "_") return(rcg) } read_rcg_csv <- function(path) { rcg <- path |> readr::read_csv() |> tibble::as_tibble() |> dplyr::filter(stopped == 0) |> dplyr::select(-"#", -stopped:-r_pen_score, -matches("^[lr]\\d+_t")) |> pivot_longer( cols = -c(cycle, b_x, b_y, b_vx, b_vy), # ボールのデータ列を除外 names_to = c("side", "unum", ".value"), # 新しい列名を設定 names_pattern = "([lr])(\\d+)_(.+)" # 正規表現で元の列名を分割 ) |> mutate(unum = as.numeric(unum)) |> dplyr::rename( step = cycle, ball_x = b_x, ball_y = b_y, ball_vx = b_vx, ball_vy = b_vy ) |> select(-ball_x, -ball_y, -ball_vx, -ball_vy, everything(), ball_x, ball_y, ball_vx, ball_vy) return(rcg) }