bruigtp / REDCapDM

R package to perform data management of REDCap datasets in R
https://bruigtp.github.io/REDCapDM/
Other
4 stars 0 forks source link

Issue with redcap_data() function #7

Closed biostatisticsfun closed 2 months ago

biostatisticsfun commented 2 months ago

Hello, I am having an issue with the redcap_data() function.

1) Using redcap_data() through API, I'm receiving this error:

Importing in progress...
Almost done...                                                                                                                         
Error in factor(data_api[[i]], levels = c(var_radio$levels[[which(var_radio$field_name %in%  :                                         
  invalid 'labels'; length 6 should be 1 or 5
In addition: Warning message:
There were 10 warnings in `dplyr::mutate()`.
The first warning was:
ℹ In argument: `levels = purrr::map(factor, ~gsub("^(\\d+),.*", "\\1", .x) %>% as.numeric)`.
Caused by warning in `gsub("^(\\d+),.*", "\\1", .x) %>% as.numeric`:
! NAs introduced by coercion

2) Then I tried using redcap_data() with local files and received this error:

Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
  cannot open file 'file.csv': No such file or directory

It looks like it is not reading in the dictionary file and I can read in the dictionary csv with read.csv() too.

Thank you for any support.

Best, Andrew

sessioninfo sessionInfo()

R version 4.3.3 (2024-02-29 ucrt) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 19044)  Matrix products: default   locale: [1] LC_COLLATE=English_United States.utf8  LC_CTYPE=English_United States.utf8    [3] LC_MONETARY=English_United States.utf8 LC_NUMERIC=C                           [5] LC_TIME=English_United States.utf8      time zone: America/New_York tzcode source: 
internal  attached base packages: [1] stats     graphics  grDevices utils     datasets  methods   base       other attached packages:  [1] remotes_2.5.0     Hmisc_5.1-1       labelled_2.12.0   stringi_1.8.3     tidyselect_1.2.1   
[6] tibble_3.2.1      stringr_1.5.1     rlang_1.1.3       REDCapR_1.1.0     purrr_1.0.2       [11] openxlsx_4.2.5.2  magrittr_2.0.3    janitor_2.2.0     REDCapDM_0.9.8   
 httr_1.4.7        [16] ggbeeswarm_0.7.2  lubridate_1.9.3   gt_0.10.1         gtsummary_1.7.2   excel.link_0.9.12 [21] tidyr_1.3.1     
  dplyr_1.1.4       ggplot2_3.5.0      loaded via a namespace (and not attached):  [1] vipor_0.4.7          fastmap_1.1.1        broom.helpers_1.14.0 digest_0.6.34         [5] rpart_4.1.23         timechange_0.3.0     lifecycle_1.0.4      cluster_2.1.6         [9] processx_3.8.4       compiler_4.3.3       tools_4.3.3          utf8_1.2.4    
       [13] yaml_2.3.8           data.table_1.14.10   knitr_1.46           htmlwidgets_1.6.4    [17] bit_4.0.5            pkgbuild_1.4.4       curl_5.2.1           xml2_1.3.6           [21] pkgload_1.3.4        withr_3.0.0          foreign_0.8-86       desc_1.4.3           [25] nnet_7.3-19          grid_4.3.3           fansi_1.0.6          colorspace_2.1-0     [29] scales_1.3.0         cli_3.6.2            rmarkdown_2.26       crayon_1.5.2         [33] generics_0.1.3       rstudioapi_0.15.0    tzdb_0.4.0           parallel_4.3.3       [37] base64enc_0.1-3      vctrs_0.6.5          jsonlite_1.8.8       callr_3.7.6          [41] hms_1.1.3            bit64_4.0.5          Formula_1.2-5        htmlTable_2.4.2      [45] beeswarm_0.4.0       glue_1.7.0           ps_1.7.6             gtable_0.3.4         [49] munsell_0.5.1
        pillar_1.9.0         htmltools_0.5.7      R6_2.5.1             [53] vroom_1.6.5          evaluate_0.23        haven_2.5.4          readr_2.1.5          [57] backports_1.4.1      snakecase_0.11.1     Rcpp_1.0.12          zip_2.3.1            [61] gridExtra_2.3        checkmate_2.3.1      xfun_0.43            forcats_1.0.0        [65] pkgconfig_2.0.3
jcarmezim commented 2 months ago

Hello Andrew,

Thank you for bringing this issue to our attention. We will take a look into the first error. Regarding the second error, in order to read local files using redcap_data(), all files must be located in the same directory. Could you please confirm this is the case?

Thank you, João

biostatisticsfun commented 2 months ago

Hi João,

Thank you for your reply. For the second error, yes I tried 2 things:

  1. I store the files in a separate path and specified the path.
  2. I stored the files in the same directory

Both options gave "No such file or directory". I'm also able to read the files in directly with read.csv() function in both cases so I know I'm specifying the right path in both cases.

Best, Andrew

jcarmezim commented 2 months ago

Regarding the first error:

Regarding the second error:

The data_path argument is refering to the R file with the code to read the data and the dictionary path argument to the csv file with the dictionary.

Thank you once again.

Best regards, João

biostatisticsfun commented 2 months ago

Hi João,

Please see attachment and I modified choices with sensitive information.

For the second error, I figured out that the .R file specifies a path to the dataset (csv) and when I changed this, the code worked. I see now that the REDCap export to R provides the .R file and csv dataset. Apologies if this was obvious, I am new to REDCap. Test.xlsx

Best, Andrew

jcarmezim commented 2 months ago

Good morning Andrew,

Perfect, thanks for sharing the file. The problem was caused by some of the radio button options starting with words instead of numbers (e.g. the xx, Other option of the race variable). We had only worked with options starting with numbers until now. We have adapted the redcap_data() function to handle both formats. We are committing a new version of the package that fixes this issue to github, which you can download using: remotes::install_github('bruigtp/REDCapDM')

Could you please try this new version and read the data using the API connection to confirm that the problem has been resolved?

Regarding the second error, we acknowledge that the arguments in the redcap_data() function are not explicit, so we have also added a warning in case someone uses another file type that is not an R file, indicating that the only supported type is R files.

Thank you very much for helping us improve the package, and if the problem is solved, we would be grateful if you would let us know and close this issue.

Best regards, João

biostatisticsfun commented 2 months ago

Hi João,

Thank you so much for working on this issue! Yes, I have re-installed and the API command works!

For the second error, it may be helpful for newer REDCap users (like me) to indicate that 3 files are needed (.R, data csv, dictionary csv) in the redcap_data() function (while the function only calls upon .R and dictionary csv files).

Thank you again for working on this issue and making the workflow between REDCap and R a great experience!

Best, Andrew