read_rcg.R 1.47 KB
Newer Older
1
#' Read RCG file
2
#'
3
4
#' @param path RCG file path
#' @return Parsed tibble of RCG file in \code{path}
5
#' @examples
6
7
#' read_rcg("data/20220405162804-HELIOS_base_3-vs-enemy_2.rcg")
read_rcg <- function(path) {
8
9
10
11
12
    rcg <- path |>
        readr::read_file() |>
        jsonlite::parse_json(simplifyVector = TRUE, flatten = TRUE) |>
        tibble::as_tibble() |>
        dplyr::filter(type == "show") |>
13
        dplyr::select(time, players, ball.x, ball.y, ball.vx, ball.vy) |>
14
        tidyr::unnest(players) |>
15
        dplyr::select(time:unum, x:neck, ball.x:ball.vy) |>
16
17
        dplyr::rename(step = time) |>
        dplyr::rename_with(stringr::str_replace, pattern = "\\.", replacement = "_")
18
19
    return(rcg)
}
20

21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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)
41
    return(rcg)
42
}