Tutorial: Query LabKey Server from RStudio

Documentation
This tutorial shows you how to pull data directly from LabKey Server into RStudio for analysis and visualization.

Tutorial Steps:

Install RStudio

  • If necessary, install R version 3.0.1 or later. If you already have R installed, you can skip this step.
  • If necessary, install RStudio Desktop on your local machine. If you already have RStudio installed, you can skip this step.

Install Rlabkey Package

  • Open RStudio.
  • On the Console enter the following:
install.packages("Rlabkey")
  • Follow any prompts to complete the installation.

Query Public Data

  • This will auto-generate the R code that queries the Physical Exam data:
library(Rlabkey)

# Select rows into a data frame called 'labkey.data'

labkey.data <- labkey.selectRows(
baseUrl="https://www.labkey.org",
folderPath="/Explore/Research Study",
schemaName="study",
queryName="PhysicalExam",
viewName="",
colSelect="ParticipantId,ParticipantVisit/Visit,date,weight_kg,temperature_C,systolicBP,diastolicBP,pulse",
colFilter=NULL,
containerFilter=NULL,
colNameOpt="rname"
)
  • Copy this code to your clipboard.
  • Go to RStudio, paste the code into the Console tab, and press the Enter key.
  • The query results will be displayed on the 'labkey.data' tab.
  • Enter the following line into the Console to visualize the data.
plot(labkey.data$systolicbp, labkey.data$diastolicbp)
  • Note that the auto-generated code will include any filters applied to the grid. For example, if you filter Physical Exam for records where temperature is greater than 37, then the auto-generated code will include a colFilter property as below:
library(Rlabkey)

# Select rows into a data frame called 'labkey.data'

labkey.data <- labkey.selectRows(
baseUrl="https://www.labkey.org",
folderPath="/Explore/Research Study",
schemaName="study",
queryName="PhysicalExam",
viewName="",
colSelect="ParticipantId,ParticipantVisit/Visit,date,weight_kg,temperature_C,systolicBP,diastolicBP,pulse",
colFilter=makeFilter(c("temperature_C", "GREATER_THAN", "37")),
containerFilter=NULL,
colNameOpt="rname"
)

Handling Login/Authentication

  • To query non-public data, that is, data that requires a login/authentication step to access, you have three options:

Query Non-public Data

  • Once you have set up authentication for RStudio, the process of querying is the same as above:
    • Go to the desired data grid. Apply filters if necessary.
    • Auto-generate the R code using Export > Script > R > Create Script.
    • Use that code in RStudio.

Discussion

Was this content helpful?

Log in or register an account to provide feedback


previousnext
 
expand all collapse all