cjbarrie/academictwitteR

[BUG] `create_data_dir` - allow recurisve path creation

TimBMK opened this issue · 1 comments

Please confirm the following

  • I have searched the existing issues
  • The behaviour of the program is deviated from what is described in the documentation.
  • I can reproduce this problem for more than one time.
  • This is NOT a 3-digit error -- it does not display an error message like something went wrong. Status code: 400.
  • This is a 3-digit error and I have consulted the Understanding API errors vignette and the suggestions do not help.

Describe the bug

This is a minor issue, but I'm wondering if it is on purpose that the create_data_dir utils function does not allow recursive path creation. That is, if you want to create more than one folder with the data_path functionality in, say, get_all_tweets, you will run into an error. This error can be somewhat confusing to the user, since it throws a writeLines - cannot open the connection error.

Expected Behavior

I would expect the function to create sub folders as needed. It would easily be fixed by setting recursive = TRUE in create_data_dir()'s dir.create() function. I am wondering, however, if recursive is left to FALSE on purpose to prevent some other issue?

Steps To Reproduce

The call on any function utilizing the directory creation of create_data_dir(), such as get_all_tweets, will fail if it needs to create more than one directory. In this example, it fails if dir.exists() is FALSE for both the folders test_2 and more_test

dir.exists("test/")

dir.exists("test/test_2/")

dir.exists("test/test_2/more_test") 

get_all_tweets(query = "hello", start_tweets = "2021-12-01T00:00:00Z", end_tweets = "2021-12-15T00:00:00Z", n = 1, data_path = "test/test_2/more_test", bind_tweets = T, bearer_token = bearer_token)

Environment

R version 4.1.0 (2021-05-18)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042)

Matrix products: default

locale:
[1] LC_COLLATE=German_Germany.1252 LC_CTYPE=German_Germany.1252 LC_MONETARY=German_Germany.1252 LC_NUMERIC=C LC_TIME=German_Germany.1252

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] forcats_0.5.1 stringr_1.4.0 dplyr_1.0.7 purrr_0.3.4 readr_1.4.0 tidyr_1.1.3 tibble_3.1.3
[8] ggplot2_3.3.5 tidyverse_1.3.1 academictwitteR_0.2.1

loaded via a namespace (and not attached):
[1] Rcpp_1.0.7 cellranger_1.1.0 pillar_1.6.2 compiler_4.1.0 dbplyr_2.1.1 tools_4.1.0 lubridate_1.7.10 jsonlite_1.7.2 lifecycle_1.0.0
[10] gtable_0.3.0 pkgconfig_2.0.3 rlang_0.4.11 reprex_2.0.0 cli_3.0.1 rstudioapi_0.13 DBI_1.1.1 curl_4.3.1 haven_2.4.1
[19] xml2_1.3.2 withr_2.4.2 httr_1.4.2 hms_1.1.0 generics_0.1.0 fs_1.5.0 vctrs_0.3.8 grid_4.1.0 tidyselect_1.1.1
[28] glue_1.4.2 R6_2.5.0 fansi_0.5.0 readxl_1.3.1 modelr_0.1.8 magrittr_2.0.1 backports_1.2.1 scales_1.1.1 ellipsis_0.3.2
[37] usethis_2.0.1 rvest_1.0.0 assertthat_0.2.1 colorspace_2.0-2 utf8_1.2.2 stringi_1.7.3 munsell_0.5.0 broom_0.7.6 crayon_1.4.1

Anything else?

The fixed create_data_dir() function would look like this:

create_data_dir <- function(data_path, verbose = TRUE){
    #create folders for storage
    if (dir.exists(file.path(data_path))) {
      .vwarn(verbose, "Directory already exists. Existing JSON files may be parsed and returned, choose a new path if this is not intended.")
      invisible(data_path)
    }
    dir.create(file.path(data_path), showWarnings = FALSE, recursive = TRUE)
    invisible(data_path)  
  }

@TimBMK Sorry for the late reply. I think most people would expect the data_path can also accept a deep path. Your suggestion makes a lot of sense. And I will implement it (and of course with a lot of tests).