Skip to content

Commit 6596a40

Browse files
authored
Merge pull request #44 from bcbi/dev
Add docstrings
2 parents b5cbc00 + 60f7651 commit 6596a40

22 files changed

+854
-185
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "REDCap"
22
uuid = "ba918724-fbf9-5e4a-a61c-87e95654e718"
33
authors = ["Cory Cothrum", "Dilum Aluthge <[email protected]>", "Ashlin Harris <[email protected]>"]
4-
version = "2.4.0"
4+
version = "2.5.0"
55

66
[deps]
77
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"

README.md

Lines changed: 8 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -6,112 +6,23 @@ REDCap.jl is an API wrapper for REDCap v14, written in Julia.
66

77
## Examples
88
```julia
9-
using REDCap
9+
pkg> activate --temp; add REDCap
10+
julia> using REDCap
1011

11-
export_version()
12+
julia> export_version()
1213

13-
project_token = create_project(
14+
julia> project_token = create_project(
1415
data = (project_title = "Test Project", purpose = 0),
1516
odm = "Data_Dictionary.xml")
1617

17-
import_records(token=project_token, data="example.csv", format=:csv)
18+
julia> import_records(token=project_token, data="example.csv", format=:csv)
1819

19-
delete_records(token=project_token, records=[2,3])
20+
julia> delete_records(token=project_token, records=[2,3])
2021

21-
export_logging(token=project_token)
22+
julia> export_logging(token=project_token)
2223
```
2324

24-
## Syntax
25-
Each REDCap method accepts a number of parameters that follow a shared naming convention.
26-
REDCap.jl is designed to closely follow the design and syntax patterns of REDCap.
27-
Every REDCap API method is available as a function that supplies certain required parameters and checks user inputs for validity.
28-
29-
Return values and REDCap messages are returned as Strings directly, but the documentation shows how these can be parsed in useful ways.
30-
31-
Function arguments are named after REDCap method parameters.
32-
These are passed as named arguments and take values with intuitive types.
33-
34-
### `token` and `url`
35-
Nearly all REDCap methods accept a token that is unique to the project and user.
36-
37-
The URL must exactly match this example:
38-
```https://example.example/redcap/api/```
39-
40-
Your REDCap token and your institution's REDCap API URL can be read by default from Julia's environment variables.
41-
You can make them avaiable to REDCap.jl by putting the following lines in [your local Julia startup file](https://docs.julialang.org/en/v1/manual/command-line-interface/#Startup-file) (probably `~/.julia/config/startup.jl`):
42-
```julia
43-
ENV["REDCAP_API_TOKEN"] = "C0FFEEC0AC0AC0DEC0FFEEC0AC0AC0DE"
44-
ENV["REDCAP_API_URL"] = "http://example.com/redcap/api/"
45-
```
46-
They can also be passed as ordinary named arguments.
47-
48-
A few methods accept a super token, including `create_project`, which can be used to generate a project and project-level token.
49-
If you have a super token, you might wish to keep that in your startup file, generating and saving project-level tokens as needed.
50-
51-
### `data`
52-
The `data` parameter contains a list of attributes.
53-
In REDCap.jl, this can be a NamedTuple (or any derived type), a file handle, or a String.
54-
If you use a NamedTuple, it will be translated internally into whatever `format` you use (xml by default).
55-
```julia
56-
import_project_info(
57-
data=(
58-
project_title="New name",
59-
project_notes="New notes"
60-
),
61-
returnFormat=:csv,
62-
)
63-
```
64-
But please keep in mind that a NamedTuple with one value must contain a comma:
65-
```julia
66-
import_project_info(
67-
data=(
68-
project_title="New name", # this comma is required
69-
),
70-
returnFormat=:csv,
71-
)
72-
```
73-
A `Dict` value is fine as well.
74-
```julia
75-
import_project_info(data=Dict(:project_title=>"New name"), returnFormat=:csv)
76-
```
77-
String values are accepted. If the string is a file name, the contents of the file are sent; otherwise, the value is sent directly as part of the API request.
78-
```julia
79-
data_string = """
80-
[{"data_access_group_name":"CA Site","unique_group_name":"ca_site"},
81-
{"data_access_group_name":"FL Site","unique_group_name":"fl_site"},
82-
{"data_access_group_name":"New Site","unique_group_name":""}]
83-
"""
84-
out = open("data_file.json","w"); write(out, data_string); close(out)
85-
86-
import_DAGs(token=t,data=data_string, format=:json) # string is passed to the API
87-
88-
import_DAGs(token=t, data="data_file.json", format=:json) # string is pattern-matched as a filename
89-
90-
```
91-
As for collections, only collections of scalar entries are currently supported.
92-
So, a list of attributes and values is accepted, but a Dict containing multiple rows per column can only be read in from a file.
93-
94-
In the REDCap API, the presence of a `data` parameter often changes the behavior of a method.
95-
For instance, most import methods are implemented as an export method with an added data parameter.
96-
In REDCap.jl, it would be considered a bug for `import_project_data` to ever act as `export_project_data`, so the data paramater is almost always required where it is present.
97-
98-
### `format` and `returnFormat`
99-
Supported options are `:csv`, `:json`, `:xml` (the default value), and sometimes `:odm`.
100-
These values can be passed as Strings or Symbols.
101-
102-
Generally, the `format` parameter designates user input and the `returnFormat` parameter applies to REDCap messages and return values.
103-
However, this is not consistent within REDCap.
104-
REDCap.jl functions are designed to not accept any parameters that have no effect on the result.
105-
106-
### `content` and `action`
107-
The `content` and `action` parameters are what define each REDCap method, for the most part.
108-
In REDCap.jl, these are passed internally and don't need to be supplied by the user.
109-
Instead, they're fixed for each function.
110-
111-
## Troubleshooting
112-
If a function call doesn't produce the expected results, try making debug messages visible for this package by running `ENV["JULIA_DEBUG"] = REDCap`.
113-
The data parameter is converted to a formatted string, so you might try different format parameters (`:csv`, `:json`, or `:xml`).
114-
Feel free to create an issue for any unexpected errors, or for feature requests.
25+
For more details, see the internal documentation (`help?> REDCap`).
11526

11627
## Acknowledgments
11728
The contributors are grateful for the support of Mary McGrath, Paul Stey, Fernando Gelin, the Brown Data Science Institute, the Brown Center for Biomedical Informatics, and the Tufts CTSI Informatics team.

src/API_Methods.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
include("api_methods/arms.jl")
2+
include("api_methods/data_access_groups.jl")
3+
include("api_methods/events.jl")
4+
include("api_methods/field_names.jl")
5+
include("api_methods/files.jl")
6+
include("api_methods/file_repository.jl")
7+
include("api_methods/instruments.jl")
8+
include("api_methods/logging.jl")
9+
include("api_methods/metadata.jl")
10+
include("api_methods/projects.jl")
11+
include("api_methods/records.jl")
12+
include("api_methods/redcap.jl")
13+
include("api_methods/repeating_instruments_and_events.jl")
14+
include("api_methods/reports.jl")
15+
include("api_methods/surveys.jl")
16+
include("api_methods/users.jl")
17+
include("api_methods/user_roles.jl")

src/REDCap.jl

Lines changed: 105 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,107 @@
1+
#TODO: some import methods can be used to delete entries
2+
# Should this behavior be migrated to delete_* functions?
3+
4+
"""
5+
REDCap API methods are defined as named Julia functions.
6+
The `content` and `action` parameters are managed internally.
7+
8+
Function docstrings indicate how to use the Julia functions.
9+
Refer to the official documentation for detailed specifications on the corresponding REDCap methods.
10+
11+
## Syntax
12+
Each REDCap method accepts a number of parameters that follow a shared naming convention.
13+
REDCap.jl is designed to closely follow the design and syntax patterns of REDCap.
14+
Every REDCap API method is available as a function that supplies certain required parameters and checks user inputs for validity.
15+
16+
Return values and REDCap messages are returned as Strings directly, but the documentation shows how these can be parsed in useful ways.
17+
18+
Function arguments are named after REDCap method parameters.
19+
These are passed as named arguments and take values with intuitive types.
20+
21+
### `token` and `url`
22+
Nearly all REDCap methods accept a token that is unique to the project and user.
23+
24+
The URL must exactly match this example:
25+
```https://example.example/redcap/api/```
26+
27+
Your REDCap token and your institution's REDCap API URL can be read by default from Julia's environment variables.
28+
You can make them avaiable to REDCap.jl by putting the following lines in [your local Julia startup file](https://docs.julialang.org/en/v1/manual/command-line-interface/#Startup-file) (probably `~/.julia/config/startup.jl`):
29+
```julia
30+
ENV["REDCAP_API_TOKEN"] = "C0FFEEC0AC0AC0DEC0FFEEC0AC0AC0DE"
31+
ENV["REDCAP_API_URL"] = "http://example.com/redcap/api/"
32+
```
33+
They can also be passed as ordinary named arguments.
34+
35+
A few methods accept a super token, including `create_project`, which can be used to generate a project and project-level token.
36+
If you have a super token, you might wish to keep that in your startup file, generating and saving project-level tokens as needed.
37+
38+
### `data`
39+
The `data` parameter contains a list of attributes, which varies between REDCap methods.
40+
For definitive attribute lists, see the official REDCap documentation.
41+
In REDCap.jl, this can be a NamedTuple (or any derived type), a file handle, or a String.
42+
If you use a NamedTuple, it will be translated internally into whatever `format` you use (xml by default).
43+
```julia
44+
import_project_info(
45+
data=(
46+
project_title="New name",
47+
project_notes="New notes"
48+
),
49+
returnFormat=:csv,
50+
)
51+
```
52+
But please keep in mind that a NamedTuple with one value must contain a comma:
53+
```julia
54+
import_project_info(
55+
data=(
56+
project_title="New name", # this comma is required
57+
),
58+
returnFormat=:csv,
59+
)
60+
```
61+
A `Dict` value is fine as well.
62+
```julia
63+
import_project_info(data=Dict(:project_title=>"New name"), returnFormat=:csv)
64+
```
65+
String values are accepted. If the string is a file name, the contents of the file are sent; otherwise, the value is sent directly as part of the API request.
66+
```julia
67+
data_string = \"\"\"
68+
[{"data_access_group_name":"CA Site","unique_group_name":"ca_site"},
69+
{"data_access_group_name":"FL Site","unique_group_name":"fl_site"},
70+
{"data_access_group_name":"New Site","unique_group_name":""}]
71+
\"\"\"
72+
out = open("data_file.json","w"); write(out, data_string); close(out)
73+
74+
import_DAGs(token=t,data=data_string, format=:json) # string is passed to the API
75+
76+
import_DAGs(token=t, data="data_file.json", format=:json) # string is pattern-matched as a filename
77+
78+
```
79+
As for collections, only collections of scalar entries are currently supported.
80+
So, a list of attributes and values is accepted, but a Dict containing multiple rows per column can only be read in from a file.
81+
82+
In the REDCap API, the presence of a `data` parameter often changes the behavior of a method.
83+
For instance, most import methods are implemented as an export method with an added data parameter.
84+
In REDCap.jl, it would be considered a bug for `import_project_data` to ever act as `export_project_data`, so the data paramater is almost always required where it is present.
85+
86+
### `format` and `returnFormat`
87+
Supported options are `:csv`, `:json`, `:xml` (the default value), and sometimes `:odm`.
88+
These values can be passed as Strings or Symbols.
89+
90+
For import methods, the `format` parameter designates user input and the `returnFormat` parameter applies to REDCap messages and return values.
91+
Otherwise, there is generally only a single `format` parameter that applies to REDCap messages and return values.
92+
93+
### `content` and `action`
94+
The `content` and `action` parameters are what define each REDCap method, for the most part.
95+
In REDCap.jl, these are passed internally and don't need to be supplied by the user.
96+
Instead, they're fixed for each function.
97+
98+
## Troubleshooting
99+
- If a function call doesn't produce the expected results, try making debug messages visible for this package by running `ENV["JULIA_DEBUG"] = REDCap`.
100+
- The `data` parameter is converted to a formatted string, so you might try different format parameters (`:csv`, `:json`, or `:xml`).
101+
If you're uncertain about the format for an input data parameter, try setting up an example in the browser and exporting the data.
102+
You may have to remove the values for certain generated columns before importing.
103+
- Feel free to create an issue for any unexpected errors, or for feature requests.
104+
"""
1105
module REDCap
2106

3107
using Dates
@@ -10,22 +114,6 @@ include("types.jl")
10114
include("export.jl")
11115
include("request.jl")
12116
include("utils.jl")
13-
include("api_methods/arms.jl")
14-
include("api_methods/data_access_groups.jl")
15-
include("api_methods/events.jl")
16-
include("api_methods/field_names.jl")
17-
include("api_methods/files.jl")
18-
include("api_methods/file_repository.jl")
19-
include("api_methods/instruments.jl")
20-
include("api_methods/logging.jl")
21-
include("api_methods/metadata.jl")
22-
include("api_methods/projects.jl")
23-
include("api_methods/records.jl")
24-
include("api_methods/redcap.jl")
25-
include("api_methods/repeating_instruments_and_events.jl")
26-
include("api_methods/reports.jl")
27-
include("api_methods/surveys.jl")
28-
include("api_methods/users.jl")
29-
include("api_methods/user_roles.jl")
117+
include("API_Methods.jl")
30118

31119
end

src/api_methods/arms.jl

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,66 @@
1-
#TODO: consider changing all functions to the following form:
2-
#=
3-
function delete_arms(;kwargs...)
4-
REDCap.request(;kwargs...)
5-
end
6-
=#
1+
"""
2+
function delete_arms(; url=get_url(), token=get_token(), arms,)
3+
4+
Delete study arms from a REDCap project
5+
6+
# Notes
7+
If no value is provided for `arms`, all study arms are returned.
8+
Deleting a study arm deletes any included records and data.
79
8-
function delete_arms(; url=get_url(), token=get_token(), arms=nothing,)
10+
# Named arguments
11+
- `url`: (read from `ENV["REDCAP_API_URL"]` by default)
12+
- `token`: an API token specific to the REDCap project and username (read from `ENV["REDCAP_API_TOKEN"]` by default)
13+
- `arms`: names of study arms (can be scalar or vector)
14+
15+
"""
16+
function delete_arms(; url=get_url(), token=get_token(), arms,)
917
REDCap.request(;
1018
url=REDCap_url(url),
1119
kwargs = (;token=REDCap_token(token), content=:arm, action=:import, arms,),
1220
)
1321
end
1422

15-
function export_arms(; url=get_url(), token=get_token(), format=nothing, returnFormat=nothing, arms=nothing,)
23+
"""
24+
function export_arms(; url=get_url(), token=get_token(), format=nothing, returnFormat=nothing, arms=nothing,)
25+
26+
Export a REDCap project's study arms
27+
28+
# Notes
29+
If no value is provided for `arms`, all study arms are returned.
30+
31+
# Named arguments
32+
- `url`: (read from `ENV["REDCAP_API_URL"]` by default)
33+
- `token`: an API token specific to the REDCap project and username (read from `ENV["REDCAP_API_TOKEN"]` by default)
34+
- `format`: the desired output format: `:csv`, `:json`, or `:xml` (default)
35+
- `arms`: names of study arms (can be scalar or vector)
36+
37+
"""
38+
function export_arms(; url=get_url(), token=get_token(), format=nothing, arms=nothing,)
1639
REDCap.request(;
1740
url=REDCap_url(url),
18-
kwargs = (; token=REDCap_token(token), content=:arm, format=REDCap_format(format), arms, returnFormat=REDCap_format(returnFormat),),
41+
kwargs = (; token=REDCap_token(token), content=:arm, format=REDCap_format(format), arms,),
1942
)
2043
end
2144

22-
#All examples use JSON
23-
#TODO: what is the proper format for multi-item XML? I can't find this anywhere...
24-
# #TODO: is this ata paratamet required, given the action parameter?
25-
function import_arms(; url=get_url(), token=get_token(), format=nothing, data=nothing, returnFormat=nothing, override=nothing,)
45+
"""
46+
function import_arms(; url=get_url(), token=get_token(), format=nothing, data, returnFormat=nothing, override=nothing,)
47+
48+
Import new study arms to a REDCap project, or rename existing arms
49+
50+
# Notes
51+
Deleting a study arm deletes any included records and data.
52+
53+
# Named arguments
54+
- `url`: (read from `ENV["REDCAP_API_URL"]` by default)
55+
- `token`: an API token specific to the REDCap project and username (read from `ENV["REDCAP_API_TOKEN"]` by default)
56+
- `arms`: names of study arms (can be scalar or vector)
57+
- `override`: if true, all existing arms are erased; if false (default), existing arms can only be renamed
58+
- `data`: May be a String, a file name, or a data type such as NamedTuple or Dict
59+
- `format`: the format of the `data` input parameter: `:csv`, `:json`, or `:xml` (default). If `data` is a String or a file name, this value must indicate the correct format. If `data` is a NamedTuple, Dict, or similar type, this value will determine what format will be used internally to pass on the data.
60+
- `returnFormat`: the desired output format: `:csv`, `:json`, or `:xml` (default)
61+
62+
"""
63+
function import_arms(; url=get_url(), token=get_token(), format=nothing, data, returnFormat=nothing, override=nothing,)
2664
REDCap.request(;
2765
url=REDCap_url(url),
2866
data=REDCap_data(data,REDCap_format(format),xml_tag="arms"),

0 commit comments

Comments
 (0)