Read, Manipulate, Explore Chess PGN Files and R API to UCI Chess Engines
The [bigchess
] package - Read, Manipulate, Explore Chess
PGN Files and R API to UCI Chess Engines
For installation you use:
install.packages("bigchess")
# Or
::install_github("rosawojciech/bigchess") devtools
Provides functions for reading *.PGN files with more than one game, including large files without copying it into RAM (using ‘ff’ package or ‘RSQLite’ package). Handle chess data and chess aggregated data, count figure moves statistics, create player profile, plot winning chances, browse openings. Set of functions of R API to communicate with UCI-protocol based chess engines.
Core function of this package was initially read.pgn(): ### read.pgn()
<- system.file("extdata", "2016_Candidates.pgn", package = "bigchess")
f <- read.pgn(f)
df # ...successfully imported 56 games...
# Example downloaded from https://www.pgnmentor.com/files.html#players and gzipped:
<- system.file("extdata", "Carlsen.gz", package = "bigchess")
f <- gzfile(f,encoding = "latin1")
con <- read.pgn(con,quiet = TRUE)
df # Fastest mode:
<- gzfile(f,encoding = "latin1")
con <- read.pgn(con,quiet = TRUE,n.moves = FALSE,extract.moves = FALSE,
df stat.moves = FALSE, ignore.other.games = FALSE)
# Parse additional tags and extract all moves:
<- gzfile(f,encoding = "latin1")
con <- read.pgn(con,add.tags = c("WhiteElo", "BlackElo", "ECO"),extract.moves = -1)
df # Example of direct downloading data from chess.com using API:
<- read.pgn("https://api.chess.com/pub/player/fabianocaruana/games/2013/03/pgn")
df # Warning of incomplete line could appear
# Example of scraping all of games given user:
<- "fabianocaruana"
user library("rjson")
<- paste0("https://api.chess.com/pub/player/",user,"/games/archives")
json_file <- fromJSON(paste(readLines(json_file), collapse=""))
json_data <- data.frame()
result for(i in json_data$archives)
<- rbind(result,read.pgn(paste0(i,"/pgn"))) result
In case of big files use read.pgn.ff() or read.pgn.db() instead of read.pgn():
require(ff)
require(ffbase)
<- system.file("extdata", "Carlsen.gz", package = "bigchess")
f <- gzfile(f,"rbt",encoding = "latin1")
con # options("fftempdir"="/path/"...) # if necessarily
<- read.pgn.ff(con,stat.moves = FALSE)
fdf delete(fdf)
# Works with all types of connections (also gz or zip files).
# con argument is passed directly to readLines(con,batch.size)
# so (if total number of lines to read is greater then batch.size)
# depending on platform use it correctly:
# Windows ('rb' opening mode for loop over readLines):
<- gzfile(system.file("extdata", "Carlsen.gz", package = "bigchess"),"rb",encoding = "latin1")
con # con <- file("path_to_big_chess_file.pgn","rb",encoding = "latin1")
<- read.pgn.ff(con)
fdf delete(fdf)
# Linux/Mac OS X ('r' opening mode for loop over readLines):
<- gzfile(system.file("extdata", "Carlsen.gz", package = "bigchess"),"r",encoding = "latin1")
con # con <- file("path_to_big_chess_file.pgn","r",encoding = "latin1")
<- read.pgn.ff(con)
fdf delete(fdf)
# Windows (example of zipped file handling)
<- unzip("zipped_pgn_file.zip")
unzf <- read.pgn.ff(file(unzf,"rb"))
fdf delete(fdf)
# Linux (make sure you have executable permission):
<- "./stockfish_10_x64"
engine_path # Windows
# engine_path <- "./stockfish_10_x64.exe"
<- uci_engine(engine_path)
e uci_quit(e)
# Using pipe '%>%' from magrittr:
require(magrittr)
uci_engine(engine_path) %>% uci_quit()
# Linux (make sure you have executable permission):
<- "./stockfish_10_x64"
engine_path # Windows
# engine_path <- "./stockfish_10_x64.exe"
<- uci_engine(engine_path)
e <- uci_go(depth = 10)
e uci_quit(e)
# Using pipe '%>%' from magrittr:
require(magrittr)
uci_engine(engine_path) %>% uci_go(depth = 10) %>% uci_quit()
# Find best answer for black after 1. e4 in 100 seconds:
uci_engine(engine_path) %>% uci_position(moves = "e2e4") %>%
uci_go(depth = 20) %>% uci_quit() %>% uci_parse()
# Find best answer for black after 1. e4 in 100 seconds:
uci_engine(engine_path) %>% uci_position(moves = "e2e4") %>%
uci_go(infinite = TRUE,stoptime = 100) %>% uci_quit() %>% uci_parse()
san2lan("1. e4 e5 2. Nf3 Nf5 3. d5 ")
lan2san("e2e4 c7c5")