Appendix A — Download and Upload Files From and To OSF

A.1 Overview

This notebook aim to help download or upload files to OSF.

A.2 Set the Environment

A.2.1 Load Packages

A.2.2 Set Data Directories

Code
data_dir <- here("data")
Code
if (!dir_exists(data_dir)) {
  dir_create(data_dir, recurse = TRUE)
}

A.2.3 Set Keys

Code
osf_auth(Sys.getenv("OSF_PAT")) # askpass()
Code
gs4_auth(cache = ".secrets")
Code
private_key <- here::here("_ssh", "id_rsa")
Code
public_key <- here::here("_ssh", "id_rsa.pub")
Code
password <- Sys.getenv("PREGNANCY_PASSWORD") # askpass()
Code
salt <- Sys.getenv("PREGNANCY_SALT") # askpass()

A.2.4 Set OSF IDs

Code
osf_pilot_data_id <- "tj5u2"
osf_raw_data_id <- "7kg34"
osf_processed_data_id <- "a2dsw"
osf_bundles_id <- "fvh4u"
osf_tidy_data_id <- "npkjw"
osf_feedbacks_id <- "8hs7v"
Code
for (i in ls(pattern = "^osf_.*_id$")) {
  assign(
    x = i,
    value = paste0("https://osf.io/", get(i)),
    envir = .GlobalEnv
  )
}

A.3 Download Metadata for Pilot Data Files

A.4 Download Metadata for Raw Actigraphy Files

A.5 Download Metadata for Processed Actigraphy Files

A.5.1 Set Data Subdirectory

Code
data_dir <- here("data", "processed", "actigraphy")
Code
if (!dir_exists(data_dir)) {
  dir_create(data_dir, recurse = TRUE)
}

A.5.2 Retrieve Files Metadata

Code
osf_files <-
  osf_processed_data_id |>
  osf_retrieve_node() |>
  osf_ls_files(n_max = Inf) |>
  filter(name == "actigraphy") |>
  pull(id) |>
  osf_retrieve_file() |>
  osf_ls_files(n_max = Inf) |>
  filter(str_detect(name, "actigraphy-processed-data"))

osf_files

A.6 Download Metadata for Bundle Files

A.7 Download Metadata for Tidy Actigraphy Files

A.8 Download Metadata for Feedback Files

A.9 Download Files

Code
files <-
  osf_files |>
  osf_download(
    path = data_dir,
    conflicts = "overwrite",
    progress = TRUE
  ) |>
  extract2("local_path") |>
  here()

A.10 Unlock Files

Code
unlocked_files <- character()

for (i in files) {
  # if (!file_exists(i)) next

  i_path <-
    i |>
    unlock_file(
      private_key = private_key,
      suffix = ".lockr",
      remove_file = TRUE,
      password = password
    )

  cat_line()

  unlocked_files <- c(unlocked_files, i_path)
}

A.11 Upload Files

Code
osf_id <- osf_bundles_id
Code
# Create the README.md file

readme_file <- tempdir() |> file.path("README.md")

c(
  "# {pregnancy}",
  "",
  "* All files were locked with the project's public key. Ask the project administrators for the key if you need access.",
  "",
  paste0("* Last update: ", lubritime::round_time(Sys.time()), ".")
) |>
  readr::write_lines(readme_file)

readr::read_lines(readme_file)
Code
osf_id |>
  osf_retrieve_node() |>
  osf_upload(
    path = readme_file,
    conflicts = "overwrite",
    progress = TRUE
  )
Code
path <- NULL # Use `NULL` to refer to the root.
Code
osf_data <-
  osf_id |>
  osf_retrieve_node() |>
  osf_ls_files(
    path = path,
    n_max = Inf
  ) |>
  dplyr::arrange(name)

osf_data
Code
dir_path <- read_clip() |> normalizePath("/", mustWork = FALSE)
Code
files <-
  list.files(dir_path) |>
  paste0(".lockr") |>
  setdiff(osf_data$name) |>
  stringr::str_subset("^README", negate = TRUE) |>
  stringr::str_remove("\\.lockr$")

# files <- list.files(dir_path)

files
Code
file.path(dir_path, files)|>
  vapply( prettycheck:::test_file_exists, logical(1)) |>
  unname()
Code
files |> write_clip()
Code
new_files <- character()

for (i in seq_along(files)) {
  i_from <- file.path(dir_path, files[i])
  i_to <- file.path(tempdir(), files[i])

  test <- file.copy(from = i_from, to = i_to, overwrite = TRUE)

  if (isTRUE(test)) new_files <- c(new_files, i_to)
}

locked_files <- character()

for (i in new_files) {
  i_path <- lock_file(
    i,
    public_key = public_key,
    remove_file = TRUE
  )

  cat_line()

  locked_files <- c(locked_files, i_path)
}
Code
osf_id |>
  osf_retrieve_node() |>
  osf_upload(
    path = locked_files,
    conflicts = "overwrite",
    progress = TRUE
  )

A.12 Lock Files

Code
for (i in unlocked_files) {
  # if (!file_exists(i)) next

  i |>
    lock_file(
      public_key = public_key,
      suffix = ".lockr",
      remove_file = TRUE
  )

  cat_line()
}