The aim of this tutorial is to easily show data and to provide results with a simple linear regression.
We will use the preloaded
Begin a new project with the File menu, then New and Project… and name our new project “LinearRegression”.
Your project appears with two files : LinearRegression.ppro, which contains the general properties of the program, and main.pseq which will contains our sequence of tasks (workflow).
The file main.pseq will contain the sequence of tasks as a chartflow and RPGM will execute each step by following the order of the chartflow. It has a Start step and we will connect our next steps to this one.
Push the F5 key to launch the program. It calls the main.pseq sequence which only has the Start step, it doesn’t do anything right now but we are going to add some steps.
Our first goal is to show the data of the
cars variable which already exists in R. To do so, we must create a graphical interface and add it to the main.pseq file. Do a right click on the File explorer and choose Create GUI or use the menu File -> New -> GUI, and name the file ShowInputs.pgui. Before populating the interface, we must add it in our sequence. Go back on main.pseq and click on the GUI button to add a GUI to the sequence. On the right side of the chartflow, in the Properties settings, choose ShowInputs.pgui for the file. An easier way to add a step in the sequence is to drag and drop ShowInputs.pgui from the File explorer on the left into the sequence editor. You can move the boxes of the steps with the mouse. Now, add an arrow from the Start step to our new GUI step with the mouseby drag and dropping.
You can push F5 to see that we now enter in the GUI step, but it is empty for now. Go back in the ShowInputs.pgui file with a double click on its step in main.pseq or from the File explorer. In the GUI, choose the widget Label which is the default one and add the first element by clicking on _Add__.
Use the viewer of the widget to see how each field works. The field Margin Top can be set to 0 in order to put an help text just below a previous widget.
By pressing F5, RPGM Client starts with the current state of the program:
It is not yet useful, but it gives an idea of how everything works. We will now add a Grid widget for displaying the data
carsis an R expression which must be evaluated with R. Otherwise RPGM will try to set the value to “cars” as a string of 4 characters.
The ShowInputs.pgui file should look like this:
By pressing F5, the result is:
We will now write a R code for computing the linear regression. Use a right click on the File explorer and choose Create R script or use the menu File -> New -> R script, and name the new file Regression.R. Like for ShowInputs.pgui, we must add the script in the Sequencer. Go back on main.pseq and add a R Script step linked to the Regression.R file and connect it to ShowInputs.pgui so that the R file will be executed after our graphical interface.
We write the following code which computes the linear regression of dist against speed of the cars data. It also provides a graphic that we will display in another step.
png("regression.png") plot(cars, pch=16, col="darkblue", main="Linear regression") reg <- lm(dist ~ speed, data=cars) abline(reg$coefficients, col="red") dev.off()
With F5, execute the program. The output folder can be open at the end where we have our graphic:
You get this graphic in the output folder. Now it’s time to show the graphic to the user, we will display it in a GUI directly in the Client. Create a new .pgui and call it Results, add it to main.pseq. The graphic will be display with an Image widget.
In Results.pgui, create a Label field with Results as value and 32 for the font size. Then add a second widget of type Image and set the value field to regression.png.
We get the following result when executing the program:
We finish by tweaking the project file:
Push F5 and watch how the top bar is updated.