bergant / rapiclient

Dynamic Open API (Swagger) Client for R
Other
62 stars 18 forks source link

OpenTrials API > Error in pieces[[1]] : subscript out of bounds #1

Closed ataiprojects closed 7 years ago

ataiprojects commented 7 years ago

Hi! I've tried to test rapiclient on OpenTrials API: ot_api <- get_api(url = "http://api.opentrials.net/v1/swagger.yaml") operations <- get_operations(ot_api) schemas <- get_schemas(ot_api) result = operations$getTrial(id = "a5d82296-8cb8-11e6-988b-0242ac12000c") Though the list of functions is loaded correctly, I get: Error in pieces[[1]] : subscript out of bounds. Other functions, such as result = operations$searchTrials(q='depression', per_page=10), result in the same error. What could it be?

bergant commented 7 years ago

Thank you for testing the package! But, I'm afraid I can't reproduce the error. However, I noticed that there is no host definition in the OpenTrials API swagger file - so you have to add it to the api object (ot_api$host <- "api.opentrials.net") before calling the get_operations function:

library(rapiclient)
ot_api <- get_api(url = "http://api.opentrials.net/v1/swagger.yaml") 
ot_api$host <- "api.opentrials.net"
operations <- get_operations(ot_api ) 

result = operations$getTrial(id = "a5d82296-8cb8-11e6-988b-0242ac12000c")
trial <- httr::content(result)

This is what I get:

str(trial, max.level = 1)    

# List of 22
#  $ id                   : chr "a5d82296-8cb8-11e6-988b-0242ac12000c"
#  $ source_id            : chr "nct"
#  $ identifiers          :List of 1
#  $ public_title         : chr "Brain-oscillation Synchronised Stimulation of the Prefrontal Cortex"
#  $ brief_summary        : chr "Combining TMS and EEG, this study investigates a personalized therapeutic non-invasive brain\n      stimulation protocol in pat"| __truncated__
#  $ target_sample_size   : int 18
#  $ gender               : chr "both"
#  $ has_published_results: logi FALSE
#  $ status               : chr "ongoing"
#  $ recruitment_status   : chr "recruiting"
#  $ registration_date    : chr "2016-09-26T00:00:00.000Z"
#  $ locations            :List of 1
#  $ interventions        :List of 3
#  $ conditions           :List of 1
#  $ persons              :List of 2
#  $ organisations        : list()
#  $ records              :List of 1
#  $ publications         : list()
#  $ documents            : list()
#  $ risks_of_bias        : list()
#  $ url                  : chr "http://api.opentrials.net/v1/trials/a5d82296-8cb8-11e6-988b-0242ac12000c"
#  $ sources              :List of 1

Is it possible that the pieces object is defined somewhere in your code? It could be that you ignored the error (when rapiclient couldn't resolve the host) and you proceeded to read empty result.

ataiprojects commented 7 years ago

Looks like pieces came from elsewhere, but I couldn't see that the host wasn't resolved. Now it all works wonders. Thank you!

bergant commented 7 years ago

Well, it was also a bug in rapiclient. According to swagger specs when host is not specified, the host of API spec should be used.

Now corrected in new version.

ataiprojects commented 7 years ago

Great. Also tagging @vitorbaptista of OpenTrials here; to show it's been tested and works.

vitorbaptista commented 7 years ago

Thanks @bergant and @ataiprojects. Happy it's working now :beers:

bergant commented 7 years ago

Here is a short demo, how to use OpenTrials API with R: https://bergant.github.io/OpenTrialsRDemo/

ataiprojects commented 7 years ago

@bergant I take my hat off.