This article describes how to extract data from a Standard R object in Displayr:
R analyses that are "built-in" to Displayr are referred to as Standard R. These are analyses where the R code has been connected to a graphical user interface (GUI) via the menu and include pre-written R code.
A built-in R-based analysis object such as a regression, visualization, or segmentation output. In this example we are using a regression output with the Name of glm. You find the Name under Properties > GENERAL in the object inspector.
In order to extract data from an R object such as a regression output, we need to know a bit more information about its structure.
The simplest way to do this is to create a new Calculation via Calculation > Custom Code containing the code:
The result contains a table that tells us what objects are contained within glm.
We can see that the 5th item is n.predictors. So, we can extract this item
However, when we do this we get a bit of baggage with it. Rather than just getting the result, we are also getting the name as well. If we just want the result, we instead use
Objects contained within the output can have sub-objects (i.e., there is a hierarchy of objects). To access objects that reside in other objects, we can use
names(glm[]). The result will reveal the names of the items in the 17th item of glm, which is the summary. We can see that the 9th item is adj.r.squared. This means that to extract the adjusted R-squared statistic we could use
glm[][] and get the below (after increasing the number of decimals):
The example above is a bit messy, however. If you type
glm[][] instead of
glm[][] you will get a different answer, but you may not spot it. Fortunately, we can instead reference parts of an object by name. For example:
This example uses $ to extract the whole table of coefficients.
Note, as the table of coefficients is also the final visualized data, this can also be referenced using the attributes function:
While in most cases the names function will be sufficient to extract the relevant information from your output, the attributes function can also extract the output class, and, if a visualization, the chart type, chart data and various other chart settings.
Using index and $ together
We can combine subscripting with using $. For example, we could extract all the standard errors from the regression model above by typing
glm$summary$coefficients[, 2] or, equivalently,
By having nothing before the comma in the bracket, it returns all the rows. By specifying just the column in the bracket, it returns just that column. And, do note that the actual text we have used here instead of Std. Error is actually a bit different to that used in the original output table at the top (Standard Error). The original table has been formatted for readability.