How to create and append to a dataset using R labkey API?

LabKey Support Forum
How to create and append to a dataset using R labkey API? kristen dang  2021-04-23 10:42
Status: Active
 

Hello,
I am having several errors when trying to use the R labkey API to create and load a dataset. I am able to load this same file as a dataset using the GUI, but cannot seem to do it with the R API.

When creating the design, I get an error about reserved fields. In the GUI I resolve this by matching specific upload file column names with reserved field names. I don't know how to do this in R:

 fields <- labkey.domain.inferFields(baseUrl=labkey.getBaseUrl(), folderPath=fp, df=inDF)
 dd <- labkey.domain.createDesign(name=batch$batch, fields=fields)
 k = which(dd$fields$name %in% c("ParticipantId", "visitCode", "sampleName"))
 dd$fields$isPrimaryKey[k] = TRUE

    eaLabkeyList = labkey.domain.create(baseUrl=labkey.getBaseUrl(), folderPath=fp, domainKind="StudyDatasetVisit", domainDesign=dd, options=list(categoryName="SequencingTest",demographics=FALSE,useTimeKeyField=FALSE,keyPropertyName="sampleName"))

Error:

HTTP request was unsuccessful. Status code = 500, Error message = java.lang.IllegalArgumentException: Property: ParticipantId is reserved or exists in the current domain.

Once I create this file in the web UI, I'm not able to append other data to it by upload since the column names don't match. Is there some documentation for how to take care of the key name matching so I can do this in R?

Thanks for any help.

 
 
chetc (LabKey Support) responded:  2021-05-06 12:55
Status: Closed

Hello Kristen,

There may be an issue with "labkey.domain.createAndLoad", a bug has been filed for this.

I tested "labkey.domain.create" endpoint with the result of "labkey.domain.inferFields" as the "domainDesign", and initially I did get the same error you did. But if you remove "ParticipantId" from the the dataframe (inDF in this case) it should resolve the error and create the domain. Even though "ParticipantId" in not a part of the design you passed in it will still be created.

After the domain has been created you can use "labkey.insertRows" to insert data into the dataset. When you do this be sure to include all of the required fields. I got locked up for a while since I wasn't passing in the date.

Attached to this post you will find a short R example that creates a dataset using fields inferred from a dataframe. Then insertRows was used to load data into the dataset.

Thanks,
Chet