Javascript Editor - Data

Overview

Most scripts that you will write will interact with data in some way, either to

  • Create or download data that gets inserted into the workspace
  • Reads data from the workspace, in order to transform it into an analysis of some kind.

Scripts interact with data in the desktop through the use of the $val service.

Workspace Data

When you add data into your workspace, the data is given a name and becomes available to the scripts that you write. It can be retrieved by passing its name into the $val service by a script such as the following

let data = $val('data');


Scripts can be used to add data to the workspace as well as reading the data.

$val.set('data', [1,2,3,4,5]);


When data is set in a workspace in this way, it will flow through to the components in your workspace that depend on that data.

Loading Data

Loading data from an external server can be accomplished using the $url function.

let data = await $url('https://www.dataserver.com/data.json');


Sample datasets of randomly generated data can be found at Sample data

Data Transformations

Data that exists in the desktop and is accessible with $val can have a transformation associated with it. The transformation is applied to the data in $vall, and applied to the data every time it is set in $val. That is, the transformation acts as an editor that edits the data in $val. This can be used to edit a dataset in order to see the effect.

Note, only one transformation can be associated with any dataset at a time.

let transform = $val.transform('data-name'); //set a transformation that just selects the first record. $val.transform('data-name', data=>[data[0]]); //setting the transform to null will remove any transformation associated with the input name. $val.transform('data-name', null);
Note, the transformation can be set as either a function, or as text which evaluates to the function.

Saving Data to the Davinci Servers

In the process of running a set of computations, you may wish to save the results to a server, where you can retrieve the data later. This can be accomplished with a simple script. See Hosting on the Davinci Servers for more information.