Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
reference_book:r_coupling [2017/07/24 01:26]
admin
reference_book:r_coupling [2023/10/11 20:35] (current)
chrystian Updated recommended Ubuntu version.
Line 1: Line 1:
 ====== R Coupling ====== ====== R Coupling ======
  
-Dinamica EGO 4 provides some sophisticated ways of using as part of a model:+Integrating R into Dinamica EGO offers numerous advantages and opportunities for enhanced modeling capabilities. 
 + 
 +===== R Integration ===== 
 + 
 +==== Windows ==== 
 +Windows O.S. users of DinamicaEGO will need to install the [[:​plugins_7|Enhancement Plugin]]. 
 + 
 +==== Linux ==== 
 +For Linux O.S. users, we provide a detailed step-by-step tutorial on integrating DinamicaEGO with the environment. 
 + 
 +It is recommended to use Ubuntu LTS 22.04 for this tutorial. Please follow the steps below: 
 + 
 +**1. Download DinamicaEGO:​** Make sure that the downloaded file has permission to execute on your computer.  
 +        - Open your file manager and browse to the location ​of the AppImage 
 +        - Right-click on the AppImage and click the ‘Properties’ entry 
 +        - Switch to the Permissions tab 
 +        - Click the ‘Allow executing file as program’ 
 + 
 +**2. Install build-essentials:​** Open terminal and run the following command to install build-essentials: 
 +<code rsplus>​ 
 +sudo apt install build-essential 
 +</​code>​ 
 + 
 +**3. Install R:** Run the following command in the Linux terminal to install R: 
 +<code rsplus>​ 
 +sudo apt install r-base 
 +</​code>​ 
 +Make sure that the installed version is **4.3 or higher**. For specific instructions on installing R, please refer to the following link [ https://​cran.r-project.org/​bin/​linux/​ubuntu ]. 
 + 
 +**4. Configure R in DinamicaEGO:​** Now, let's configure DinamicaEGO to use the R installation you just performed. 
 + - Open DinamicaEGO. 
 + - In the menu, go to "​Tools"​ and select "​Options"​. 
 + - In the options window, click on the "​Integration"​ tab. 
 + - Within the "​Integration"​ tab, you will find an option called "​R"​. Click on it. 
 + - Check the box that says "Use alternative R installation"​. 
 + - Next, add the path to the R script (///​usr/​bin/​Rscript//​) - this may vary depending on how you installed R on your system. 
 + - Finally, click on "​Integrate with Alternative R" to complete the configuration. 
 + 
 +Once the procedure above is complete, the R scripts can be used by Calculate R Expression 
 + 
 +===== Example: Calculate R Expression ===== 
 +You can use the Calculate R expression function using the following example: 
 + 
 +<​code>​ 
 +# Install and load the required packages 
 +dinamicaPackage("​dplyr"​) 
 + 
 +# Read the table. 
 +input <- read.csv(s1) 
 + 
 +# Print the table  
 +print(input) 
 + 
 +# Use the table connected to the Calculate Expression. 
 +input2 = t1 
 + 
 +# Print the table  
 +print(input2) 
 + 
 +# Create a new table  
 +data <- data.frame( 
 +  name = c("​John",​ "​Jane",​ "​Mike"​),​ 
 +  age = c(25, 30, 35), 
 +  city = c("New York", "​London",​ "​Paris"​),​ 
 +  stringsAsFactors = FALSE 
 +
 + 
 +# Print the table  
 +print(data) 
 + 
 +# Calculate the arithmetic mean of column1 using dplyr 
 +mean_column1 <- data %>% summarise(mean_column1 = mean(age)) 
 +   
 +# Print the arithmetic mean of column1 
 +print(mean_column1$mean_column1) 
 +</​code>​
  
 ===== Exchanging Data between R and Dinamica ===== ===== Exchanging Data between R and Dinamica =====
  
-Dinamica EGO changes the game when it comes to exchanging data with R. Now Dinamica can use a specialized set of functors to send and receive data to and from R. R can use an special package (called "​[[:​external_communication|Dinamica]]"​) to send and receive data to and from Dinamica. This allows both tools to run simultaneously and none of them needs be in charge of controlling the simulation. Those specialized functors and package functions not only take care of data exchange between both tools, but also ensure that the calls are kept synchronized by blocking a receiver when the corresponding data is not yet available or the sender has not yet finished baking all the information.+Dinamica EGO changes the game when it comes to exchanging data with R. Now Dinamica can use a specialized set of functors to send and receive data to and from R. R can use an special package (called "​[[:​external_communication|Dinamica]]"​) to send and receive data to and from Dinamica. This allows both tools to run simultaneously and none of them needs be in charge of controlling the simulation. Those specialized functors and package functions not only take care of data exchange between both tools, but also ensure that the calls are kept synchronized by blocking a receiver when the corresponding data is not yet available or the sender has not yet finished baking all the information.
  
 {{ reference_book:​rcoupling4.png?​direct&​600 |}} {{ reference_book:​rcoupling4.png?​direct&​600 |}}
Line 27: Line 102:
 ===== Calculate R Expression ===== ===== Calculate R Expression =====
  
-Another way of coupling Dinamica and R is by using the functor [[:​Calculate R Expression]]. This functor allows an R script to be executed as part of a Dinamica model. This is usually the preferred way of coupling Dinamica and R. By default, Dinamica uses an [[:plugins_4|Enhancement Plugin]] containing a custom R interpreter. This interpreter comes with several pre-installed packages that can be used to define the R script.+Another way of coupling Dinamica and R is by using the functor [[:​Calculate R Expression]]. This functor allows an R script to be executed as part of a Dinamica model. This is usually the preferred way of coupling Dinamica and R. By default, Dinamica uses an [[:plugins_7|Enhancement Plugin]] containing a custom R interpreter. This interpreter comes with several pre-installed packages that can be used to define the R script.
  
 Data from Dinamica model can be passed using hooks just like it would be done with any functor from the Calculate family, including [[:​Calculate Lookup Table]], [[:​Calculate Map]] etc. Inside the R script, the data is accessible via binding variables such as ''​v1'',​ ''​s1'',​ ''​t1''​ etc. Data from Dinamica model can be passed using hooks just like it would be done with any functor from the Calculate family, including [[:​Calculate Lookup Table]], [[:​Calculate Map]] etc. Inside the R script, the data is accessible via binding variables such as ''​v1'',​ ''​s1'',​ ''​t1''​ etc.
Line 44: Line 119:
  
 {{ reference_book:​rcoupling10.png?​direct&​600 |}} {{ reference_book:​rcoupling10.png?​direct&​600 |}}
 +
 +<​note>​Some very simple examples showing how to use an R script as part of a Dinamica EGO model are provided as part of the dataset distributed with the Dinamica EGO application. Check the content of ''​Dataset\Examples\various_applications\calculate r expression''​.</​note>​
  
 ==== Selecting the R Interpreter to Use ==== ==== Selecting the R Interpreter to Use ====
  
-When using the [[:​Calculate R Expression]],​ Dinamica automatically invokes and stops the selected R interpreter to evaluate the expression as necessary. The user can control which R interpreter will be used by selecting appropriate options in "Tools -> Options | Integration | Calculate R Expression / R Integration"​. It is possible to choose between the default R interpreter provided with the [[:plugins_4|Enhancement Plugin]] or an alternative R installation installed independently.+When using the [[:​Calculate R Expression]],​ Dinamica automatically invokes and stops the selected R interpreter to evaluate the expression as necessary. The user can control which R interpreter will be used by selecting appropriate options in "Tools -> Options | Integration | Calculate R Expression / R Integration"​. It is possible to choose between the default R interpreter provided with the [[:plugins_7|Enhancement Plugin]] or an alternative R installation installed independently.
  
 {{ reference_book:​rcoupling11.png?​direct&​600 |}} {{ reference_book:​rcoupling11.png?​direct&​600 |}}
  
-Any R interpreter can be used as an alternative (and external) R installation provided that a compatible [[https://​cran.r-project.org/​bin/​windows/​Rtools/​|RTools]] and the [[:​external_communication|"​Dinamica"​]] package are installed. The biggest advantage of using a alternative version of R is that the user will be free to install and import any packages he wants. When using the default R interpreter from the [[:plugins_4|Enhancement Plugin]], the user is limited to the packages that come pre-installed with the platform. ​+Any R interpreter can be used as an alternative (and external) R installation provided that a compatible [[https://​cran.r-project.org/​bin/​windows/​Rtools/​|RTools]] and the [[:​external_communication|"​Dinamica"​]] package are installed. The biggest advantage of using a alternative version of R is that the user will be free to install and import any packages he wants. When using the default R interpreter from the [[:plugins_7|Enhancement Plugin]], the user is limited to the packages that come pre-installed with the platform. ​
  
 For more information about how to set an external R interpreter,​ see the documentation about the [[:​Calculate R Expression]] and [[:external communication]]. For more information about how to set an external R interpreter,​ see the documentation about the [[:​Calculate R Expression]] and [[:external communication]].