Pārlūkot izejas kodu

add download script from khondula

George C. Privon 6 gadi atpakaļ
vecāks
revīzija
c910549739
2 mainītis faili ar 25 papildinājumiem un 0 dzēšanām
  1. 24 0
      code/download-data.R
  2. 1 0
      data/README.md

+ 24 - 0
code/download-data.R

@@ -0,0 +1,24 @@
+library(rvest)
+
+# link to data
+webpage <- read_html("https://eightthirtyfour.com/data")
+
+datalinks <-  webpage %>% 
+  html_nodes(xpath = '//*[contains(concat( " ", @class, " " ), concat( " ", "centertable", " " ))]') %>% 
+    html_table(fill = TRUE)
+
+# base url for links
+base_url <- "https://eightthirtyfour.com/nba/pbp/"
+
+filenames <- datalinks[[1]] %>% 
+  dplyr::filter(Season != "all seasons (zip)") %>%
+  dplyr::pull(CSV)
+
+download_links <- paste0(base_url, filenames)
+
+if(!"data" %in% list.files()) {fs::dir_create("data")}
+
+purrr::map2(.x = download_links,
+            .y = paste0("../data/", filenames),
+            ~download.file(.x, .y))
+

+ 1 - 0
data/README.md

@@ -1 +1,2 @@
 Data are available in csv format from [Eight Thirty Four](https://eightthirtyfour.com/data), provided by Katherine Evans and Udam Saini.
 Data are available in csv format from [Eight Thirty Four](https://eightthirtyfour.com/data), provided by Katherine Evans and Udam Saini.
+They can be automatically downloaded by running the `download-data.R` script in the `code/` directory.