LabKey Server provides a rich API for building client applications that retrieve and interact with data from the database. To get started building a client application, LabKey Server can generate a script that retrieves a grid of data from the database. Adapt and extend the script's capabilities to meet your needs. Developers can generate a script snippet for any data grid. The following script languages are supported:
  • Java
  • JavaScript
  • Perl
  • Python
  • R
  • SAS
  • Stable URL
You can also generate a Stable URL from this export menu which can be used to reload the query, preserving any filters, sorts, or custom sets of columns.

Export/Generate Scripts

To generate a script for a given dataset:

  • Navigate to the grid view of interest and click (Export).
  • Select the Script tab and select an available language: Java, JavaScript, Perl, Python, R, or SAS.
  • Click Create Script to generate a script.

For example, the Physical Exam dataset in the LabKey Demo Study can be retrieved using this snippet of JavaScript:

<script type="text/javascript">

LABKEY.Query.selectRows({
requiredVersion: 9.1,
schemaName: 'study',
queryName: 'Physical Exam',
columns: 'ParticipantId,date,Weight_kg,Temp_C,SystolicBloodPressure,DiastolicBloodPressure,Pulse,Signature,Pregnancy,Language,ARV,height_cm,DataSets/Demographics/Group',
filterArray: null,
success: onSuccess,
failure: onError
});

function onSuccess(results) {
var data = '';
var length = Math.min(10, results.rows.length);

// Display first 10 rows in a popup dialog
for (var idxRow = 0; idxRow < length; idxRow++) {
var row = results.rows[idxRow];

for (var col in row) {
data = data + row[col].value + ' ';
}

data = data + 'n';
}

alert(data);
}

function onError(errorInfo) {
alert(errorInfo.exception);
}

</script>

Filters. Filters that have been applied to the grid view are included in the script. Note that some module actions apply special filters to the data (e.g., an assay may filter based on a "run" parameter in the URL); these filters are not included in the exported script. Always test the generated script to verify it will retrieve the data you expect, and modify the filter parameters as appropriate.

Column List. The script explicitly includes a column list so the column names are obvious and easily usable in the code.

Foreign Tables. The name for a lookup column will be the name of the column in the base table, which will return the raw foreign key value. If you want a column from the foreign table, you need to include that explicitly in your view before generating the script, or add "/<ft-column-name>" to the field key.

Use Exported Scripts

JavaScript Examples.

  • You can paste a script into a <script> block on the source tab of an HTML wiki.
  • For a better development experience, you can create a custom module. HTML pages in that module can use the script to create custom interfaces.
R Examples
  • Use the script in a custom R view.
  • Use the script within an external R environment to retrieve data from LabKey Server. Paste the script into your R console. Learn more in this topic: Rlabkey Package.

Related Topics

Was this content helpful?

Log in or register an account to provide feedback


previousnext
 
expand allcollapse all