library(prolific.api)
prolific_api_access <- api_access(api_token = "")
# Create a new study
fancy_new_study <- prolific_study(
name = "A fancy study on Prolific",
external_study_url = "https://www.my_fancy_study_url.com",
completion_code = "123ab456cd78",
eligibility_requirements = list(),
estimated_completion_time = 1,
reward = 1,
total_available_places = 0
)
# Check the study's validity
print(fancy_new_study$validity_check())
# Whoops, better add a description and change the total_available_places,
# using RefClass and S4 methods for illustration
# both are equivalent, so only one of the two commands is required in practice
# RefClass variant
fancy_new_study$total_available_places <- 1L
# S4 variant
total_available_places(fancy_new_study) <- 1L
# RefClass variant
fancy_new_study$description <- "A fancy description"
# S4 variant
description(fancy_new_study) <- "A fancy description"
# Re-Check the study's validity
print(fancy_new_study$validity_check())
# Note: For the following code to work,
# you have to replace in the code above by the actual API token
if (FALSE) {
# Post the 'fancy_new_study' to Prolific - i.e. create it as a draft study on the platform
output_of_post <- prolific_api_access$access(
endpoint = "studies",
method = "post",
data = fancy_new_study
)
# Success: fancy_new_study got an ID - it is now a draft study on Prolific!
fancy_new_study$id
# Note: The output of the access() command with a prolific_study object as `data` argument
# is a pointer to this prolific_study object.
# The prolific_study object is updated by reference
print(tracemem(output_of_post) == tracemem(fancy_new_study))
# Change the study's name
name(fancy_new_study) <- "A NEW name for 'fancy_new_study'"
# Update (patch) the study on Prolific,
# using S4 methods for illustration
output_of_patch <- access(
prolific_api_access,
endpoint = c("studies", id(fancy_new_study)),
method = "patch",
data = fancy_new_study
)
# Note: As above, the output of the access() command is a pointer to the prolific_study object.
print(tracemem(output_of_post) == tracemem(fancy_new_study))
# Delete fancy_new_study
prolific_api_access$access(
endpoint = c("studies", id(fancy_new_study)),
method = "delete",
as_list = FALSE
)
}
Run the code above in your browser using DataLab