Using the R Console

To start a new R session, navigate to the R tab.

r icon, grabbed on 19.01.17, using XAP version 1.19.1 2740-8759a, from https://edcvaatest04.aridhiatest.net/#

R console, grabbed on 19.01.17, using XAP version 1.19.1 2740-8759a, from https://edcvaatest04.aridhiatest.net/#/workspaces/1152/rtools

Selecting a version of R

Before starting the console, you can select the version of R from the sidebar on the right. Workspaces provides a "legacy" version of R which can be used with scripts which have been migrated from an Analytixagility workspace (Workspaces version 1.25 or earlier).

Note, you cannot store the version of R in a script but must start the console in the right version before loading your script.

Import data tables with R libraries

You can import data tables using platform specific functions (see 'Running XAP and R commands' below) or using generic DBI and RPostgreSQL libraries: library(DBI) library(RPostgreSQL)

  • db_con <- dbConnect(PostgreSQL())
  • my_table_df <- dbReadTable(db_con, "my_table_name")

Import csv files from Files folder

Files can easily be imported using readr library:

  • library(readr)
  • dataset <- read_csv("~/files/your_subfolder/your_file.csv", trim_ws = FALSE)

Running XAP and R commands

Display the list of platform specific functions by typing ls(), which delivers a list of functions as shown below.

R console ls(), grabbed on 19.01.17, using XAP version 1.19.1 2740-8759a, from https://analytixagility.aridhia.net/#/workspaces/58/rtools Check that your uploaded data table exists. In this example we’ll look for a data table named iris, using the function xap.table_exist(“your_table name”) to see if R console can find it.

R console table_exists, grabbed on 19.01.17, using XAP version 1.19.1 2740-8759a, from https://analytixagility.aridhia.net/#/workspaces/58/rtools Use xap.read_table(“your_table name”) to read data from the database.

R console read_table, grabbed on 19.01.17, using XAP version 1.19.1 2740-8759a, from https://analytixagility.aridhia.net/#/workspaces/58/rtools Now you can use standard R commands and syntax to interact with your data, just like you would do it in a standard R editor, e.g. view summary data.

add icon, grabbed on 17.01.17, using XAP version 1.19.1 2740-8759a, from https://edcvaatest04.aridhiatest.net/#/workspaces/1150 You can upload or create new R files from the ‘Add’ menu using the New R File command. A new file editor tab will open. You will be requested to insert a file name, e.g. ‘iris_model’. Once you have created the R file you can place your code in the editor.

view summary data, grabbed on 19.01.17, using XAP version 1.19.1 2740-8759a, from https://analytixagility.aridhia.net/#/workspaces/58/rtools

iris_model.r, grabbed on 19.01.17, using XAP version 1.19.1 2740-8759a, from https://analytixagility.aridhia.net/#/workspaces/58/rtools You must save the R file before running it in the R console.

r session sidebar, grabbed on 19.01.17, using XAP version 1.19.1 2740-8759a, from https://analytixagility.aridhia.net/#/workspaces/58/rtools To run your R script from R console, open the console and click on ‘Click to start console’. Once connected, you can find your newly created file in the R session Files tab, located in the right-hand sidebar either by scrolling through the list of files, or using the ‘Search’ function. Execute it by clicking the action button, grabbed on 19.01.17, using XAP version 1.19.1 2740-8759a, from https://analytixagility.aridhia.net/#/workspaces/58/rtools arrow which appears when you hover over the file name. This will call xap.source() function on your script and execute it in the R console. You can click on the plot and save it as an image file:


r session plot 1, grabbed on 20.01.17, using XAP version 1.19.1 2740-8759a, from https://analytixagility.aridhia.net/#/workspaces/58/rtools

If your script returns plot outputs it will be displayed in the R Session sidebar under the Plots tab.

r session plot 2, grabbed on 20.01.17, using XAP version 1.19.1 2740-8759a, from https://analytixagility.aridhia.net/#/workspaces/58/rtools

Stopping an R script

stop, grabbed on 20.01.17, using XAP version 1.19.1 2740-8759a, from https://analytixagility.aridhia.net/#/workspaces/58/rtools

If you run an R script or command and notice that it is taking a long time to complete, you can stop it by clicking the ‘Stop’ button to halt its execution.

Updated on November 30, 2020

Was this article helpful?

Related Articles

Not the solution you were looking for?
Click the link below to submit a support ticket
CONTACT SERVICE DESK