<jupyter_start><jupyter_text>Below is a demo for exporting data from RedCap. Dependencies redcapAPI library, API_KEY, and project url.<jupyter_code># Importing the library
library(redcapAPI)<jupyter_output><empty_output><jupyter_text>Establishing a connection to the redcap project.<jupyter_code>RedCap_URL <- "<your-project-url>"
API_KEY <- "<your-api-key>"
rcon <- redcapConnection(url=RedCap_URL, token=API_KEY)<jupyter_output><empty_output><jupyter_text>Exporting project metadata from redcap.  The metadata consists of various keys like field_type, form_name, field_name, identifier etc.<jupyter_code>export_metadata <- exportMetaData(rcon,
                  fields = NULL,
                  forms = NULL,
)

export_metadata<jupyter_output><empty_output><jupyter_text>Exporting Records from RedCap .<jupyter_code>export <- suppressWarnings(exportRecords(
   rcon,
   fields = NULL,
    forms = NULL,
    recodrd= NULL
 ))

export<jupyter_output><empty_output><jupyter_text>Exporting Records by specifying different parameters such as records, fields,and forms.<jupyter_code># This is how a specific field can be exported. 
show_fields =exportRecords(
  rcon,
  fields = 'eligibility_1',
  records = NULL,
    )

show_fields
# This is how a specific form can be exported by specifying the form name in the form paremeter.
show_forms =exportRecords(
    rcon,
    forms = "consent_to_participate",
    records = NULL,
    )
show_forms
# By assigning NULL value to the exportRecords function parameters all of the records can be exported at once.
show_all_records =suppressWarnings(exportRecords(
    rcon,
    forms = NULL,
    records = NULL,
))

show_all_records
# To save and read files from jupyter notebook the R function setwd() can be used. This function sets the working directory.
# The home directory consists of jovyan, public, <user's directory>, and shared subdirectories.
# Using the setwd function the working directory can be changed for example: setwd("/home/jovyan"). This just
# set the working directory to /home/jovyan. Now all files that are saved or being read in will be from /home/jovyan.
# NOTE: Once the working directory is set the chosen directory will be the working directory unless changed. 

# Set the working dorectory for saving and reading in files. 
setwd("/home/shared/")

# To save data in csv format.
write.csv(export_metadata, "<prefered-file-name>.csv", row.names = FALSE)
# To read data from csv file. 
read.csv("<file-name>.csv")
# Set the working directory to /home/shared using setwd() from the above code. 
# To save your code to the persistent disk, use file.copy("<source file>", "<destination file>").
# Below code demonstrates, copying the current working file into /home/shared/.
file.copy("/home/jovyan/blackbalsam/clinical-demo-notebooks/RedCapAPIRDemo.ipynb", "RedCapAPIRDemo-backup.ipynb")
# To see the file that has been saved using the file.save, use file.show(<file name to view in the working directory>).
# Below code shows the file saved in /home/shared/.
file.show("RedCapAPIRDemo-backup.ipynb")
# To start working on the saved file in the peristent disk, make a copy of the file into /home/jovyan/blackbalsam, 
# use file.copy("<source file>", "<destination file>").
# Below code shows, copying the file from /home/shared/ to the notebook home directory /home/jovyan/.
file.copy("RedCapAPIRDemo-backup.ipynb", "/home/jovyan/blackbalsam/clinical-demo-notebooks/RedCapAPIRDemo-backup.ipynb")
# To remove a file from a directory, use file.remove("<file name>"). Below code deletes the files saved to /home/shared
file.remove("RedCapAPIRDemo-backup.ipynb")
file.remove("/home/jovyan/blackbalsam/clinical-demo-notebooks/RedCapAPIRDemo-backup.ipynb")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0946271050521251, 'fraction_numerical': 0.0002673082063619353, 'mean_word_length': 4.8105590062111805, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '641322', 'license': 'MIT', 'n_tokens_mistral': 1167, 'n_tokens_neox': 1125, 'n_words': 385, 'path': 'clinical-demo-notebooks/RedCapAPIRDemo.ipynb', 'repo_name': 'helxplatform/blackbalsam', 'size': '3741'}