| Title: | Interface with XNAT Systems |
|---|---|
| Description: | An R package for interacting with XNAT systems via the REST API. Provides functions to authenticate, list projects/subjects/experiments/scans, search, and download data from XNAT neuroimaging archives. |
| Authors: | Bradley Buchsbaum [aut, cre] |
| Maintainer: | Bradley Buchsbaum <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.2.0 |
| Built: | 2026-06-02 15:00:12 UTC |
| Source: | https://github.com/bbuchsbaum/xnatR |
Establishes authentication credentials for communicating with an XNAT server. Credentials are stored in the package environment for use by other functions.
authenticate_xnat( base_url = NULL, username = NULL, password = NULL, ssl_verify = TRUE, verify = TRUE, use_jsession = FALSE )authenticate_xnat( base_url = NULL, username = NULL, password = NULL, ssl_verify = TRUE, verify = TRUE, use_jsession = FALSE )
base_url |
The base URL of the XNAT server (e.g., "https://central.xnat.org"). If NULL, checks XNATR_HOST environment variable, then config file, then .netrc. |
username |
XNAT username. If NULL, checks XNATR_USER environment variable, then config file, then .netrc. For XNAT 1.8+ with auth providers, use "provider/username" format. |
password |
XNAT password. If NULL, checks XNATR_PASS environment variable, then config file, then .netrc. |
ssl_verify |
Whether to verify SSL certificates. Default TRUE. |
verify |
Whether to verify credentials by making a test request. Default TRUE. |
use_jsession |
Whether to establish a JSESSION cookie-based session instead of using Basic Auth for each request. Can improve performance. Default FALSE. |
Invisibly returns TRUE if authentication is successful.
## Not run: # Direct credentials authenticate_xnat( base_url = "https://central.xnat.org", username = "guest", password = "guest" ) # Using environment variables (XNATR_HOST, XNATR_USER, XNATR_PASS) authenticate_xnat() # Using config file (~/.xnatR_config.yml) authenticate_xnat() # Using .netrc file authenticate_xnat(base_url = "https://central.xnat.org") # With JSESSION for better performance authenticate_xnat( base_url = "https://central.xnat.org", username = "myuser", password = "mypass", use_jsession = TRUE ) # XNAT 1.8+ with auth provider authenticate_xnat( base_url = "https://myxnat.org", username = "ldap/myuser", password = "mypass" ) ## End(Not run)## Not run: # Direct credentials authenticate_xnat( base_url = "https://central.xnat.org", username = "guest", password = "guest" ) # Using environment variables (XNATR_HOST, XNATR_USER, XNATR_PASS) authenticate_xnat() # Using config file (~/.xnatR_config.yml) authenticate_xnat() # Using .netrc file authenticate_xnat(base_url = "https://central.xnat.org") # With JSESSION for better performance authenticate_xnat( base_url = "https://central.xnat.org", username = "myuser", password = "mypass", use_jsession = TRUE ) # XNAT 1.8+ with auth provider authenticate_xnat( base_url = "https://myxnat.org", username = "ldap/myuser", password = "mypass" ) ## End(Not run)
Reads DICOM header values directly from the files stored in the XNAT archive
using the /data/services/dicomdump service, without downloading the image
files. This is useful for inspecting acquisition parameters (repetition
time, echo time, sequence name, and so on) or for filtering scans on real
header values before deciding what to download.
dicom_dump(experiment_id, scan_id = NULL, fields = NULL, client = NULL)dicom_dump(experiment_id, scan_id = NULL, fields = NULL, client = NULL)
experiment_id |
The experiment (session) identifier (required). |
scan_id |
Optional scan identifier. When supplied, the dump is limited to that scan; otherwise the whole session is dumped. |
fields |
Optional character vector of DICOM tags to return. Tags may be
given as 8-digit hex ( |
client |
Optional |
Only DICOM-backed scans are supported. The service samples header values from the archived files, and the exact columns returned depend on your XNAT version.
A tibble of class xnat_dicom_dump. Columns follow the XNAT DICOM
dump ResultSet and typically include the DICOM tag, value representation
(vr), description, and value.
## Not run: xnat_connect(base_url = "https://central.xnat.org", username = "guest", password = "guest") # Dump every header field for a session dicom_dump("OAS1_0001_MR1") # A single scan, only Modality (0008,0060) and Repetition Time (0018,0080) dicom_dump("OAS1_0001_MR1", scan_id = "1", fields = c("00080060", "00180080")) ## End(Not run)## Not run: xnat_connect(base_url = "https://central.xnat.org", username = "guest", password = "guest") # Dump every header field for a session dicom_dump("OAS1_0001_MR1") # A single scan, only Modality (0008,0060) and Repetition Time (0018,0080) dicom_dump("OAS1_0001_MR1", scan_id = "1", fields = c("00080060", "00180080")) ## End(Not run)
Downloads an experiment scan selection (ALL by default) as an archive.
download_experiment( experiment_id, scan_id = "ALL", format = "zip", dest_dir = tempdir(), dest_file = NULL, extract = FALSE, progress = TRUE, strict = TRUE, client = NULL )download_experiment( experiment_id, scan_id = "ALL", format = "zip", dest_dir = tempdir(), dest_file = NULL, extract = FALSE, progress = TRUE, strict = TRUE, client = NULL )
experiment_id |
Experiment identifier. |
scan_id |
Scan ID/type to download; defaults to |
format |
Archive format: |
dest_dir |
Destination directory. Defaults to |
dest_file |
Optional destination filename. If |
extract |
If |
progress |
Show download progress bar. Default |
strict |
If |
client |
Optional |
Archive path, extracted file paths (when extract = TRUE), or NULL
on failure when strict = FALSE.
Downloads files from an XNAT experiment, with options to select specific scans and resources.
download_files( project_id, subject_id, experiment_id, scan_id = "ALL", resource = NULL, format = "zip", dest_dir = getwd(), dest_file = NULL, progress = TRUE, client = NULL )download_files( project_id, subject_id, experiment_id, scan_id = "ALL", resource = NULL, format = "zip", dest_dir = getwd(), dest_file = NULL, progress = TRUE, client = NULL )
project_id |
The project identifier. |
subject_id |
The subject identifier. |
experiment_id |
The experiment identifier. |
scan_id |
Scan ID(s) to download. Can be:
|
resource |
Resource name (e.g., "DICOM", "NIFTI"). NULL for all resources. |
format |
Download format: "zip" (default) or "tar.gz". |
dest_dir |
Destination directory. Defaults to current working directory. |
dest_file |
Custom destination filename. If NULL, auto-generated. |
progress |
Show download progress bar. Default TRUE. |
client |
Optional |
Invisibly returns the path to the downloaded file.
## Not run: authenticate_xnat(base_url = "https://central.xnat.org", username = "guest", password = "guest") # Download all scans as ZIP download_files( project_id = "MyProject", subject_id = "Subject001", experiment_id = "Exp001" ) # Download specific scans download_files( project_id = "MyProject", subject_id = "Subject001", experiment_id = "Exp001", scan_id = c("1", "2") ) # Download DICOM only download_files( project_id = "MyProject", subject_id = "Subject001", experiment_id = "Exp001", resource = "DICOM" ) ## End(Not run)## Not run: authenticate_xnat(base_url = "https://central.xnat.org", username = "guest", password = "guest") # Download all scans as ZIP download_files( project_id = "MyProject", subject_id = "Subject001", experiment_id = "Exp001" ) # Download specific scans download_files( project_id = "MyProject", subject_id = "Subject001", experiment_id = "Exp001", scan_id = c("1", "2") ) # Download DICOM only download_files( project_id = "MyProject", subject_id = "Subject001", experiment_id = "Exp001", resource = "DICOM" ) ## End(Not run)
Downloads all experiments and scans for a subject.
download_subject( project_id, subject_id, format = "zip", dest_dir = getwd(), progress = TRUE, client = NULL )download_subject( project_id, subject_id, format = "zip", dest_dir = getwd(), progress = TRUE, client = NULL )
project_id |
The project identifier. |
subject_id |
The subject identifier. |
format |
Download format: "zip" (default) or "tar.gz". |
dest_dir |
Destination directory. |
progress |
Show download progress bar. |
client |
Optional |
Invisibly returns a character vector of downloaded file paths.
## Not run: paths <- download_subject( project_id = "MyProject", subject_id = "Subject001", dest_dir = "~/Downloads" ) ## End(Not run)## Not run: paths <- download_subject( project_id = "MyProject", subject_id = "Subject001", dest_dir = "~/Downloads" ) ## End(Not run)
Downloads a file using its full URL or a pre-constructed path.
download_xnat_file(url, dest_file, progress = TRUE, client = NULL)download_xnat_file(url, dest_file, progress = TRUE, client = NULL)
url |
Full URL to the file, or a path relative to the XNAT base URL. |
dest_file |
Destination file path. |
progress |
Show download progress bar. Default TRUE. |
client |
Optional |
Invisibly returns the destination path.
## Not run: download_xnat_file( url = "/data/projects/P1/subjects/S1/experiments/E1/scans/1/resources/DICOM/files/file.dcm", dest_file = "file.dcm" ) ## End(Not run)## Not run: download_xnat_file( url = "/data/projects/P1/subjects/S1/experiments/E1/scans/1/resources/DICOM/files/file.dcm", dest_file = "file.dcm" ) ## End(Not run)
Creates a template configuration file at ~/.xnatR_config.yml
initialize_config()initialize_config()
Invisibly returns the path to the config file.
## Not run: initialize_config() # Edit ~/.xnatR_config.yml with your credentials ## End(Not run)## Not run: initialize_config() # Edit ~/.xnatR_config.yml with your credentials ## End(Not run)
Copies the packaged xnatR command wrapper to a directory on your shell
PATH, or to another destination directory you choose.
install_cli(dest_dir = "~/.local/bin", overwrite = FALSE, commands = NULL)install_cli(dest_dir = "~/.local/bin", overwrite = FALSE, commands = NULL)
dest_dir |
Destination directory for command wrappers. |
overwrite |
If |
commands |
Optional command names to install. Currently only |
Invisibly returns the installed wrapper path.
Check if authenticated
is_authenticated(client = NULL)is_authenticated(client = NULL)
client |
Optional |
TRUE if credentials are stored, FALSE otherwise.
## Not run: if (is_authenticated()) { list_projects() } ## End(Not run)## Not run: if (is_authenticated()) { list_projects() } ## End(Not run)
Retrieves assessors (derived data like FreeSurfer outputs, QC results) for a specific experiment.
list_assessors( project_id, subject_id, experiment_id, columns = NULL, limit = NULL, offset = NULL, client = NULL )list_assessors( project_id, subject_id, experiment_id, columns = NULL, limit = NULL, offset = NULL, client = NULL )
project_id |
The project identifier. |
subject_id |
The subject identifier. |
experiment_id |
The experiment identifier. |
columns |
Character vector of column names to include. |
limit |
Maximum number of results to return. |
offset |
Number of results to skip for pagination. |
client |
Optional |
A tibble of class xnat_assessors containing assessor details.
Common columns include:
ID: Assessor identifier
label: Assessor label
xsiType: Assessor data type
project: Project ID
## Not run: assessors <- list_assessors( project_id = "MyProject", subject_id = "Subject001", experiment_id = "Exp001" ) ## End(Not run)## Not run: assessors <- list_assessors( project_id = "MyProject", subject_id = "Subject001", experiment_id = "Exp001" ) ## End(Not run)
Retrieves all data types (XSI types) available on the XNAT server. These are the schema elements that define different types of data (e.g., xnat:mrSessionData, xnat:subjectData).
list_data_types(client = NULL)list_data_types(client = NULL)
client |
Optional |
A tibble of class xnat_datatypes containing data type information.
Columns include:
ELEMENT_NAME: The XSI type name (e.g., "xnat:mrSessionData")
Other schema-related columns
## Not run: datatypes <- list_data_types() print(datatypes) # Find all MR-related types mr_types <- datatypes[grep("mr", datatypes$ELEMENT_NAME, ignore.case = TRUE), ] ## End(Not run)## Not run: datatypes <- list_data_types() print(datatypes) # Find all MR-related types mr_types <- datatypes[grep("mr", datatypes$ELEMENT_NAME, ignore.case = TRUE), ] ## End(Not run)
Retrieves files from an experiment-level resource.
list_experiment_files( project_id, subject_id, experiment_id, resource, client = NULL )list_experiment_files( project_id, subject_id, experiment_id, resource, client = NULL )
project_id |
The project identifier. |
subject_id |
The subject identifier. |
experiment_id |
The experiment identifier. |
resource |
The resource label. |
client |
Optional |
A tibble of class xnat_files.
## Not run: files <- list_experiment_files( project_id = "MyProject", subject_id = "Subject001", experiment_id = "Exp001", resource = "SNAPSHOTS" ) ## End(Not run)## Not run: files <- list_experiment_files( project_id = "MyProject", subject_id = "Subject001", experiment_id = "Exp001", resource = "SNAPSHOTS" ) ## End(Not run)
Returns experiment-level files and, optionally, falls back to scan-level files
(scans/ALL/files) if none are present at the experiment level.
list_experiment_files_all( experiment_id, include_scan_level = TRUE, client = NULL )list_experiment_files_all( experiment_id, include_scan_level = TRUE, client = NULL )
experiment_id |
Experiment identifier. |
include_scan_level |
If |
client |
Optional |
A tibble of class xnat_files.
Retrieves resources available at the experiment level.
list_experiment_resources(project_id, subject_id, experiment_id, client = NULL)list_experiment_resources(project_id, subject_id, experiment_id, client = NULL)
project_id |
The project identifier. |
subject_id |
The subject identifier. |
experiment_id |
The experiment identifier. |
client |
Optional |
A tibble of class xnat_resources.
## Not run: resources <- list_experiment_resources( project_id = "MyProject", subject_id = "Subject001", experiment_id = "Exp001" ) ## End(Not run)## Not run: resources <- list_experiment_resources( project_id = "MyProject", subject_id = "Subject001", experiment_id = "Exp001" ) ## End(Not run)
Retrieves a list of experiments (imaging sessions). When only
project_id is given, returns all experiments across every subject in
that project. When subject_id is also supplied, results are scoped to
that single subject.
list_experiments( project_id, subject_id = NULL, columns = NULL, limit = NULL, offset = NULL, client = NULL )list_experiments( project_id, subject_id = NULL, columns = NULL, limit = NULL, offset = NULL, client = NULL )
project_id |
The project identifier (required). |
subject_id |
Optional subject identifier. When |
columns |
Character vector of column names to include. |
limit |
Maximum number of results to return. |
offset |
Number of results to skip for pagination. |
client |
Optional |
A tibble of class xnat_experiments containing experiment details.
Common columns include:
ID: Experiment identifier
label: Experiment label
project: Project ID
subject_ID: Subject ID
xsiType: Experiment type (e.g., "xnat:mrSessionData")
date: Session date
## Not run: authenticate_xnat(base_url = "https://central.xnat.org", username = "guest", password = "guest") # All experiments in a project all_exps <- list_experiments(project_id = "MyProject") # Experiments for one subject subj_exps <- list_experiments( project_id = "MyProject", subject_id = "Subject001" ) ## End(Not run)## Not run: authenticate_xnat(base_url = "https://central.xnat.org", username = "guest", password = "guest") # All experiments in a project all_exps <- list_experiments(project_id = "MyProject") # Experiments for one subject subj_exps <- list_experiments( project_id = "MyProject", subject_id = "Subject001" ) ## End(Not run)
Retrieves a list of files within a specific resource.
list_files( project_id, subject_id, experiment_id, scan_id, resource, client = NULL )list_files( project_id, subject_id, experiment_id, scan_id, resource, client = NULL )
project_id |
The project identifier. |
subject_id |
The subject identifier. |
experiment_id |
The experiment identifier. |
scan_id |
The scan identifier. |
resource |
The resource label (e.g., "DICOM", "NIFTI"). |
client |
Optional |
A tibble of class xnat_files containing file details.
Common columns include:
Name: File name
Size: File size in bytes
URI: File URI for downloading
collection: Resource collection name
file_format: File format
file_content: Content type
## Not run: files <- list_files( project_id = "MyProject", subject_id = "Subject001", experiment_id = "Exp001", scan_id = "1", resource = "DICOM" ) ## End(Not run)## Not run: files <- list_files( project_id = "MyProject", subject_id = "Subject001", experiment_id = "Exp001", scan_id = "1", resource = "DICOM" ) ## End(Not run)
Retrieves a list of projects from the authenticated XNAT server.
list_projects(columns = NULL, limit = NULL, offset = NULL, client = NULL)list_projects(columns = NULL, limit = NULL, offset = NULL, client = NULL)
columns |
Character vector of column names to include in the result. Use NULL (default) to return all available columns. |
limit |
Maximum number of results to return. NULL for no limit. |
offset |
Number of results to skip. Used for pagination. |
client |
Optional |
A tibble of class xnat_projects containing project details.
Common columns include:
ID: Project identifier
name: Project display name
secondary_ID: Secondary identifier
description: Project description
pi_firstname, pi_lastname: Principal investigator
## Not run: authenticate_xnat(base_url = "https://central.xnat.org", username = "guest", password = "guest") # List all projects projects <- list_projects() # Get specific columns only projects <- list_projects(columns = c("ID", "name", "description")) # Pagination first_10 <- list_projects(limit = 10) next_10 <- list_projects(limit = 10, offset = 10) ## End(Not run)## Not run: authenticate_xnat(base_url = "https://central.xnat.org", username = "guest", password = "guest") # List all projects projects <- list_projects() # Get specific columns only projects <- list_projects(columns = c("ID", "name", "description")) # Pagination first_10 <- list_projects(limit = 10) next_10 <- list_projects(limit = 10, offset = 10) ## End(Not run)
Retrieves the fields that can be queried/searched for a specific XSI type. Useful for building search queries.
list_queryable_fields(xsi_type, client = NULL)list_queryable_fields(xsi_type, client = NULL)
xsi_type |
The XSI type to query (e.g., "xnat:mrSessionData", "xnat:subjectData"). |
client |
Optional |
A tibble of class xnat_fields containing field information.
Common columns include:
FIELD_ID: Field identifier for queries
TYPE: Data type of the field
HEADER: Display name
DESC: Field description
## Not run: # Get queryable fields for MR sessions fields <- list_queryable_fields("xnat:mrSessionData") # Get fields for subjects subject_fields <- list_queryable_fields("xnat:subjectData") ## End(Not run)## Not run: # Get queryable fields for MR sessions fields <- list_queryable_fields("xnat:mrSessionData") # Get fields for subjects subject_fields <- list_queryable_fields("xnat:subjectData") ## End(Not run)
Convenience wrapper around list_experiments() that returns experiments
sorted by date (most recent first) with an optional cap on the number of
rows returned.
list_recent_sessions( project_id, n = NULL, subject_id = NULL, columns = NULL, client = NULL )list_recent_sessions( project_id, n = NULL, subject_id = NULL, columns = NULL, client = NULL )
project_id |
The project identifier (required). |
n |
Maximum number of sessions to return. |
subject_id |
Optional subject identifier to scope results. |
columns |
Character vector of column names to include. |
client |
Optional |
A tibble of class xnat_experiments, ordered most-recent first.
## Not run: # 10 most recent sessions in a project recent <- list_recent_sessions("MyProject", n = 10) # All sessions for a subject, newest first recent_subj <- list_recent_sessions("MyProject", subject_id = "S001") ## End(Not run)## Not run: # 10 most recent sessions in a project recent <- list_recent_sessions("MyProject", n = 10) # All sessions for a subject, newest first recent_subj <- list_recent_sessions("MyProject", subject_id = "S001") ## End(Not run)
Retrieves reconstructions (processed image data) for a specific experiment.
list_reconstructions( project_id, subject_id, experiment_id, columns = NULL, limit = NULL, offset = NULL, client = NULL )list_reconstructions( project_id, subject_id, experiment_id, columns = NULL, limit = NULL, offset = NULL, client = NULL )
project_id |
The project identifier. |
subject_id |
The subject identifier. |
experiment_id |
The experiment identifier. |
columns |
Character vector of column names to include. |
limit |
Maximum number of results to return. |
offset |
Number of results to skip for pagination. |
client |
Optional |
A tibble of class xnat_reconstructions containing reconstruction details.
Common columns include:
ID: Reconstruction identifier
type: Reconstruction type
xsiType: Data type
## Not run: recons <- list_reconstructions( project_id = "MyProject", subject_id = "Subject001", experiment_id = "Exp001" ) ## End(Not run)## Not run: recons <- list_reconstructions( project_id = "MyProject", subject_id = "Subject001", experiment_id = "Exp001" ) ## End(Not run)
Retrieves resources (e.g., DICOM, NIFTI) available for a specific scan.
list_resources(project_id, subject_id, experiment_id, scan_id, client = NULL)list_resources(project_id, subject_id, experiment_id, scan_id, client = NULL)
project_id |
The project identifier. |
subject_id |
The subject identifier. |
experiment_id |
The experiment identifier. |
scan_id |
The scan identifier. |
client |
Optional |
A tibble of class xnat_resources containing resource details.
Common columns include:
xnat_abstractresource_id: Resource ID
label: Resource label (e.g., "DICOM", "NIFTI")
file_count: Number of files
file_size: Total size in bytes
format: Resource format
## Not run: resources <- list_resources( project_id = "MyProject", subject_id = "Subject001", experiment_id = "Exp001", scan_id = "1" ) ## End(Not run)## Not run: resources <- list_resources( project_id = "MyProject", subject_id = "Subject001", experiment_id = "Exp001", scan_id = "1" ) ## End(Not run)
Retrieves a list of scans from a specific experiment.
list_scans( project_id, subject_id, experiment_id, columns = NULL, limit = NULL, offset = NULL, client = NULL )list_scans( project_id, subject_id, experiment_id, columns = NULL, limit = NULL, offset = NULL, client = NULL )
project_id |
The project identifier (required). |
subject_id |
The subject identifier (required). |
experiment_id |
The experiment identifier (required). |
columns |
Character vector of column names to include. |
limit |
Maximum number of results to return. |
offset |
Number of results to skip for pagination. |
client |
Optional |
A tibble of class xnat_scans containing scan details.
Common columns include:
ID: Scan number
type: Scan type (e.g., "T1", "T2", "BOLD")
series_description: Series description
quality: Scan quality rating
xsiType: Scan data type
frames: Number of frames
## Not run: authenticate_xnat(base_url = "https://central.xnat.org", username = "guest", password = "guest") scans <- list_scans( project_id = "MyProject", subject_id = "Subject001", experiment_id = "Experiment001" ) ## End(Not run)## Not run: authenticate_xnat(base_url = "https://central.xnat.org", username = "guest", password = "guest") scans <- list_scans( project_id = "MyProject", subject_id = "Subject001", experiment_id = "Experiment001" ) ## End(Not run)
Retrieves a list of subjects from a specific project on the XNAT server.
list_subjects( project_id, columns = NULL, limit = NULL, offset = NULL, client = NULL )list_subjects( project_id, columns = NULL, limit = NULL, offset = NULL, client = NULL )
project_id |
The project identifier (required). |
columns |
Character vector of column names to include. Use NULL (default) to return all available columns. |
limit |
Maximum number of results to return. |
offset |
Number of results to skip for pagination. |
client |
Optional |
A tibble of class xnat_subjects containing subject details.
Common columns include:
ID: Subject identifier
label: Subject label
project: Project ID
gender: Subject gender
handedness: Subject handedness
age: Subject age
## Not run: authenticate_xnat(base_url = "https://central.xnat.org", username = "guest", password = "guest") subjects <- list_subjects(project_id = "MyProject") # With pagination subjects <- list_subjects(project_id = "MyProject", limit = 100) ## End(Not run)## Not run: authenticate_xnat(base_url = "https://central.xnat.org", username = "guest", password = "guest") subjects <- list_subjects(project_id = "MyProject") # With pagination subjects <- list_subjects(project_id = "MyProject", limit = 100) ## End(Not run)
Helper function to create a search criterion for use with xnat_search().
search_criterion(field, comparison, value)search_criterion(field, comparison, value)
field |
The field ID to search. |
comparison |
Comparison type: "EQUALS", "LIKE", "GREATER_THAN", "LESS_THAN", "GREATER_THAN_EQUAL", "LESS_THAN_EQUAL". |
value |
The value to compare against. |
A list representing the criterion.
## Not run: # Create criteria for a search project_criterion <- search_criterion( field = "xnat:mrSessionData/project", comparison = "EQUALS", value = "MyProject" ) date_criterion <- search_criterion( field = "xnat:mrSessionData/date", comparison = "GREATER_THAN", value = "2023-01-01" ) # Use in search results <- xnat_search( root_type = "xnat:mrSessionData", fields = c("xnat:mrSessionData/ID"), criteria = list(project_criterion, date_criterion) ) ## End(Not run)## Not run: # Create criteria for a search project_criterion <- search_criterion( field = "xnat:mrSessionData/project", comparison = "EQUALS", value = "MyProject" ) date_criterion <- search_criterion( field = "xnat:mrSessionData/date", comparison = "GREATER_THAN", value = "2023-01-01" ) # Use in search results <- xnat_search( root_type = "xnat:mrSessionData", fields = c("xnat:mrSessionData/ID"), criteria = list(project_criterion, date_criterion) ) ## End(Not run)
Execute Search Builder Query
search_execute(builder, format = "json", client = NULL)search_execute(builder, format = "json", client = NULL)
builder |
A search builder object. |
format |
Output format: "json" (default) or "csv". |
client |
Optional |
A tibble containing the search results.
Searches for projects containing a substring in their ID or name.
search_projects(pattern, columns = NULL, client = NULL)search_projects(pattern, columns = NULL, client = NULL)
pattern |
A character string to search for (case-insensitive). |
columns |
Character vector of column names to include. |
client |
Optional |
A tibble of class xnat_projects containing matching projects.
## Not run: authenticate_xnat(base_url = "https://central.xnat.org", username = "guest", password = "guest") # Search for projects containing "test" results <- search_projects("test") # Search for projects with "MRI" in name mri_projects <- search_projects("MRI") ## End(Not run)## Not run: authenticate_xnat(base_url = "https://central.xnat.org", username = "guest", password = "guest") # Search for projects containing "test" results <- search_projects("test") # Search for projects with "MRI" in name mri_projects <- search_projects("MRI") ## End(Not run)
Performs a scan-parameter search across sessions/scans using XNAT's XML search endpoint with a tidy, xnatR-style interface.
search_scans( subject_id = NULL, project_id = NULL, age = NULL, experiment_id = NULL, scan_type = NULL, tr = NULL, te = NULL, ti = NULL, flip = NULL, voxel_res_units = NULL, voxel_res_x = NULL, voxel_res_y = NULL, voxel_res_z = NULL, orientation = NULL, client = NULL )search_scans( subject_id = NULL, project_id = NULL, age = NULL, experiment_id = NULL, scan_type = NULL, tr = NULL, te = NULL, ti = NULL, flip = NULL, voxel_res_units = NULL, voxel_res_x = NULL, voxel_res_y = NULL, voxel_res_z = NULL, orientation = NULL, client = NULL )
subject_id |
Optional subject ID filter. |
project_id |
Optional project ID filter. |
age |
Optional age filter. |
experiment_id |
Optional experiment ID filter. |
scan_type |
Optional scan type filter. |
tr |
Optional repetition time filter. |
te |
Optional echo time filter. |
ti |
Optional inversion time filter. |
flip |
Optional flip angle filter. |
voxel_res_units |
Optional voxel-resolution units filter. |
voxel_res_x |
Optional X voxel-resolution filter. |
voxel_res_y |
Optional Y voxel-resolution filter. |
voxel_res_z |
Optional Z voxel-resolution filter. |
orientation |
Optional orientation filter. |
client |
Optional |
A tibble containing matching scan rows.
Add Fields to Search Builder
search_select(builder, ...)search_select(builder, ...)
builder |
A search builder object. |
... |
Field IDs to include in the results. |
The modified search builder (for chaining).
Add Criterion to Search Builder
search_where(builder, field, comparison, value)search_where(builder, field, comparison, value)
builder |
A search builder object. |
field |
Field ID to filter on. |
comparison |
Comparison type. |
value |
Value to compare. |
The modified search builder (for chaining).
This helper makes existing code written for the global-session workflow work
with an explicit xnat_client.
with_xnat_client(client, expr)with_xnat_client(client, expr)
client |
An |
expr |
Expression to evaluate. |
The value of expr.
These helpers provide an interactive console UI for exploring XNAT resources.
They are designed to be safe in non-interactive contexts: when
.interactive = FALSE, the functions return the corresponding listing tibble
without prompting.
Browse projects interactively
xnat_browse_projects( client = NULL, pattern = NULL, columns = c("ID", "name", "description"), page_size = 20, .interactive = interactive() )xnat_browse_projects( client = NULL, pattern = NULL, columns = c("ID", "name", "description"), page_size = 20, .interactive = interactive() )
client |
Optional |
pattern |
Optional substring filter applied to the current page. |
columns |
Columns to display (defaults to common project fields). |
page_size |
Rows per page (default 20). |
.interactive |
If |
A tibble row selection (or NULL if quit). In non-interactive mode,
returns a tibble of projects.
If project_id is omitted in interactive mode, the function first prompts for
a project.
xnat_browse_subjects( project_id = NULL, client = NULL, pattern = NULL, columns = c("ID", "label", "gender", "age"), page_size = 20, .interactive = interactive() )xnat_browse_subjects( project_id = NULL, client = NULL, pattern = NULL, columns = c("ID", "label", "gender", "age"), page_size = 20, .interactive = interactive() )
project_id |
Project identifier (optional in interactive mode). |
client |
Optional |
pattern |
Optional substring filter applied to the current page. |
columns |
Columns to display (defaults to common subject fields). |
page_size |
Rows per page (default 20). |
.interactive |
If |
A tibble row selection (or NULL if quit). In non-interactive mode,
returns a tibble of subjects.
xnat_client
Low-level constructor for client objects. Prefer xnat_connect() for most
use cases.
xnat_client( base_url, username = NULL, password = NULL, ssl_verify = TRUE, jsession = NULL )xnat_client( base_url, username = NULL, password = NULL, ssl_verify = TRUE, jsession = NULL )
base_url |
Base URL of the XNAT server. |
username |
Username. |
password |
Password. |
ssl_verify |
Whether to verify SSL certificates. Default |
jsession |
Optional JSESSIONID value. |
An xnat_client.
xnat_connect() creates an explicit client object that can be passed to all
xnatR functions via the client argument. This avoids relying on global
session state stored in the package environment.
xnat_connect( base_url = NULL, username = NULL, password = NULL, ssl_verify = TRUE, verify = TRUE, use_jsession = FALSE, jsession = NULL, xnat_name = NULL )xnat_connect( base_url = NULL, username = NULL, password = NULL, ssl_verify = TRUE, verify = TRUE, use_jsession = FALSE, jsession = NULL, xnat_name = NULL )
base_url |
Base URL of the XNAT server (e.g., |
username |
Username, or |
password |
Password, or |
ssl_verify |
Whether to verify SSL certificates. Default |
verify |
Whether to verify credentials by making a test request. Default |
use_jsession |
Whether to establish a JSESSION cookie-based session instead
of using Basic Auth for each request. Default |
jsession |
Optional existing JSESSIONID value. When provided, requests use cookie auth and do not send Basic Auth. |
xnat_name |
Optional server nickname used for compatibility with Rxnat-style
environment variables ( |
authenticate_xnat() remains available for a global-session workflow.
xnat_connect() returns an xnat_client.
## Not run: client <- xnat_connect( base_url = "https://central.xnat.org", username = "guest", password = "guest", use_jsession = TRUE ) projects <- list_projects(client = client) ## End(Not run)## Not run: client <- xnat_connect( base_url = "https://central.xnat.org", username = "guest", password = "guest", use_jsession = TRUE ) projects <- list_projects(client = client) ## End(Not run)
Returns an xnat_client populated from the package environment (set by
authenticate_xnat()), or NULL if not authenticated.
xnat_current_client()xnat_current_client()
An xnat_client or NULL.
Drills down the project -> subject -> experiment -> scan hierarchy using fzf for fuzzy search and multi-select, with a preview pane showing the highlighted row's detail (DICOM headers at the scan level).
xnat_explore( client = NULL, project_id = NULL, subject_id = NULL, experiment_id = NULL, dest_dir = ".", .interactive = interactive(), run_fn = .fzf_run )xnat_explore( client = NULL, project_id = NULL, subject_id = NULL, experiment_id = NULL, dest_dir = ".", .interactive = interactive(), run_fn = .fzf_run )
client |
Optional |
project_id, subject_id, experiment_id
|
Optional starting point; supplying them skips the corresponding levels. |
dest_dir |
Destination directory for the |
.interactive |
If |
run_fn |
Internal seam for invoking fzf; overridden in tests. |
Keys: Enter opens the highlighted item; Tab marks rows; Ctrl-S returns
the marked rows (e.g. to feed download_experiment()); Ctrl-D downloads the
marked rows immediately and stays in the explorer; Ctrl-E (at the subject
level) jumps to all sessions in the project; Ctrl-O opens the highlighted
item in the XNAT web UI; Esc goes back a level (or exits at the top).
Requires the fzf binary on your PATH (e.g. brew install fzf) and an
interactive session.
A tibble of the rows selected with Ctrl-S (or Enter on a leaf
scan), or NULL if you exit without selecting.
## Not run: xnat_connect(base_url = "https://central.xnat.org", username = "guest", password = "guest") chosen <- xnat_explore() # Jump straight to a known session's scans: xnat_explore(project_id = "CENTRAL_OASIS", subject_id = "OAS1_0001", experiment_id = "OAS1_0001_MR1") ## End(Not run)## Not run: xnat_connect(base_url = "https://central.xnat.org", username = "guest", password = "guest") chosen <- xnat_explore() # Jump straight to a known session's scans: xnat_explore(project_id = "CENTRAL_OASIS", subject_id = "OAS1_0001", experiment_id = "OAS1_0001_MR1") ## End(Not run)
Clears stored credentials and optionally invalidates the server session.
xnat_logout(invalidate_session = FALSE)xnat_logout(invalidate_session = FALSE)
invalidate_session |
Whether to send a logout request to the server. Default FALSE (just clears local credentials). |
Invisibly returns TRUE.
## Not run: xnat_logout() # Also invalidate server session xnat_logout(invalidate_session = TRUE) ## End(Not run)## Not run: xnat_logout() # Also invalidate server session xnat_logout(invalidate_session = TRUE) ## End(Not run)
Performs an advanced search across XNAT data using the XML search API. This function builds the XML query for you based on simple R syntax.
xnat_search(root_type, fields, criteria = NULL, format = "json", client = NULL)xnat_search(root_type, fields, criteria = NULL, format = "json", client = NULL)
root_type |
The XSI type to search (e.g., "xnat:mrSessionData",
"xnat:subjectData"). Use |
fields |
Character vector of field IDs to return. Use
|
criteria |
A list of search criteria. Each criterion is a list with:
|
format |
Output format: "json" (default) or "csv". |
client |
Optional |
A tibble containing the search results.
## Not run: # Search for all MR sessions in a project results <- xnat_search( root_type = "xnat:mrSessionData", fields = c("xnat:mrSessionData/ID", "xnat:mrSessionData/label", "xnat:mrSessionData/date"), criteria = list( list(field = "xnat:mrSessionData/project", comparison = "EQUALS", value = "MyProject") ) ) # Search for subjects with specific gender subjects <- xnat_search( root_type = "xnat:subjectData", fields = c("xnat:subjectData/ID", "xnat:subjectData/label"), criteria = list( list(field = "xnat:subjectData/gender", comparison = "EQUALS", value = "M") ) ) ## End(Not run)## Not run: # Search for all MR sessions in a project results <- xnat_search( root_type = "xnat:mrSessionData", fields = c("xnat:mrSessionData/ID", "xnat:mrSessionData/label", "xnat:mrSessionData/date"), criteria = list( list(field = "xnat:mrSessionData/project", comparison = "EQUALS", value = "MyProject") ) ) # Search for subjects with specific gender subjects <- xnat_search( root_type = "xnat:subjectData", fields = c("xnat:subjectData/ID", "xnat:subjectData/label"), criteria = list( list(field = "xnat:subjectData/gender", comparison = "EQUALS", value = "M") ) ) ## End(Not run)
Creates a search builder object for constructing complex searches with a fluent API.
xnat_search_builder(root_type, client = NULL)xnat_search_builder(root_type, client = NULL)
root_type |
The XSI type to search. |
client |
Optional |
A search builder object.
## Not run: # Build and execute a search using fluent API results <- xnat_search_builder("xnat:mrSessionData") |> search_select("xnat:mrSessionData/ID", "xnat:mrSessionData/label") |> search_where("xnat:mrSessionData/project", "EQUALS", "MyProject") |> search_where("xnat:mrSessionData/date", "GREATER_THAN", "2023-01-01") |> search_execute() ## End(Not run)## Not run: # Build and execute a search using fluent API results <- xnat_search_builder("xnat:mrSessionData") |> search_select("xnat:mrSessionData/ID", "xnat:mrSessionData/label") |> search_where("xnat:mrSessionData/project", "EQUALS", "MyProject") |> search_where("xnat:mrSessionData/date", "GREATER_THAN", "2023-01-01") |> search_execute() ## End(Not run)
Get current XNAT server URL
xnat_server(client = NULL)xnat_server(client = NULL)
client |
Optional |
The current server URL or NULL if not authenticated.
## Not run: xnat_server() ## End(Not run)## Not run: xnat_server() ## End(Not run)
Revokes an alias token so it can no longer be used for authentication.
xnat_token_invalidate(alias, secret, client = NULL)xnat_token_invalidate(alias, secret, client = NULL)
alias |
The alias portion of the token to invalidate. |
secret |
The secret portion of the token to invalidate. |
client |
Optional |
Invisibly returns TRUE if successful.
## Not run: # Invalidate a token xnat_token_invalidate(alias = "my_alias", secret = "my_secret") ## End(Not run)## Not run: # Invalidate a token xnat_token_invalidate(alias = "my_alias", secret = "my_secret") ## End(Not run)
Creates a new alias token for the authenticated user. Alias tokens can be used instead of passwords for authentication and can be invalidated independently.
xnat_token_issue(client = NULL)xnat_token_issue(client = NULL)
client |
Optional |
A list containing the token details:
alias: The alias (username-like identifier)
secret: The secret (password-like value)
estimatedExpirationTime: When the token expires
## Not run: authenticate_xnat(base_url = "https://central.xnat.org", username = "myuser", password = "mypass") token <- xnat_token_issue() # Use token$alias and token$secret for subsequent authentication ## End(Not run)## Not run: authenticate_xnat(base_url = "https://central.xnat.org", username = "myuser", password = "mypass") token <- xnat_token_issue() # Use token$alias and token$secret for subsequent authentication ## End(Not run)
Retrieves all active alias tokens for the authenticated user.
xnat_token_list(client = NULL)xnat_token_list(client = NULL)
client |
Optional |
A tibble of active tokens with columns:
alias: Token alias
xdatUserId: Associated user ID
estimatedExpirationTime: Expiration timestamp
## Not run: tokens <- xnat_token_list() print(tokens) ## End(Not run)## Not run: tokens <- xnat_token_list() print(tokens) ## End(Not run)
Checks if an alias token is still valid.
xnat_token_validate(alias, secret, client = NULL)xnat_token_validate(alias, secret, client = NULL)
alias |
The alias portion of the token. |
secret |
The secret portion of the token. |
client |
Optional |
A list with validation details:
valid: Logical, whether the token is valid
alias: The alias
estimatedExpirationTime: When the token expires (if valid
## Not run: # Check if a token is still valid result <- xnat_token_validate(alias = "my_alias", secret = "my_secret") if (result$valid) { message("Token is valid until ", result$estimatedExpirationTime) } ## End(Not run)## Not run: # Check if a token is still valid result <- xnat_token_validate(alias = "my_alias", secret = "my_secret") if (result$valid) { message("Token is valid until ", result$estimatedExpirationTime) } ## End(Not run)
Get current username
xnat_username(client = NULL)xnat_username(client = NULL)
client |
Optional |
The current username or NULL if not authenticated.
## Not run: xnat_username() ## End(Not run)## Not run: xnat_username() ## End(Not run)
Parses xnatR command-line arguments, dispatches to package functions, writes results to stdout, and returns a process-style integer status code.
xnatR_cli(args = commandArgs(trailingOnly = TRUE))xnatR_cli(args = commandArgs(trailingOnly = TRUE))
args |
Character vector of command-line arguments. |
Integer status code: 0 for success and 2 for usage/runtime errors.