Skip to content

Instantly share code, notes, and snippets.

@fidadoma
Created October 1, 2019 14:48
Show Gist options
  • Save fidadoma/2a7a8351b8dcfaebef3f2335e67f771c to your computer and use it in GitHub Desktop.
Save fidadoma/2a7a8351b8dcfaebef3f2335e67f771c to your computer and use it in GitHub Desktop.
Script for downloading data from osf
# when data are in private repo on osf, we want to download them prior to the analysis
# load tidyverse
library(tidyverse)
# install package to communicate with osf.io. Check the github repo for guidelines
library(osfr)
# function to download data using dplyr do() function
download_files <- function(df, local_data_pth, should_overwrite = T) {
# we need to set correct class as the current version of osfr does not works with dplyr properly
class(df) <- c("osf_tbl_file","osf_tbl", class(df))
df %>%
rowwise() %>%
do(osf_retrieve_file(.$id) %>%
osf_download(path = file.path(local_data_pth, .$name),
overwrite = should_overwrite))
}
# set local data path
local_data_pth <- file.path("data")
# You need to add your PAT key into separate file. This line is used for authentication
osf_auth(token = read_lines("osf_token_write_fd.txt"))
# add guid of your repo
data_guid <- "XXX"
osf_project <- osf_retrieve_node(data_guid)
# create local structure for data
if(!dir.exists(local_data_pth)) {
dir.create(local_data_pth,recursive = T)
}
# select the files that are nested in osf structure
data_files <-
osf_project %>%
osf_ls_files() %>%
filter(name == "directory1") %>%
osf_ls_files() %>%
filter(name == "directory2") %>%
osf_ls_files()
# and download files locally
data_files %>%
do(download_files(.,local_data_pth))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment