To start a new R session, navigate to the R tab.
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.
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.
Use xap.read_table(“your_table name”) to read data from the database.
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.
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.
You must save the R file before running it in the R console.
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
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:
If your script returns plot outputs it will be displayed in the R Session sidebar under the Plots tab.
Stopping an R script
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.