R notebook examples

Here are some examples that demonstrate how to work with Cognos Analytics data sources in a notebook using the R programming language.

Tip: To switch your notebook's programming mode from Python to R, from the Kernel menu, select Change kernel > R.

A basic read

The following example reads a file called SampleFile_GOSales.xls and displays the first six rows of it.

A notebook cell that reads a file and displays the first six rows of it

Reading a file

The following example reads a file called SampleFile_GOSales.xls and specifies the nrows and usecols parameters to output the City and Quantity columns for the first two rows of data in the file.

A notebook cell that reads a file and then uses the nrows and usecols parameters to display 2 rows of data with City and Quantity values.

Reading part of a file

Specify iterator and chunksize to work with chunks of data, rather than the whole data source all at once. The following example reads 20,000 rows at a time from the SampleFile_GOSales.xls file. As the file is read, the number of rows in the chunk is displayed.

Notebook cells that read 20,000 rows at a time from the SampleFile_GOSales.xls file

Writing to a file

The following example writes the contents of a DataFrame table of data in to a file called regions sales, which is then stored in My content.

Notebook cells that write three rows of data to a file and stores it in My content

Reading a data module

A data module has relationships, aggregations, calculated columns, and so on, defined on its data. The read_data() method defines a section of data from a data module by selecting columns from the tables in the module. The data that is returned includes the relationships, aggregations, calculated columns, and so on, defined in the data module.

Specifying the read_data() method without the table_name parameter returns a DataFrame that contains the names of all the tables defined in the data module, as shown in the following example:

Notebook cell that reads a data module and displays its table names

In the following example, the pastry_inventory table in the Coffee sales and marketing data module is read and the columns Date and Quantity Sold are returned.

A notebook cell that reads pastry_inventory table in the Coffee sales and marketing data module and returns Date and Quantity Sold columns

Reading a package

Like a data module, a package has relationships, aggregations, calculated columns, and so on, defined on its data. In addition, a package logically groups data into query subjects and folders. You can use the read_data() method to navigate through the structure of a package by using the query_subject and folder_name parameters.

The following code reads the Go data warehouse package:

Notebook cell that uses the read_data() method to read the GO data warehouse package

Specifying the folder_name parameter returns the contents of all folders with that name in the package. If you want to return only the content of one specific folder, for example folder c, that's inside folder b, that's inside folder a, put a list of the folder names in the folder_name parameter. The following example uses the folder_name parameter to get all of the query subjects in the Employee expense folder:

Notebook cells that use the folder_name parameter to get all of the query subjects in the Employee expense folder

The following code returns the query items that are in the Employee expense fact query subject:

Notebook cells that returns the query items that are in the Employee expense fact query subject

The following code returns the query items that are in the Account query subject:

A notebook cell that returns the query items that are in the Account query subject

Reading the metadata for OLAP data in a Framework Manager package

The following code shows the metadata parameter set to true, which returns the query subjects in the package:

Notebook cells that show how to use the metadata parameter for a OLAP data in a Framework Manager package