Specimen, Set primaryType and derivativeType using R API

LabKey Support Forum
Specimen, Set primaryType and derivativeType using R API Anthony Corbett  2017-09-15 20:37
Status: Closed
 

I believe PrimaryType and DerivativeType are lookups to study.SpecimenPrimaryType.RowId and study.SpecimenDerivative.RowId respectively. Thus, you need to use the integer RowId of those tables.

I would query those tables to get the RowId and whatever column has your string label and then create a named vector to do the lookups. The following works in 17.1:

library(Rlabkey)
library(magrittr)

primaryTypes <- labkey.selectRows(
  baseUrl="https://localhost:8080/labkey", 
  folderPath="/containerPath", 
  schemaName="study", 
  queryName="SpecimenPrimaryType", 
  colSelect=c("RowId","Description"), 
  colNameOpt = 'rname'
) %$% setNames(rowid, as.character(description))

derivativeTypes <- labkey.selectRows(
  baseUrl="https://localhost:8080/labkey", 
  folderPath="/containerPath", 
  schemaName="study", 
  queryName="SpecimenDerivative", 
  colSelect=c("RowId","Description"), 
  colNameOpt = 'rname'
) %$% setNames(rowid, as.character(description))

Now you can use those for lookups:

toadd = data.frame(patient_id = "ptid" , GlobalUniqueId = "test_1", sequenceNum = 1, drawtimestamp = "20170303", primaryType = primaryTypes[["Tissue"]], derivativeType = derivativeTypes[["gDNA"]], stringsAsFactors = FALSE)

If you have many rows (e.g. imported from flat file or excel) where primaryType and derivativeType columns are your text labels already I would use dplyr::mutate to replace the string value with the rowid using the named vector lookup:

library(dplyr)
toadd <- mutate(toadd, primaryType=primaryTypes[primaryType], derivativeType=derivativeTypes[derivativeType])