Bigchess – R package to analyze chess database files

 

Standard import:

library(bigchess)
f <- system.file("extdata", "2016_Candidates.pgn", package = "bigchess")
df <- read.pgn(f)
## 2018-09-25 19:58:54, successfully imported 56 games
## 2018-09-25 19:58:54, N moves computed
## 2018-09-25 19:58:54, extract moves done
## 2018-09-25 19:58:54, stat moves computed
# ...successfully imported 56 games...

# Example downloaded from https://www.pgnmentor.com/files.html#players and gzipped:
f <- system.file("extdata", "Carlsen.gz", package = "bigchess")
con <- gzfile(f,encoding = "latin1")
df <- read.pgn(con,quiet = TRUE)
# Fastest mode:
con <- gzfile(f,encoding = "latin1")
df <- read.pgn(con,quiet = TRUE,n.moves = FALSE,extract.moves = FALSE,
stat.moves = FALSE, ignore.other.games = FALSE)
# Parse additional tags and extract all moves:
con <- gzfile(f,encoding = "latin1")
df <- read.pgn(con,add.tags = c("WhiteElo", "BlackElo", "ECO"),extract.moves = -1)
## 2018-09-25 19:58:57, successfully imported 2410 games
## 2018-09-25 19:58:57, N moves computed
## 2018-09-25 19:58:58, extract moves done
## 2018-09-25 19:58:59, stat moves computed
# Example of direct downloading data from chess.com using API:
df <- read.pgn("https://api.chess.com/pub/player/fabianocaruana/games/2013/03/pgn")
## Warning in readLines(con): niekompletna końcowa linia znaleziona w
## 'https://api.chess.com/pub/player/fabianocaruana/games/2013/03/pgn'
## 2018-09-25 19:59:01, successfully imported 567 games
## 2018-09-25 19:59:01, N moves computed
## 2018-09-25 19:59:01, extract moves done
## 2018-09-25 19:59:01, stat moves computed
# Warning of incomplete line could appear

# Example of scraping all of games given user:
user <- "fabianocaruana"
library("rjson")
json_file <- paste0("https://api.chess.com/pub/player/",user,"/games/archives")
json_data <- fromJSON(paste(readLines(json_file), collapse=""))
## Warning in readLines(json_file): niekompletna końcowa linia znaleziona w
## 'https://api.chess.com/pub/player/fabianocaruana/games/archives'
result <- data.frame()
options(warn=-1)
for(i in json_data$archives)
result <- rbind(result,read.pgn(paste0(i,"/pgn"),quiet = T))

Analyze best Kasparov games openings:

f <- system.file("extdata", "Kasparov.gz", package = "bigchess")
con <- gzfile(f,encoding = "latin1")
df <- read.pgn(con,quiet = TRUE,ignore.other.games = TRUE,stat.moves = FALSE)

bo <- browse_opening(subset(df,grepl("Kasparov",White)))
plot of chunk unnamed-chunk-2
# Analyze 'best' answer to Kasparov Ruy Lopez:
bo <- browse_opening(subset(df,grepl("Kasparov",White)),"1.e4 e5 2.Nf3 Nc6 3.Bb5")
plot of chunk unnamed-chunk-2
# Analyze best answer to "1.e4 e5 2.Nf3" in aggregated data
browse_opening(FirstTwoMoves,"1.e4 e5 2.Nf3")
plot of chunk unnamed-chunk-2
##     B2 White_score Draws_percent Black_score      N
## 11 Nc6        35.9          37.5        26.6 166167
## 13 Nf6        38.0          43.3        18.7  21834
## 8   d6        42.4          29.8        27.8   7759
## 9   f5        54.3          19.7        26.0    753
## 7   d5        52.0          22.2        25.7    369
## 14 Qe7        46.7          30.8        22.5    347
## 3  Bc5        66.7          16.7        16.7     12
## 10  f6        80.0           0.0        20.0     10
## 1   a6        66.7          16.7        16.7      6
## 15 Qf6       100.0           0.0         0.0      4
## 6   c6        37.5          25.0        37.5      8
## 4  Bd6       100.0           0.0         0.0      2
## 2   b6         0.0           0.0       100.0      1
## 5   c5         0.0         100.0         0.0      1
## 12 Ne7         0.0         100.0         0.0      1
f <- system.file("extdata", "Kasparov.gz", package = "bigchess")
con <- gzfile(f,encoding = "latin1")
df <- read.pgn(con,quiet = TRUE,stat.moves = FALSE)
tr <- tree_move(subset(df,W1=="e4"),"B1")
plot_tree_move(tr,"1. e4 ... ?")
plot of chunk unnamed-chunk-3
# Plot tree move openings in aggregated data
tr <- tree_move(FirstTwoMoves,"W1")
plot_tree_move(tr,paste0("1. ... ?\n",sum(FirstTwoMoves$Freq)," total games"))
plot of chunk unnamed-chunk-3

Package rchess integration

library(rchess)
library(bigchess)

f <- system.file("extdata", "Carlsen.gz", package = "bigchess")
con <- gzfile(f,encoding = "latin1")
df <- read.pgn(con,quiet = TRUE)

# load and plot first game in data frame
chsspgn <- Chess$new()
chsspgn$load_pgn(df[1,"Movetext"])
## [1] TRUE
plot(chsspgn)
plot of chunk unnamed-chunk-3