PageSourceSearch

https://gama-platform.org/assets/js/8f1ab682.31fbdc6e.js

js gama-platform.org collected 2026-10-03 22:47:49 UTC 12,694 bytes, 1 lines download raw bytes

1"use strict";(self.webpackChunk=self.webpackChunk||[]).push([["1170"],{88930(e,n,l){l.r(n),l.d(n,{metadata:()=>r,default:()=>h,frontMatter:()=>a,contentTitle:()=>s,toc:()=>d,assets:()=>o});var r=JSON.parse('{"id":"CallingR","title":"Calling R","description":"[//]: # (startConcept|call_r)","source":"@site/versioned_docs/version-1.8.1/CallingR.md","sourceDirName":".","slug":"/CallingR","permalink":"/wiki/1.8.1/CallingR","draft":false,"unlisted":false,"editUrl":"https://github.com/gama-platform/gama/wiki/CallingR/_edit","tags":[],"version":"1.8.1","frontMatter":{"title":"Calling R"},"sidebar":"main","previous":{"title":"Using Database Access","permalink":"/wiki/1.8.1/UsingDatabase"},"next":{"title":"Using FIPA ACL","permalink":"/wiki/1.8.1/UsingFIPAACL"}}'),i=l(74848),t=l(28453);let a={title:"Calling R"},s,o={},d=[{value:"Introduction",id:"introduction",level:2},{value:"Table of contents",id:"table-of-contents",level:2},{value:"Configuration in GAMA",id:"configuration-in-gama",level:2},{value:"Calling R from GAML",id:"calling-r-from-gaml",level:2},{value:"Calling the built-in operators",id:"calling-the-built-in-operators",level:3},{value:"Example 1",id:"example-1",level:4},{value:"Calling R codes from a text file (<strong>.R,</strong>.txt) WITHOUT the parameters",id:"calling-r-codes-from-a-text-file-rtxt-without-the-parameters",level:3},{value:"Example 2",id:"example-2",level:4},{value:"Correlation.R file",id:"correlationr-file",level:4},{value:"Output",id:"output",level:3},{value:"Example 3",id:"example-3",level:4},{value:"RandomForest.R file",id:"randomforestr-file",level:4},{value:"Output",id:"output-1",level:4},{value:"Calling R codes from a text file (.R, .txt) WITH the parameters",id:"calling-r-codes-from-a-text-file-r-txt-with-the-parameters",level:3},{value:"Example 4",id:"example-4",level:4},{value:"Mean.R file",id:"meanr-file",level:4},{value:"Output",id:"output-2",level:3},{value:"Example 5",id:"example-5",level:4},{value:"AddParam.R file",id:"addparamr-file",level:4},{value:"Output",id:"output-3",level:4}];function c(e){let n={a:"a",code:"code",h2:"h2",h3:"h3",h4:"h4",li:"li",ol:"ol",p:"p",pre:"pre",strong:"strong",ul:"ul",...(0,t.R)(),...e.components};return(0,i.jsxs)(i.Fragment,{children:[(0,i.jsx)(n.h2,{id:"introduction",children:"Introduction"}),"\n",(0,i.jsxs)(n.p,{children:["R language is one of powerful data mining tools, and its community is very large in the world (See the website: ",(0,i.jsx)(n.a,{href:"http://www.r-project.org/",children:"http://www.r-project.org/"}),"). Adding the R language into GAMA is our strong endeavors to accelerate many statistical, data mining tools into GAMA."]}),"\n",(0,i.jsxs)(n.p,{children:["RCaller 2.0 package (Website: ",(0,i.jsx)(n.a,{href:"http://code.google.com/p/rcaller/",children:"http://code.google.com/p/rcaller/"}),") is used for GAMA 1.6.1."]}),"\n",(0,i.jsx)(n.h2,{id:"table-of-contents",children:"Table of contents"}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsxs)(n.li,{children:[(0,i.jsx)(n.a,{href:"#introduction",children:"Introduction"}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.a,{href:"#configuration-in-gama",children:"Configuration in GAMA"})}),"\n",(0,i.jsxs)(n.li,{children:[(0,i.jsx)(n.a,{href:"#calling-r-from-gaml",children:"Calling R from GAML"}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsxs)(n.li,{children:[(0,i.jsx)(n.a,{href:"#calling-the-built-in-operators",children:"Calling the built-in operators"}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.a,{href:"#example-1",children:"Example 1"})}),"\n"]}),"\n"]}),"\n",(0,i.jsxs)(n.li,{children:[(0,i.jsx)(n.a,{href:"#calling-r-codes-from-a-text-file-txt-without-the-parameters",children:"Calling R codes from a text file (.txt) WITHOUT the parameters"}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.a,{href:"#example-2",children:"Example 2"})}),"\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.a,{href:"#correlationr-file",children:"Correlation.R file"})}),"\n"]}),"\n"]}),"\n",(0,i.jsxs)(n.li,{children:[(0,i.jsx)(n.a,{href:"#output",children:"Output"}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.a,{href:"#example-3",children:"Example 3"})}),"\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.a,{href:"#randomforestr-file",children:"RandomForest.R file"})}),"\n"]}),"\n"]}),"\n"]}),"\n"]}),"\n"]}),"\n"]}),"\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.a,{href:"#load-the-package",children:"Load the package:"})}),"\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.a,{href:"#read-data-from-iris",children:"Read data from iris:"})}),"\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.a,{href:"#build-the-decision-tree",children:"Build the decision tree:"})}),"\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.a,{href:"#build-the-random-forest-of-50-decision-trees",children:"Build the random forest of 50 decision trees:"})}),"\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.a,{href:"#predict-the-acceptance-of-test-set",children:"Predict the acceptance of test set
1:"})}),"\n",(0,i.jsxs)(n.li,{children:[(0,i.jsx)(n.a,{href:"#calculate-the-accuracy",children:"Calculate the accuracy:"}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.a,{href:"#output",children:"Output"})}),"\n",(0,i.jsxs)(n.li,{children:[(0,i.jsx)(n.a,{href:"#calling-r-codes-from-a-text-file-r-txt-with-the-parameters",children:"Calling R codes from a text file (.R, .txt) WITH the parameters"}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.a,{href:"#example-4",children:"Example 4"})}),"\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.a,{href:"#meanr-file",children:"Mean.R file"})}),"\n"]}),"\n"]}),"\n",(0,i.jsxs)(n.li,{children:[(0,i.jsx)(n.a,{href:"#output",children:"Output"}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.a,{href:"#example-5",children:"Example 5"})}),"\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.a,{href:"#addparamr-file",children:"AddParam.R file"})}),"\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.a,{href:"#output",children:"Output"})}),"\n"]}),"\n"]}),"\n"]}),"\n"]}),"\n"]}),"\n",(0,i.jsx)(n.h2,{id:"configuration-in-gama",children:"Configuration in GAMA"}),"\n",(0,i.jsxs)(n.ol,{children:["\n",(0,i.jsxs)(n.li,{children:["\n",(0,i.jsx)(n.p,{children:"Install R language into your computer."}),"\n"]}),"\n",(0,i.jsxs)(n.li,{children:["\n",(0,i.jsxs)(n.p,{children:["In GAMA, select menu option: ",(0,i.jsx)(n.strong,{children:"Edit/Preferences"}),"."]}),"\n"]}),"\n",(0,i.jsxs)(n.li,{children:["\n",(0,i.jsxs)(n.p,{children:['In "',(0,i.jsx)(n.strong,{children:"Config RScript's path"}),'", browse to your "',(0,i.jsx)(n.strong,{children:"Rscript"}),'" file (R language installed in your system).']}),"\n"]}),"\n"]}),"\n",(0,i.jsxs)(n.p,{children:[(0,i.jsx)(n.strong,{children:"Notes"}),': Ensure that install.packages("Runiversal") is already applied in R environment.']}),"\n",(0,i.jsx)(n.h2,{id:"calling-r-from-gaml",children:"Calling R from GAML"}),"\n",(0,i.jsx)(n.h3,{id:"calling-the-built-in-operators",children:"Calling the built-in operators"}),"\n",(0,i.jsx)(n.h4,{id:"example-1",children:"Example 1"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{children:"model 
1CallingR\n\nglobal {\n	list X <- [2, 3, 1];\n	list Y <- [2, 12, 4]; \n\n	list result;\n	\n	init{\n		write corR(X, Y); // -> 0.755928946018454\n		write meanR(X); // -> 2.0\n	}\n}\n"})}),"\n",(0,i.jsxs)(n.h3,{id:"calling-r-codes-from-a-text-file-rtxt-without-the-parameters",children:["Calling R codes from a text file (",(0,i.jsx)(n.strong,{children:".R,"}),".txt) WITHOUT the parameters"]}),"\n",(0,i.jsxs)(n.p,{children:["Using ",(0,i.jsx)(n.strong,{children:"R_compute(String RFile)"})," operator. This operator DOESN\u2019T ALLOW to add any parameters form the GAML code. All inputs is directly added into the R codes.\n",(0,i.jsx)(n.strong,{children:"Remarks"}),": Don\u2019t let any white lines at the end of R codes. ",(0,i.jsx)(n.strong,{children:"R_compute"})," will return the last variable of R file, this parameter can be a basic type or a list.  Please ensure that the called packages must be installed before using."]}),"\n",(0,i.jsx)(n.h4,{id:"example-2",children:"Example 2"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{children:'model CallingR\n\nglobal\n{\n	list result;\n\n	init{\n		result <- R_compute("C:/YourPath/Correlation.R");\n		write result at 0;\n	}\n}\n'})}),"\n",(0,i.jsx)(n.p,{children:"Above syntax is deprecated, use following syntax with R_file instead of R_compute:"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{children:'model CallingR\n\nglobal\n{\n	file result;\n\n	init{\n		result <- R_file("C:/YourPath/Correlation.R");\n		write result.contents;\n	}\n}\n'})}),"\n",(0,i.jsx)(n.h4,{id:"correlationr-file",children:"Correlation.R file"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{children:'x <- c(1, 2, 3)\n\ny <- c(1, 2, 4)\n\nresult <- cor(x, y, method = "pearson")\n'})}),"\n",(0,i.jsx)(n.h3,{id:"output",children:"Output"}),"\n",(0,i.jsx)(n.p,{children:(0,i.jsx)(n.code,{children:"result::[0.981980506061966]"})}),"\n",(0,i.jsx)(n.h4,{id:"example-3",children:"Example 3"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{children:'model CallingR\n\nglobal\n{\n	list result;\n\n	init{\n		result <- R_compute("C:/YourPath/RandomForest.R");\n\n		write result at 0;\n	}\n}\n'})}),"\n",(0,i.jsx)(n.h4,{id:"randomforestr-file",children:"RandomForest.R file"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{children:'\nlibrary(randomForest)\n\n\ndata(iris)\n\nnrow<-length(iris[,1])\n\nncol<-length(iris[1,])\n\nidx<-sample(nrow,replace=FALSE)\n\ntrainrow<-round(2*nrow/3)\n\ntrainset<-iris[idx[1:trainrow],]\n\n\ntrainset<-iris[idx[1:trainrow],]\n\ntestset<-iris[idx[(trainrow+1):nrow],]\n\n\nmodel<-randomForest(x= trainset[,-ncol], y= trainset[,ncol], mtry=3, ntree=50)\n\n\npred<-predict(model, testset[,-ncol], type="class")\n\n\nacc<-sum(pred==testset[, ncol])/(nrow-trainrow)\n'})}),"\n",(0,i.jsx)(n.h4,{id:"output-1",children:"Output"}),"\n",(0,i.jsx)(n.p,{children:(0,i.jsx)(n.code,{children:"acc::[0.98]"})}),"\n",(0,i.jsx)(n.h3,{id:"calling-r-codes-from-a-text-file-r-txt-with-the-parameters",children:"Calling R codes from a text file (.R, .txt) WITH the parameters"}),"\n",(0,i.jsxs)(n.p,{children:["Using ",(0,i.jsx)(n.strong,{children:"R_compute_param(String RFile, List vectorParam)"})," operator. This operator ALLOWS to add the parameters from the GAML code."]}),"\n",(0,i.jsxs)(n.p,{children:[(0,i.jsx)(n.strong,{children:"Remarks"}),": Don\u2019t let any white lines at the end of R codes. ",(0,i.jsx)(n.strong,{children:"R_compute_param"})," will return the last variable of R file, this parameter can be a basic type or a list. Please ensure that the called packages must be installed before using."]}),"\n",(0,i.jsx)(n.h4,{id:"example-4",children:"Example 4"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{children:'model CallingR\n\nglobal\n{\n	list X <- [2, 3, 1];\n	list result;\n\n	init{\n		result <- R_compute_param("C:/YourPath/Mean.R", X);\n		write result at 0;\n	}\n}\n'})}),"\n",(0,i.jsx)(n.h4,{id:"meanr-file",children:"Mean.R file"}),"\n",(0,i.jsx)(n.p,{children:(0,i.jsx)(n.code,{children:"result <- mean(vectorParam)"})}),"\n",(0,i.jsx)(n.h3,{id:"output-2",children:"Output"}),"\n",(0,i.jsx)(n.p,{children:(0,i.jsx)(n.code,{children:"result::[3.33333333333333]"})}),"\n",(0,i.jsx)(n.h4,{id:"example-5",children:"Example 5"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{children:'model 
1CallingR\n\nglobal {\n	list X <- [2, 3, 1];\n	list result;\n	\n        init{\n		result <- R_compute_param("C:/YourPath/AddParam.R", X);\n		write result at 0;\n	}\n}\n'})}),"\n",(0,i.jsx)(n.h4,{id:"addparamr-file",children:"AddParam.R file"}),"\n",(0,i.jsx)(n.p,{children:(0,i.jsx)(n.code,{children:"v1 <- vectorParam[1]"})}),"\n",(0,i.jsx)(n.p,{children:(0,i.jsx)(n.code,{children:"v2<-vectorParam[2]"})}),"\n",(0,i.jsx)(n.p,{children:(0,i.jsx)(n.code,{children:"v3<-vectorParam[3]"})}),"\n",(0,i.jsx)(n.p,{children:(0,i.jsx)(n.code,{children:"result<-v1+v2+v3"})}),"\n",(0,i.jsx)(n.h4,{id:"output-3",children:"Output"}),"\n",(0,i.jsx)(n.p,{children:(0,i.jsx)(n.code,{children:"result::[10]"})})]})}function h(e={}){let{wrapper:n}={...(0,t.R)(),...e.components};return n?(0,i.jsx)(n,{...e,children:(0,i.jsx)(c,{...e})}):c(e)}},28453(e,n,l){l.d(n,{R:()=>a,x:()=>s});var r=l(96540);let i={},t=r.createContext(i);function a(e){let n=r.useContext(t);return r.useMemo(function(){return"function"==typeof e?e(n):{...n,...e}},[n,e])}function s(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(i):e.components||i:a(e.components),r.createElement(t.Provider,{value:n},e.children)}}}]);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.