Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Record names that do not comply to the REDCapR pattern #357

Closed
AlexBalhuizen opened this issue Oct 14, 2021 · 5 comments
Closed

Record names that do not comply to the REDCapR pattern #357

AlexBalhuizen opened this issue Oct 14, 2021 · 5 comments
Assignees

Comments

@AlexBalhuizen
Copy link

AlexBalhuizen commented Oct 14, 2021

Hi,
I tried to use REDCapR_write and REDCapR_write_oneshot to batch upload pdfs.
Each file has the record id as its filename. When I use the REDcap playground code or redcapAPI it works, but here it is only one file per time.
The record names are uniquely numbered as: ABC123-X01B-M01 as they are specific for sampling sites, occasions, and vial numbers.

When I run the code it uses ABC123, and if I do "" as "ABC123-X01B-M01", it returns an error.

REDCapR_write
_#Define some constants
uri <- "https://redcapxxx/api/"
token <- "TOKEN" # The simple project -pid
field <- "portion_files_fj"
event <- "sorting_arm_1'" # only for longitudinal events
#Upload a single image file.
record <- "ABC123-X01B-M01"
file_path <- system.file("C://FJpdfs//ABC123-X01B-M01.pdf", package="REDCapR")

It gives me the following error:

Error in REDCapR::redcap_upload_file(file_name = file_path, record = record, : Assertion on 'file_name' failed: Must comply to pattern '^.{1,}$'.

Is it possible to run batch uploading with these record names?
I

@AlexBalhuizen AlexBalhuizen changed the title I Record names that do not comply to REDcapR pattern Oct 14, 2021
@AlexBalhuizen AlexBalhuizen changed the title Record names that do not comply to REDcapR pattern Record names that do not comply to the REDcapR pattern Oct 14, 2021
@wibeasley wibeasley changed the title Record names that do not comply to the REDcapR pattern Record names that do not comply to the REDCapR pattern Oct 15, 2021
@wibeasley wibeasley self-assigned this Oct 15, 2021
@wibeasley
Copy link
Member

Thanks for explaining it, @AlexBalhuizen. I'm not sure how REDCapR::redcap_write() is related to this, so for now I'll concentrate on the error message that the file names aren't complying.

I'm stumped because that is a very permissive regex. It's requiring only that the string has at least one character.

checkmate::assert_character(file_name , any.missing=FALSE, len=1, pattern="^.{1,}$")

In isolation, that file name passes. Can you double check that is the error message associated with the ABC123... file?

checkmate::assert_character("C://FJpdfs//ABC123-X01B-M01.pdf", any.missing=FALSE, len=1, pattern="^.{1,}$")

If that doesn't solve it, please make sure REDCapR is the latest version and run the documentation example for REDCapR::redcap_upload_file_oneshot().

If that passes, please provide more information about the error with your dataset, such as traceback().

@AlexBalhuizen
Copy link
Author

I tried a couple of versions of the record name. It seems there are two problems one is that I use a dash - in the record name. Due to this, I have to use single quotes or double quotes to have R use the full record name, but it seems then that redcapR doesn't work with either single quotes or double quotes to define the record name.

For example:
If I run a quoted "P" as record
record <- "P"
Error in REDCapR::redcap_upload_file(file_name = file_path, record = record, : Assertion on 'file_name' failed: Must comply to pattern '^.{1,}$'.

While redcapr processes everything without quotes or dashes, e.g. with underscore instead of dash. Here, error not found as I did run it without a token.

record <- ABC0123
Error: object 'ABC0123' not found
#Upload a single image file.
record <- ABC0123-X01B-M01
Error: object 'ABC0123' not found
record <- ABC0123_X01B_M01
Error: object 'ABC0123_X01B_M01' not found
record <- "ABC0123_X01B_M01"

If I run checkmate it returns empty.

@wibeasley
Copy link
Member

wibeasley commented Oct 28, 2021

My guess is that we shouldn't worry about record now. The file_path argument is throwing the error.

Can you peek at the value your passing to file_path first? If it's like the code you've included in the first post, there's no file there, so system.file() returns a zero-length character. That's why it's failing the regex (that requires only that the file path have at least one character).

file_path <- system.file("C://FJpdfs//ABC123-X01B-M01.pdf", package="REDCapR")

message("File path : `", file_path, "`")
#> File path : ``

checkmate::assert_file_exists(file_path)
#> Error in eval(ei, envir) : 
#>   Assertion on 'file_path' failed: File does not exist: ''.

That first line (that I copied from your post) is asking for the "C:/...pdf" file to be found in the REDCapR directory. I don't think that's what you mean to do though.

@AlexBalhuizen
Copy link
Author

Thanks, yes it works to remove the system.files( and package="REDCapR"). and only keeping the path.
I will then need to figure out how to generate the records from the file names.

@wibeasley
Copy link
Member

I'm going to close this, since it no longer seems like a REDCap API issue.

Here's a nudge towards "how to generate the records from the file names"

I like the fs package. This will strip off the directory and the extension from the full path.

file_path <- "C://FJpdfs//ABC123-X01B-M01.pdf"

fs::path_ext_remove(fs::path_file(file_path))
#> "ABC123-X01B-M01"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants