Chapter 6. AirXCell module : The R Code Editor

The R Code Editor module offers the user the possibility to edit an R source code and execute it. The user can execute the current line (the one where the user left the cursor), a selection or even the whole file.

The R Code Editor supports most common development text editor features such as copy / paste, indentation, etc.

A new empty R Code Editor can be added to the workspace by using Workspace → New Module → New Editor ()

A newly created R Code Editor looks as shown on Figure 6.1, “New R Code Editor”.

Figure 6.1. New R Code Editor

New R Code Editor


6.1. The R Code Editor Toolbar

Most functionalities aside from actually editing the R source code are available from the toolbar:

  • Import File : import a text file inside the R Code Editor.

  • Export File : export the R Code Editor content to an R source code text file.

  • Cut : cut the selected text and put in the clipboard. (Ctrl + X)

  • Copy : copy the selected text and put in the clipboard. (Ctrl + C)

  • Paste : paste the clipboard content at the current cursor location. (Ctrl + V)

  • Undo : undo last action (edit). (Ctrl + Z)

  • Redo : redo last (undone) action (edit). (Ctrl + Y)

  • Select all text : select the whole editor content. (Ctrl + A)

  • Delete selection : delete the text snippet currently selected on the Editor. (Del)

  • Find : find a text pattern in the current Editor content. Replace is also available from here. (Ctrl + F)

  • Run whole Editor content : run the whole Editor content in the R environment. (Ctrl + Shift + A)

  • Run selected text : run the text currently selected in the Editor in the R environment. (Ctrl + Shift + S)

  • Run current line : run the current line (the one where the user left the cursor). (Ctrl + Shift + C)

6.2. Loading files in the R environment

As described in Section 2.3, “AirXCell limitations”, it is not yet possible in AirXCell to upload a data file on the remote R environment. There is however a workaround to this limitation since it is possible to import a CSV file within a calculation sheet.

Illustrational example

Let's assume one is editing the following code within an R Code Editor

# CONTROL CARD

# data contains a list of mean and standard deviation values for each sample
# on every row. 
read.csv ("control_data.csv") -> sample_data

# let's rename the values. we use x.bar as the mean per sample
# and sd_x as the standard deviation
sample_data$mean -> x.bar
sample_data$sd -> sd_x

# let's compute the mean of the sample means
x.barbar <- mean(x.bar)

# and the mean of the standard deviation
sd_x.bar <- mean(sd_x)

# adaptations values for samples of size 10
B3 <- 0.284 ; B4 <- 1.716; A3 <- 0.975

# sd_x.CL is the control limit
sd_x.CL <- sd_x.bar*c(B3,1,B4)

# x.bar.CL is the control limit
x.bar.CL <- x.barbar + c(-A3,0,A3)*sd_x.bar

# now let's display how the samples behave
chart (x.bar,pch=20,ylim=c(3.0,5.5))
lines(x.bar)
abline(h=c(x.bar.CL,3.2,5.2),col=c("blue","black","blue","red","red"))
text(c(rep(17.7,3),rep(17.4,2)),c(x.bar.CL,3.2,5.2),label=c("LCL","","UCL","lower tol.","upper tol."),pos=3)       
       

This program assumes that on the R side, a file named control_data.csv exists and contains the sample values in the form :

mean,sd
4.51,0.185
4.49,0.367
4.33,0.267
...
4.30,0.275       
       

where every row gives statistical informations on samples as measured on a production line. The first column gives the mean value for each sample while the second column gives the standard deviation for each sample.

Now the problem with this is that there is no mean to make the line read.csv ("control_data.csv") -> sample_data work since there is no way to uplad the data file control_data.csv on the remote R environment.

This is where the file import feature (5.9) from the calculation sheet (5) comes in help. It is perfectly possible to import the CSV file control_data.csv in a new spreadsheet using the file import feature (5.9) to get the values from the CSV file within the R environment.

Let's assume the control_data.csv has been imported in the calculation sheet with internal ID 8 (as shown for instance by the name of the calculation sheet being Sheet8). One can rewrite the following lines from the R program:

read.csv ("control_data.csv") -> sample_data

# let's rename the values. we use x.bar as the mean per sample
# and sd_x as the standard deviation
sample_data$mean -> x.bar
sample_data$sd -> sd_x       
       

this way :

# let's rename the values. we use x.bar as the mean per sample
# and sd_x as the standard deviation
Sheet8$val[2:19,1] -> x.bar
Sheet8$val[2:19,2] -> sd_x     
       

and the program works

This even has a strong advantage : the values are clearly shown on a user friendy calculation sheet module and the user can change them simply and rerun the program as often as she wants.

6.3. Import / Export

The R Code Editor enables the user to import a text file as an R source code and to export the current editor content to an R source code test file. This is really no different than the general file import / export system (4.5.1) already discussed.