Monday, September 17, 2018

RStudio Data Structure Helpers

Frequently an R function outputs a data object that has internal structure and perhaps other nested objects.The internal structure of an R object can be reviewed in more detail with the structure command

str(object)

Furthermore, RStudio provides multiple helpers for accessing nested structures in the generated objects. Let's quickly review what can be done.

Start with the Global Environment viewer

The right pane of an RStudio R session displays the session Global Environment.



The icons on the right of the list of session objects (indicated in the screeenshot by blue and yellow arows) allow you to easily review and drill down to the structure of these objects.

For example, clicking on the table-like icon opens a tab in RStudio with a tabular display of the object (usually a data frame)

Complex (non-Tabular) objects


Complex objects are identified by their type.

For example, the tsneT1 object is of type '2Dreduction', and we can reveal its complex structure by clicking on the magnifying glass icon to the right of the list. This opens a new RStudio editor tab and displays the following:

Reviewing elements of complex objects 

We can still further review the elements of the tsne complex object.
Hovering over the elements of the complex tsneT1, object reveals a 'console code' symbol to the right.
Clicking on the 'embedding' list element enters in the console the R command for exploring this element's structure.

Exporting elements of complex objects


Finally, we may need to export one of the elements of the complex object for further analysis. In my case, I was interested in exporting the t-SNE embeddings (dimenstions) so I used the following command

## Export TSNE and labels
write.csv(tsneT1[["embedding"]], file.choose(), row.names=LabelsT1)

This exported the tsneT1 embeddings to a file on the file  system

In Summary

These are some quick tips on how to use the RStudio structure viewing tools for exploring R object data structures for debugging and analysis purposes. I hope you find them useful

No comments:

Post a Comment