Category:R

From Displayr
(Redirected from R)
Jump to navigation Jump to search

Analyses can be conducted in Displayr using the R Language. In every sense, when using R from within Displayr, you are using "pure" R. All the functions are written in R. Any R code is automatically sent over the internet to a server with a normal version of R installed on it. The results are then sent back, and presented to you in Displayr. While we have attempted to make it feel like Q and R are one-and-the-same, in reality they are completely different programs which "talk" to each other.

How to use R from within Displayr

There are a number of ways to use R from within Displayr:

  1. Entering R code directly into an Calculation (Calculation > Custom code).
  2. Creating an R Variable (Anything > Data > Variables > New > Custom Code > R and then either Numeric or Text depending on the type of variable you wish to create).
  3. Creating a new Data Set using R (in the Data Set tree, either + or + Add Data Set).
  4. Accessing pre-written R code using menus and forms. This is how most advanced analyses are conducted in Displayr (e.g., regression, principal components analysis). This is referred to as Standard R.
  5. Automatic updating. Any R code that is created via Custom Calculations, Standard R, or QScripts is set to automatically update when the inputs change (e.g., if the input data changes, if a new data file is created, or if other options are changed).

References to variables and questions

R code can refer to both Variables and Variable Sets, by typing either the variable name or the name of the Variable Set into the R CODE box of a Custom Calculation or R Variable.

References to tables

New tables and other types of outputs (strings, charts, variables, data files) can be created by manipulating tables. Every table has a Name, which can be viewed and changed by clicking on the table and going to Properties > GENERAL > Name (in the Object Inspector, on the right-side of the screen). A table's Name is automatically constructed from its Label, which is, in turn, automatically created from its contents. For example, a table that shows Age in its rows and Gender in its columns, will have a Label of Age by Gender and a Name of table.age.by.gender.

Where a table's reference name is the same as the name of a variable, question, or Custom Calculation, you can disambiguate using QTables$reference.name (e.g., QTables$table.2).

References to other Custom Calculations

Like a table, a Calculation has both a Name and a Label. The Name must be unique within a document. The Name is used to refer to other Calculations in code. For example, if one Calculation has a reference name of x, the code x * 2 in another Calculation will show the value of x multiplied by 2.

There are a number of ways of changing the Name of a Calculation:

  1. By changing the Name in the Object Inspector (Properties > GENERAL).
  2. By assigning a variable name in the last line of code. For example, the following code creates an Calculation with a Name of dog containing the string (or, in R parlance, character) Sherlock:
dog <- "Sherlock"

Avoiding ambiguous names

There are situations where two things may have the same Name. For example:

  • A table and a variable may both have the Name of Q2.
  • A Calculation and a table may both have the name brand.health.

Where this occurs, any R code code that refers to the non-unique name needs to be disambiguated, by using a Fully Qualified Name:

Object type Syntax Example
Calculations QROutputs$item.name QROutputs$r.output.3
Tables QTables$item.name QTables$age.by.gender.3
Variables Colas.sav$variable.name or Colas.sav$Variables$variable.name Colas.sav$d1 or Colas.sav$Variables$d1
Variable Sets Colas.sav$question.name, Colas.sav$Questions$question.name or Colas.sav$VariableSets$variable.set.name Colas.sav$Age, Colas.sav$Questions$Age or Colas.sav$VariableSets$Age

R Variables

An R Variable is a variable in a Data Set, created as follows:

  1. Anything > Data > Variables > New > Custom Code > R and then either Numeric or Text.
  2. Enter code written the R Language in the R CODE box. This code should create a vector, table or data-frame, with the same number of observations as in the data file.

R Data Sets

Data Sets can be added to a project using R: in the Data Sets tree, click + or + Add Data Set and then click R. These data sets need to have a tabular structure (e.g., be a data.frame).

Standard R

It is possible to do just about any form of data analysis using R by writing code. Where we think analyses are likely to be used by many of our users, we have made it available via a graphical user interface (i.e., menus and/or buttons and the like, without needing to write code). We refer to the analyses that we have made available via a graphical user interface as Standard R. The R Logo is used to mark menu items that use Standard R. See Standard R for more information about how Standard R items work and are created.

Updating

R code is automatically re-run whenever: