Export/Generate Scripts

LabKey Server provides a rich API for building client applications on top of LabKey Server -- for example, applications that retrieve and interact with data from the database. To get started building a client application, LabKey Server can generate a client script that retrieves a grid of data from the database. Adapt and extend the scripts capabilities to meet your needs. You 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.

To generate a script for a given dataset:

  • Navigate to the grid view of interest and click the Export button.
  • 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,height_cm,Weight_kg,Temp_C,SystolicBloodPressure,DiastolicBloodPressure,Pulse,
Respirations,Signature,Pregnancy,Language,ARV,ARVtype',
filterArray: null,
sort: null,
success: onSuccess,
error: 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's retrieving 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 in 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 dat from LabKey Server. Paste the script into your R console. See documentation on the Rlabkey CRAN package.

Related Topics


previousnext
 
expand allcollapse all