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.
A basic read
The following example reads a file called SampleFile_GOSales.xls 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.
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.
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.
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:
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.
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:
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:
The following code 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:
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: