Commit f04a8431 authored by Keisuke ANDO's avatar Keisuke ANDO 😌
Browse files

Merge branch 'feature/package' into 'develop'

[update] close #4, close #3, close #2; RPackage化

See merge request !7
parents b28e230f ee5cf161
^soccer\.Rproj$
^\.Rproj\.user$
^LICENSE\.md$
^docker$
^docker-compose.yml$
^README.Rmd$
^temp$
Package: socceR
Title: RoboCupSoccer Simulation 2D log analysis tools
Version: 0.0.0.9000
Authors@R:
person(given = "Keisuke",
family = "ANDO",
role = c("aut", "cre"),
email = "ando@maslab.aitech.ac.jp")
Description: Functions to parse logs and to extract various information from log data.
License: MIT + file LICENSE
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.1
Imports:
dplyr,
fs,
jsonlite,
purrr,
readr,
rlang,
stringr,
tidyr
Suggests:
testthat (>= 3.0.0)
Config/testthat/edition: 3
YEAR: 2022
COPYRIGHT HOLDER: Keisuke ANDO
# MIT License
Copyright (c) 2022 Keisuke ANDO
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
# Generated by roxygen2: do not edit by hand
export(read_log)
export(read_rcg)
export(read_rcl)
importFrom(dplyr,as_tibble)
importFrom(dplyr,filter)
importFrom(dplyr,mutate)
importFrom(dplyr,rename_all)
importFrom(dplyr,select)
importFrom(dplyr,select_if)
importFrom(fs,path)
importFrom(fs,path_ext)
importFrom(jsonlite,parse_json)
importFrom(purrr,discard)
importFrom(purrr,map)
importFrom(purrr,map_chr)
importFrom(readr,read_file)
importFrom(readr,read_lines)
importFrom(rlang,.data)
importFrom(stringr,str_extract)
importFrom(stringr,str_remove)
importFrom(stringr,str_replace)
importFrom(stringr,str_split)
importFrom(stringr,str_trim)
importFrom(tidyr,unnest)
...@@ -2,10 +2,20 @@ ...@@ -2,10 +2,20 @@
#' #'
#' @param path log file path #' @param path log file path
#' @return Parsed tibble of log file in \code{path} #' @return Parsed tibble of log file in \code{path}
#' @importFrom fs path path_ext
#' @importFrom readr read_file read_lines
#' @importFrom jsonlite parse_json
#' @importFrom dplyr as_tibble filter select select_if rename_all mutate
#' @importFrom tidyr unnest
#' @importFrom purrr map map_chr discard
#' @importFrom stringr str_replace str_extract str_remove str_split str_trim
#' @importFrom rlang .data
#' @export #' @export
#' @examples #' @examples
#' \dontrun{
#' rcg <- read_log("data/20220405162804-HELIOS_base_3-vs-enemy_2.rcg") #' rcg <- read_log("data/20220405162804-HELIOS_base_3-vs-enemy_2.rcg")
#' rcl <- read_log("data/20220405162804-HELIOS_base_3-vs-enemy_2.rcl") #' rcl <- read_log("data/20220405162804-HELIOS_base_3-vs-enemy_2.rcl")
#' }
read_log <- function(path) { read_log <- function(path) {
ext <- path |> path() |> path_ext() ext <- path |> path() |> path_ext()
...@@ -19,19 +29,54 @@ read_log <- function(path) { ...@@ -19,19 +29,54 @@ read_log <- function(path) {
stop("Not supported format: ", path) stop("Not supported format: ", path)
} }
filter_rcg <- function(rcg, type) {
filtered <- rcg |>
filter(.data$type == (!!type)) |>
select_if(~ !all(is.na(.x)) && !all(is.null(unlist(.x)))) |>
select(!type)
return(filtered)
}
#' @rdname read_log #' @rdname read_log
#' @export #' @export
read_rcg <- function(path) { read_rcg <- function(path) {
rcg <- path |> parsed <- path |>
read_file() |> read_file() |>
parse_json(simplifyVector = TRUE, flatten = TRUE) |> parse_json(simplifyVector = TRUE, flatten = TRUE) |>
as_tibble() |> as_tibble()
filter(type == "show") |>
select(time, stime, players, ball.x, ball.y, ball.vx, ball.vy) |> rcg <- list(
unnest(players) |> header = parsed |>
select(time:capacity, ball.x:ball.vy) |> filter_rcg("header") |>
rename(step = time) |> as.list(),
rename_with(str_replace, pattern = "\\.", replacement = "_")
server_param = parsed |>
filter_rcg("server_param") |>
rename_all(str_remove, "params.") |>
as.list(),
player_param = parsed |>
filter_rcg("player_param") |>
rename_all(str_remove, "params.") |>
as.list(),
player_type = parsed |>
filter_rcg("player_type") |>
rename_all(str_remove, "params."),
team = parsed |>
filter_rcg("team") |>
unnest(.data$teams),
show = parsed |>
filter_rcg("show") |>
unnest(.data$players) |>
rename_all(str_replace, "[.]", "_"),
msg = parsed |>
filter_rcg("msg")
)
return(rcg) return(rcg)
} }
...@@ -43,29 +88,30 @@ read_rcl <- function(path) { ...@@ -43,29 +88,30 @@ read_rcl <- function(path) {
read_lines() |> read_lines() |>
as_tibble() |> as_tibble() |>
mutate( mutate(
step = value |> str_extract("\\d+") |> as.numeric(), time = .data$value |> str_extract("\\d+") |> as.numeric(),
agent = value |> str_extract("\\w+_([0-9]{1,2}|Coach)(?!\\))"), agent = .data$value |> str_extract("\\w+_([0-9]{1,2}|Coach)(?!\\))"),
team = agent |> str_remove("_([0-9]{1,2}|Coach)"), team = .data$agent |> str_remove("_([0-9]{1,2}|Coach)"),
unum = agent |> str_extract("([0-9]{1,2}|Coach)$"), unum = .data$agent |> str_extract("([0-9]{1,2}|Coach)$"),
commands = value |> commands = .data$value |>
str_extract("\\(.+\\)$") |> str_extract("\\(.+\\)$") |>
map(~ .x |> map(
~ .x |>
str_split("\\(|\\)", simplify = TRUE) |> str_split("\\(|\\)", simplify = TRUE) |>
str_trim() |> str_trim() |>
discard(~ .x == "")), discard(~ .x == "")),
) |> ) |>
unnest(commands) |> unnest(.data$commands) |>
mutate( mutate(
commands = commands |> str_split("\\ ", n = 2), splited = .data$commands |> str_split("\\ ", n = 2),
command = commands |> map_chr(1), command = .data$splited |> map_chr(1),
args = commands |> map(~ .x[-1]), args = .data$splited |> map(~ .x[-1]),
) |> ) |>
select( select(
step, .data$time,
team, .data$team,
unum, .data$unum,
command, .data$command,
args .data$args
) )
return(rcl) return(rcl)
......
version: '3'
services:
rstudio:
build: './docker/prairielearn'
image: 'soccer/studio'
container_name: 'soccer_rstudio'
environment:
TZ: 'Asia/Tokyo'
DISABLE_AUTH: 'true'
ports:
- '8787:8787'
volumes:
- '.:/home/rstudio/workspace'
# Alternative Rocker compatible with Apple M1
FROM prairielearn/workspace-rstudio:latest
# Change root user
USER 0
# Update package list
RUN apt-get update
# Install X11 library for ggplot2
RUN apt-get install -y libxt6
# Install Japanese fonts and language
RUN apt-get install -y \
fonts-ipaexfont \
fonts-noto-cjk \
language-pack-ja
# Set language to Japanese
ENV LC_ALL=ja_JP.UTF-8
ENV LANG=ja_JP.UTF-8
# Set locale to Japanese
RUN locale-gen ja_JP.UTF-8
# Install R packages
RUN /rocker_scripts/install_tidyverse.sh
RUN install2.r -e -s -n -1 \
conflicted \
&& rm -rf /tmp/downloaded_packages/ /tmp/*.rds
# Change rstudio user
USER rstudio
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/read_log.R
\name{read_log}
\alias{read_log}
\alias{read_rcg}
\alias{read_rcl}
\title{Read log file}
\usage{
read_log(path)
read_rcg(path)
read_rcl(path)
}
\arguments{
\item{path}{log file path}
}
\value{
Parsed tibble of log file in \code{path}
}
\description{
Read log file
}
\examples{
\dontrun{
rcg <- read_log("data/20220405162804-HELIOS_base_3-vs-enemy_2.rcg")
rcl <- read_log("data/20220405162804-HELIOS_base_3-vs-enemy_2.rcl")
}
}
Version: 1.0 Version: 1.0
RestoreWorkspace: Default RestoreWorkspace: No
SaveWorkspace: Default SaveWorkspace: No
AlwaysSaveHistory: Default AlwaysSaveHistory: Default
EnableCodeIndexing: Yes EnableCodeIndexing: Yes
...@@ -11,3 +11,12 @@ Encoding: UTF-8 ...@@ -11,3 +11,12 @@ Encoding: UTF-8
RnwWeave: Sweave RnwWeave: Sweave
LaTeX: pdfLaTeX LaTeX: pdfLaTeX
AutoAppendNewline: Yes
StripTrailingWhitespace: Yes
LineEndingConversion: Posix
BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd,collate,namespace
#' PLAYGROUND
#'
#' devtools::check() # check coding style and test functions
#' devtools::load_all() # load socceR
# make your functions or test cases
rcl <- read_log("tests/testthat/20220405162804-HELIOS_base_3-vs-enemy_2.rcl")
rcg <- read_log("tests/testthat/20220405162804-HELIOS_base_3-vs-enemy_2.rcg")
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment