Interface between Octave and R - r

Could anyone kindly update the status on the interface between Octave and R? ROctave package was developed in 2002, but no new updates after that. I like to call some functions from Octave in R such as "roots". How to do it?
Thanks for your help.

Expanding on chl's point of using R directly, you can also consider these CRAN packages which explicitly bring Octave functionality to R:
pracma
signal
And then there is the old but trusted R / Octave cheat sheet.
Edit in 2012 There is now also an emerging CRAN package RcppOctave which permits R to execute Octave code. The package is at a reasonably early stage, and works so far only on Unix.

I do not know of any active R/octave project, but if you're just after finding roots for a given polynomial you can use one of the polynom or PolynomF package:
Here is an example with P(x)= 6 + 5*x + 4*x^2 + 3*x^3 + 2*x^4 + x^5.
In octave,
octave[2] > p = 1:6;
octave[3] > roots(p)
ans =
0.55169 + 1.25335i
0.55169 - 1.25335i
-1.49180 + 0.00000i
-0.80579 + 1.22290i
-0.80579 - 1.22290i
In R,
> library(polynom)
> p <- polynomial(6:1)
> pz <- solve(p)
> pz
[1] -1.491798+0.000000i -0.805786-1.222905i -0.805786+1.222905i
[4] 0.551685-1.253349i 0.551685+1.253349i

I found this CRAN package called RcppOctave:
"Direct interface to Octave. The primary goal is to facilitate the port of Matlab/Octave scripts to R. The package enables to call any Octave functions from R and as well as browsing their documentation, passing variables between R and Octave, using R core RNGs in Octave, which ensure stochastic computations are also reproducible."
http://cran.r-project.org/web/packages/RcppOctave/index.html

Related

Could not find the "DouglasPeuckerEpsilon" in R language

I'm trying to use Douglas Peucker's algorithm in R.
By executing the following code, I get the following error:
Px <- (1:100)/10
Py <- dnorm(Px,3,1)+dnorm(Px,7,1)+Px/10
### Example 1
### Simplification using epsilon
par(mfrow=c(2,2))
plot(Px,Py,type="l")
plot(DouglasPeuckerEpsilon(Px,Py,0.01),type="b",col=4)
Could not find the "DouglasPeuckerEpsilon" in R language.
This is because I do not have the DP package installed?
Perform the following Steps:
Install the package (kmlShape)
Then load the Library (kmlShape)
install.packages("kmlShape")
library("kmlShape")
Then run plot(DouglasPeuckerEpsilon(Px,Py,0.01),type="b",col=4) for Ramer-Douglas-Peucker algorithm (RDP)

How to list all demos and their sources of an R package

I was wondering whether there is a special command to list all demo programs (R-scripts) of a package and to inspect their content without running them, i.e. without using
demo(name_of_demo_file)
In particular, I am looking for an approach to achieve this without downloading the sources and browsing the demo directory.
Running demo(package = "stats") displays a list of the demos. While the function doesn't let you access the code without running it, you could extract that from the package source. You wouldn't need to re-download the source since you already have it installed, and can find it within R with system.file.
For instance, you could write a short function:
print_demo_code <- function(demo, package) {
demo_file <- system.file("demo", paste0(demo, ".R"), package = package)
cat(readLines(demo_file), sep = "\n")
}
For example:
print_demo_code("nlm", package = "stats")
displays:
# Copyright (C) 1997-2009 The R Core Team
### Helical Valley Function
### Page 362 Dennis + Schnabel
require(stats); require(graphics)
theta <- function(x1,x2) (atan(x2/x1) + (if(x1 <= 0) pi else 0))/ (2*pi)
## but this is easier :
theta <- function(x1,x2) atan2(x2, x1)/(2*pi)
...

Points3D Function in R

I have the scatterplot3d package installed in R. When I load it with library(scatterplot3d) or require(scatterplot3d) I am able to create a 3d scatter plot. However, when I try to use the points3d function I get the following error:
Error: could not find function "points3d"
I tried reinstalling the package to no avail (both inside R and as a tarball using R CMD INSTALL in the command line). I am running Xubuntu 12.10 kernel 3.8.7-030807-generic and R version 2.15.3 (2013-03-01).
Entering locate points3d in the command line gave me no results.
I also tried the par.mar default settings command as explained in the manual.
scatterplot3d does an interesting object-oriented twist on the usual R practice. The object returned from the function call includes the points3d function as built-in part of the object but it is not in the Global environment. It is intended that you add to the existing plot-object using that "attached" function that is not a free-living organism but rather a domesticated animal that only exists in the object corral, so you would use this as your syntax:
object$point3d(x,y,z)
I do "feel your pain" but can show you how to overcome the frustration, since I created a working example yesterday: Using scatterplot3d to plot a sphere
You need to intall the package plot3D in the usual way via
install.packages("plot3D")
Then you just need to import, generate the dataset and use the function points3D()
library(plot3D)
x = rnorm(100)
y = rnorm(100)
z = x + y + rnorm(100,0,1)
points3D(x, y, z, col = rainbow(1000))
This is the plot generated by the code above

How to correct for "Error in nullmodel(comm, method) : could not find function "list2env" in the vegan package

I'm busy exploring the package vegan for R, using it to calculate nestedness of community matrices and null models. I'm particularly interested in using the permat functions as well as Oecosimu.
However, when running my program I obtained the following errors:
Error in nullmodel(comm, method) : could not find function "list2env"
Error in nullmodel(m, ALGO) : could not find function "list2env"
I then even ran an example (given below) of how to use these functions given by the R help function, and even these examples gave the same error. Am I suppose to import something else in order to use these functions or how do I go about fixing this?
Examples:
m <- matrix(c(
1,3,2,0,3,1,
0,2,1,0,2,1,
0,0,1,2,0,3,
0,0,0,1,4,3
), 4, 6, byrow=TRUE)
x1 <- permatswap(m, "quasiswap")
summary(x1)
x2 <- permatfull(m)
summary(x2)
x3 <- permatfull(m, "none", mtype="prab")
x3$orig
summary(x3)
x4 <- permatfull(m, strata=c(1,1,2,2))
summary(x4)
Technically, this is a bug in the development version of Vegan on R-Forge. We were failing to declare a dependency on R versions >= 2.12 in DESCRIPTION. I have checked in the relevant change to the source tree to fix this but it will take a day or so before the tarball and binaries are rebuilt by R-Forge.
That said, you should probably update your R to something more recent. Or use the versions of those functions provided in Vegan 2.0-x on CRAN.
list2env is part of R base, which means it comes with the distribution, not in an add-on package. So if you don't have it you're probably either running an old version of R or have a broken installation. The example worked fine for me, with R 2.12.1 and vegan 2.1-0.
Your code works for me without an error message
The most probable cause of your error is your using old versions of R, vegan or permute
The R news for changes says
CHANGES IN R VERSION 2.12.0: NEW FEATURES:
o New list2env() utility function as an inverse of
as.list(<environment>) and for fast multi-assign() to existing
environment. as.environment() is now generic and uses list2env()
as list method.
CHANGES IN R VERSION 2.12.1: BUG FIXES:
o When list2env() created an environment it was missing a PROTECT
call and so was vulnerable to garbage collection.
CHANGES IN R VERSION 2.13.0: NEW FEATURES:
o list2env(envir = NULL) defaults to hashing (with a suitably sized
environment) for lists of more than 100 elements.
So update your version of R and the packages and try again.

How to display the code of a .C routine used by R function?

I was studying some functions of the package rimage. If you want to see, for example, the code for the sobel.h function, you obtain:
> library(rimage)
> sobel.h
function (img)
{
w <- dim(img)[2]
h <- dim(img)[1]
imagematrix(abs(matrix(.C("sobel_h", as.double(img), as.integer(w),
as.integer(h), eimg = double(w * h), PACKAGE = "rimage")$eimg,
nrow = h, ncol = w)), noclipping = TRUE)
}
So the sobel.h function uses the C routine called sobel_h, (which is (I think) stored in the file rimage.dll).
Is there any way to see the C code of the sobel_h function?
(I speak about the package rimage for a practical example; but the answer would of course generalised to all packages that uses .C routines).
Check this:
Uwe Ligges. R Help Desk: Accessing the sources. R News, 6(4):43-45, October 2006.
In order to access the sources of
compiled code (i.e., C, C++, or
Fortran), it is not sufficient to have
the binary version of R or a
contributed package installed. Rather,
it is necessary to download the
sources for R or for the package.
The Linux source for rimage is here:
http://cran.r-project.org/src/contrib/rimage_0.5-8.1.tar.gz
Windows source is here:
http://cran.r-project.org/bin/windows/contrib/r-release/rimage_0.5-8.1.zip
sobel.c is in the rimage/src/ directory in the unpacked files.
you will have to find the source code to rimage.dll (try googling on sobel_h and rimage.dll, i found something promising)

Resources