library(rvest)
## Warning: package 'rvest' was built under R version 4.2.3
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.4.0 ✔ purrr 0.3.4
## ✔ tibble 3.1.8 ✔ dplyr 1.0.9
## ✔ tidyr 1.2.0 ✔ stringr 1.5.0
## ✔ readr 2.1.2 ✔ forcats 0.5.2
## Warning: package 'stringr' was built under R version 4.2.3
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ readr::guess_encoding() masks rvest::guess_encoding()
## ✖ dplyr::lag() masks stats::lag()
library(dplyr)
library(mongolite)
## Warning: package 'mongolite' was built under R version 4.2.3
library(DataExplorer)
library(ggplot2)
library(hrbrthemes)
## Warning: package 'hrbrthemes' was built under R version 4.2.3
## NOTE: Either Arial Narrow or Roboto Condensed fonts are required to use these themes.
## Please use hrbrthemes::import_roboto_condensed() to install Roboto Condensed and
## if Arial Narrow is not on your system, please see https://bit.ly/arialnarrow
library(RColorBrewer)
A caption
dengan pendefinisian url sebagai berikut:
url <- "https://www.chess.com/ratings"
link <- read_html(url)
Langkah selanjutnya yakni memeriksa elemen HTML pada situs tersebut menggunakan perintah inspect:
A caption
dan didapatkan bahwa data berada pada elemen table HTML, kemudian dilakukan perintah scrap sebagai berikut:
tabel <- link %>% html_nodes("table") %>% html_table() %>% as.data.frame()
Setelah didapatkan table data, kemudian dilakukan proses pembersihan untuk membentuk dataframe dengan elemen yang valid dengan perintah sebagai berikut:
tabel$Name <- str_squish(tabel$Name)
tabel_fix <- tabel[,-3]
tabel_fix$Rank <- substr(tabel_fix$Rank,nchar(tabel_fix$Rank)-3+1,nchar(tabel_fix$Rank))
tabel_fix$Scraping_Date <- Sys.time()
dan hasilnya sebagai berikut:
A caption
Setelah didapatkan data yang diharapkan, maka langkah selanjutnya yakni menyimpan data ke dalam perangkat MongoDB Cloud dengan bentuk NoSQL dalam format dokumen dan koleksi.
atlas <- mongo(collection = "scrapingchess",
db = "scrapingchess",
url = "mongodb+srv://teguhprasetyo08:T36uh#2023@cluster1.fevi6fb.mongodb.net/?retryWrites=true&w=majority"
)
Berikut perintah untuk menyimpan data ke MongoDB Cloud
atlas$insert(tabel_fix)
## List of 5
## $ nInserted : num 50
## $ nMatched : num 0
## $ nRemoved : num 0
## $ nUpserted : num 0
## $ writeErrors: list()
atlas$disconnect()
dengan format struktur dokumen sebagai berikut:
A caption
A caption
Adapun hasil screenshot pada koleksi dokumen MongoDB Cloud sudah memiliki kolom "Scraping_Date" namun karena keterbatasan tools view sehingga tidak dapat dilakukan screenshot untuk hasil scraping terupdate.
Langkah terakhir yakni melakukan analisis deskriptif sebagai pelengkap informasi dari data yang dilakukan scraping. Data tersebut merupakan data rating pemain catur di bawah naungan organisasi catur dunia FIDE. Rating catur tersebut dapt diakses secara live yang terdiri dari rating permainan tipe klasik (classical), cepat (rapid), dan kilat (blitz). Pada situs tersebut, update dilakukan secara harian dengan mengkalkulasi rating terbaru berdasarkan pertandingan FIDE resmi.
Untuk melakukan analisis deskripsi singkat, terlebih dahulu dataframe dibentuk dalam format long untuk memudahkan klasifikasi berdasarkan tipe permainan dengan perintah sebagai berikut:
tabel_eda <- reshape2::melt(tabel_fix, id.var=c('Rank','Name','Scraping_Date'), variable.name='Type')
str(tabel_eda)
## 'data.frame': 150 obs. of 5 variables:
## $ Rank : chr "#1" " #2" " #3" " #4" ...
## $ Name : chr "GM Magnus Carlsen" "GM Hikaru Nakamura" "GM Fabiano Caruana" "GM Ding Liren" ...
## $ Scraping_Date: POSIXct, format: "2023-06-11 15:53:38" "2023-06-11 15:53:38" ...
## $ Type : Factor w/ 3 levels "Classical","Rapid",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ value : int 2835 2787 2782 2780 2779 2777 2775 2769 2754 2752 ...
dengan hasil sebagai berikut:
A caption
# Basic Statistics
summary(tabel_eda)
## Rank Name Scraping_Date
## Length:150 Length:150 Min. :2023-06-11 15:53:38.45
## Class :character Class :character 1st Qu.:2023-06-11 15:53:38.45
## Mode :character Mode :character Median :2023-06-11 15:53:38.45
## Mean :2023-06-11 15:53:38.45
## 3rd Qu.:2023-06-11 15:53:38.45
## Max. :2023-06-11 15:53:38.45
## Type value
## Classical:50 Min. :2543
## Rapid :50 1st Qu.:2658
## Blitz :50 Median :2708
## Mean :2704
## 3rd Qu.:2744
## Max. :2896
# Spreadness
ggplot(tabel_eda, aes(x=Type, y=value, fill=Type)) +
geom_boxplot(alpha=0.3, width=0.1, color="black") +
geom_violin(alpha=0.3) +
theme(legend.position="none") +
scale_fill_brewer(palette="Dark2")
ggplot(tabel_eda, aes(x=value, fill=Type)) +
geom_density( color="#e9ecef", alpha=0.6, position = 'identity') +
scale_fill_manual(values=c("#69b3a2", "#404080", "#FFA500")) +
theme_ipsum() +
labs(fill="")
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family not
## found in Windows font database
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family not
## found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family not
## found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
# Aggregate
agregat <- aggregate(value ~ Name, data=tabel_eda, mean)
tabel_agregat <- agregat %>% arrange(desc(value))
View(tabel_agregat)
coul <- brewer.pal(5, "Set2")
barplot(tabel_agregat$value[1:3],names.arg=tabel_agregat$Name[1:3],col=coul)
Berdasarkan boxplot dan visualisasi grafik sebaran tersebut diketahui bahwa sebaran rating tipe permainan klasik cenderung homogen dibandingkan rapid dan blitz. Hal tersebut disebabkan bahwa untuk tipe permainan catur klasik menggunakan kontrol waktu yang jauh lebih lama (1 jam lebih) dibanding tipe permainan lainnya, sehingga pecatur dapat memanfaatkan waktu yang lama tersebut untuk menemukan langkah terbaik dalam permainan catur dan meminimalisir kesalahan. Saat ini jika dilakukan perhitungan rata-rata rating tiap tipe permainan, maka Grand Master Magnus Carlsen menjadi pemain catur dengan rating tertinggi, disusul oleh Grand Master Alireza Firouzja dan Hikaru Nakamura.