R - Maple Help

CodeGeneration

 R
 translate Maple code to R code

 Calling Sequence R(x, cgopts)

Parameters

 x - expression, list, rtable, procedure, or module cgopts - (optional) one or more CodeGeneration options

Description

 • The R(x, cgopts) calling sequence translates Maple code to R code.
 - If the parameter x is an algebraic expression, then an R statement assigning the expression to a variable is generated.
 - If x is a list, Maple Array, or rtable of algebraic expressions, then a sequence of R statements assigning the elements to an R Array is produced. Only the initialized elements of the rtable or Maple Array are translated.
 - If x is a list of equations $\mathrm{nm}=\mathrm{expr}$, where $\mathrm{nm}$ is a name and $\mathrm{expr}$ is an algebraic expression, then this is understood as a sequence of assignment statements. In this case, the equivalent sequence of R assignment statements is generated.
 - If x is a procedure, then an R class is generated containing a function equivalent to the procedure, along with any necessary import statements.
 - If x is a module, then an R class is generated, as described on the RDetails help page.
 - There is also limited support for the case that x is a command constructor from the Linear Algebra, Statistics or Time Series packages. For supported commands, an equivalent R command is generated. The list of supported commands for Statistics is listed on the RDetails help page.
 • The parameter cgopts may include one or more CodeGeneration options, as described in CodeGenerationOptions.

Examples

For a description of the options used in the following examples, see CodeGenerationOptions.

 > $\mathrm{with}\left(\mathrm{CodeGeneration}\right):$

Translate a simple expression and assign it to the name $w$ in the target code.

 > $R\left(x+yz-2xz,\mathrm{resultname}="w"\right)$
 w <- -2 * x * z + y * z + x

Translate a list and assign it to an Array with the name $w$ in the target code.

 > $R\left(\left[\left[x,2y\right],\left[5,z\right]\right],\mathrm{resultname}="w"\right)$
 w <- c(c(x,2 * y),c(5,z))

Translate a computation sequence, optimizing the input first:

 > $\mathrm{cs}≔\left[s=1.0+x,t=\mathrm{ln}\left(s\right)\mathrm{exp}\left(-x\right),r=\mathrm{exp}\left(-x\right)+xt\right]:$
 > $R\left(\mathrm{cs},\mathrm{optimize}\right)$
 s <- 0.10e1 + x t1 <- log(s) t2 <- exp(-x) t <- t2 * t1 r <- x * t + t2

Declare that $x$ is a float and $y$ is an integer and return the result in a string:

 > $s≔R\left(x+y+1,\mathrm{declare}=\left[x::\mathrm{float},y::'\mathrm{integer}'\right],\mathrm{output}=\mathrm{string}\right)$
 ${s}{≔}{"cg <- x + y + 1"}$ (1)

Translate a procedure. Assume that all untyped variables have type integer.

 > f := proc(x, y, z) return x*y-y*z+x*z; end proc:
 > $R\left(f,\mathrm{defaulttype}=\mathrm{integer}\right)$
 f <- function(x,y,z)   return(y * x - y * z + x * z)

Translate a procedure containing an implicit return. A new variable is created to hold the return value.

 > f := proc(n)   local x, i;   x := 0.0;   for i to n do     x := x + i;   end do; end proc:
 > $R\left(f\right)$
 f <- function(n) {   x <- 0.0e0   for(i in 1 : n)   {     x <- x + i     cgret <- x   }   return(cgret) }

Translate a linear combination of hyperbolic trigonometric functions:

 > $R\left(2\mathrm{cosh}\left(x\right)-7\mathrm{tanh}\left(x\right)\right)$
 cg0 <- 2 * cosh(x) - 7 * tanh(x)

Translate a procedure with no return value containing a printf statement:

 > f := proc(a::integer, p::integer)   printf("The integer remainder of %d divided by %d is: %d", a, p, irem(a, p)); end proc:
 > $R\left(f\right)$
 f <- function(a,p)   print(sprintf("The integer remainder of %d divided by %d is: %d",a,p,a %% p))

Translate a procedure involving routines in linear algebra:

 > detHilbert := proc(M, n :: posint) uses LinearAlgebra;    return Determinant( HilbertMatrix( n ) ); end proc:
 > $R\left(\mathrm{detHilbert}\right)$
 require("Matrix") detHilbert <- function(M,n)   return(det(Hilbert(n)))

Translate some descriptive statistics commands from the Statistics package:

 > $R\left('\mathrm{Statistics}:-\mathrm{Mean}\left(\mathrm{Matrix}\left(\left[\left[2,4,8,21\right]\right]\right)\right)'\right)$
 cg1 <- mean(matrix(c(2,4,8,21),nrow=1,ncol=4))
 > $R\left('\mathrm{Statistics}:-\mathrm{Median}\left(\mathrm{Matrix}\left(\left[\left[2,4,8,21\right]\right]\right)\right)'\right)$
 cg2 <- median(matrix(c(2,4,8,21),nrow=1,ncol=4))
 > $R\left('\mathrm{Statistics}:-\mathrm{Variance}\left(\left[4,8,15,16,23,42\right]\right)'\right)$
 cg3 <- var(c(4,8,15,16,23,42))
 > $R\left('\mathrm{Statistics}:-\mathrm{Scale}\left(\left[4,8,15,16,23,42\right],\mathrm{center}=2,\mathrm{scale}=1\right)'\right)$
 cg4 <- scale(c(4,8,15,16,23,42), center = 2, scale = 1)

Translate a procedure that prints values for the mean and standard deviation:

 > g := proc( x ) return sprintf("Mean = %.0f\n Standard Deviation = %.0f", 'Statistics:-Mean'(x), 'Statistics:-StandardDeviation'(x) ); end proc:
 > $R\left(g\right)$
 g <- function(x)   return(sprintf("Mean = %.0f\n Standard Deviation = %.0f", mean(x), sd(x)))

Due to different choices for default methods for computing some quantities, some commands may give different results in R than in Maple. For example, when computing certain quantiles, Maple uses method 7 by default whereas R uses method 6. For more details on the methods, see the Data Set Options section of the Quantile help page.

 > $R\left('\mathrm{Statistics}:-\mathrm{Quartile}\left(\left[4,8,15,16,23,42\right],3\right)'\right)$
 cg5 <- quantile(c(4,8,15,16,23,42), prob = 3/4)

Translate some other summary statistics:

 > $R\left('\mathrm{Statistics}:-\mathrm{Count}\left(\left[1,3,5,7\right]\right)'\right)$
 cg6 <- length(c(1,3,5,7))
 > $R\left('\mathrm{Statistics}:-\mathrm{CountMissing}\left(\left[1,Float\left(\mathrm{undefined}\right),3,5,7,Float\left(\mathrm{undefined}\right)\right]\right)'\right)$
 cg7 <- sum(is.na(c(1,NaN,3,5,7,NaN)))
 > $R\left('\mathrm{Statistics}:-\mathrm{InterquartileRange}\left(\left[4,8,15,16,23,42\right]\right)'\right)$
 cg8 <- IQR(c(4,8,15,16,23,42), type = 8)
 > $R\left('\mathrm{Statistics}:-\mathrm{FivePointSummary}\left(\left[1,3,5,7\right]\right)'\right)$
 cg9 <- fivenum(c(1,3,5,7))

CodeGeneration[R] can translate various statistical visualizations:

 > $R\left('\mathrm{Statistics}:-\mathrm{Histogram}\left(\left[1,3,5,7,8,4,4\right],\mathrm{color}="Red",\mathrm{title}="Histogram",\mathrm{frequencyscale}=\mathrm{absolute},\mathrm{axes}=\mathrm{none}\right)'\right)$
 cg10 <- hist(c(1,3,5,7,8,4,4), axes = FALSE, col = "Red", freq = TRUE, main = "Histogram")
 > $R\left('\mathrm{Statistics}:-\mathrm{BoxPlot}\left(\left[4,8,15,16,23,42\right],\mathrm{color}="Orange",\mathrm{title}="BoxPlot",\mathrm{notched}=\mathrm{true},\mathrm{orientation}=\mathrm{horizontal}\right)'\right)$
 cg11 <- boxplot(c(4,8,15,16,23,42), col = "Orange", notch = TRUE, horizontal = TRUE, main = "BoxPlot")
 > $R\left('\mathrm{Statistics}:-\mathrm{BarChart}\left(\left[4,8,15,16,23,42\right],\mathrm{color}="Orange",\mathrm{title}="Bar Chart",\mathrm{width}=0.5,\mathrm{distance}=0.2,\mathrm{format}=\mathrm{default}\right)'\right)$
 cg12 <- barplot(c(4,8,15,16,23,42), col = "Orange", space = 0.2e0, beside = TRUE, main = "Bar Chart", width = 0.5e0, horiz = TRUE)
 > $R\left('\mathrm{TimeSeriesAnalysis}:-\mathrm{TimeSeriesPlot}\left(\mathrm{TimeSeriesAnalysis}:-\mathrm{TimeSeries}\left(\left[10,25,30,55\right]\right)\right)'\right)$
 cg13 <- plot.ts(ts(c(10,25,30,55)))

Translate commands to sample values from known distributions. Note that in this case, a warning is returned as R does not support direct references to distribution names in a similar manner to Maple:

 > $R\left('\mathrm{Statistics}:-\mathrm{PDF}\left(\mathrm{LogNormal}\left(0,1\right),1\right)'\right)$
 cg14 <- dlnorm(1, meanlog = 0, sdlog = 1)
 > $R\left('\mathrm{Statistics}:-\mathrm{ProbabilityFunction}\left(\mathrm{Poisson}\left(2\right),1\right)'\right)$
 cg15 <- dpois(1,2)
 > $R\left('\mathrm{Statistics}:-\mathrm{CDF}\left(\mathrm{LogNormal}\left(0,1\right),1\right)'\right)$
 cg16 <- plnorm(1, meanlog = 0, sdlog = 1)
 > $R\left('\mathrm{Statistics}:-\mathrm{Sample}\left(\mathrm{Normal}\left(0,1\right),10\right)'\right)$
 cg17 <- rnorm(10, mean = 0, sd = 1)
 > $R\left('\mathrm{Statistics}:-\mathrm{Quantile}\left(\mathrm{Weibull}\left(3,5\right),0.5\right)'\right)$
 cg18 <- qweibull(0.5e0,5, scale = 3)

Several hypothesis Tests can also be translated:

 > $R\left('\mathrm{Statistics}:-\mathrm{ChiSquareIndependenceTest}\left(\left[4,8,15,16,23,42\right]\right)'\right)$
 cg19 <- chisq.test(c(4,8,15,16,23,42))
 > $R\left('\mathrm{Statistics}:-\mathrm{TwoSampleTTest}\left(\left[1,3,5,7\right],\left[2,4,8,3\right],1,\mathrm{confidence}=0.975,\mathrm{alternative}=\mathrm{twotailed}\right)'\right)$
 cg20 <- t.test(c(1,3,5,7), c(2,4,8,3), mu = 1, conf.level = 0.975e0, alternative = "two.sided")

Compatibility

 • The CodeGeneration[R] command was introduced in Maple 2015.