Calling R function within Matlab and loading Matlab variables into the R function - r

I am calling R script from within Matlab. The R script is a function that should load the data generated from Matlab and then passes it through the R function, and finally computes a result and sends it back to Matlab. I have included a very simplified code below. The Matlab and R file are in the same path. The R_script.R is the following:
require("mclust")
group = function(data, num_cls){
Mclustmodel = Mclust(data, num_cls)
return(Mclustmodel$class)
}
In Matlab, the code is:
system( 'Rscript ./R_script.R' )
X = rand(10);
K = 3;
class = group(X, K);
My question is: Can I load X and K into the R function group, and directly calculate the answer?
I am using a Linux system.
Thanks.

You could try RMatlab. See this blog post for instructions. RMatlab is biderctional so you can run it from Matlab, send commands to R and get results as Matlab variables. It works on unix systems.

Related

R callr different R version

Is it possible to run callr::r with a different R version than the one currently used? An example would be to call the following from R3.5.1, but execute it with R3.4.1.
callr::r(
func = R.version.string,
r_binary = '..../R3.4.1/bin' # inexistent argument
)
It should return something like "3.4.1". I have looked into callr:::setup_r_binary_and_args, but it takes the current R version.

Calling a python function from R with passing the arguments

Is there any package for calling a python function from R by passing the function arguments through R? Now i have directly called the python file using system in R.
a<-system('/home/anaconda3/bin/python /home/Desktop/myfile.py' ,intern = TRUE)
But this myfile.py file is having a function with paramenter. How to specify the parameter in R?
I have tried system('/home/anaconda3/bin/python /home/Desktop/myfile.py argument',wait=FALSE,intern = TRUE) .But it returns 0.
for example I want to pass the number of core that my python script can use:
system(paste('/home/anaconda3/bin/python','home/Desktop/myfile.py',NCORE))
Then in Python Script before launch the function I can read my parameter in this way:
n_core = int(sys.argv[1])
sys.argv is a list in Python, which contains the command-line arguments passed to the script.
please look at reticulate
library(reticulate)
os <- import("os")
os$listdir(".")

Walsh-Hadamard Transform in r

I search for a command to compute Walsh-Hadamard Transform of an image in R, but I don't find anything. In MATLAB fwht use for this. this command implement Walsh-Hadamard Tranform to each row of matrix. Can anyone introduce a similar way to compute Walsh-Hadamard on rows or columns of Matrix in R?
I find a package here:
http://www2.uaem.mx/r-mirror/web/packages/boolfun/boolfun.pdf
But why this package is not available when I want to install it?
Packages that are not maintained get put in the Archive. They get put there when that aren't updated to match changing requirements or start making errors with changing R code base. https://cran.r-project.org/web/packages/boolfun/index.html
It's possible that you might be able to extract useful code from the archive version, despite the relatively ancient version of R that package was written under.
The R code for walshTransform calls an object code routine:
walshTransform <- function ( truthTable ) # /!\ should check truthTable values are in {0,1}
{
len <- log(length(truthTable),base=2)
if( len != round(len) )
stop("bad truth table length")
res <- .Call( "walshTransform",
as.integer(truthTable),
as.integer(len))
res
}
Installing the package succeeded on my Mac, but would require the appropriate toolchain on whatever OS you are working in.

collect output of system rscript command as r object

I am using RStudio in Windows to develop and run a pipeline for multivariate analysis that involve big dataset (90 by ~ 60000 matrices). With matrices of such a size, I got "protection from stack overflow" pretty often. One way of avoid this problem, while still using RStudio as opposite to the regular Rgui, is to run my script(s) using the following syntax
system("Rscript --max-ppsize=500000, my_script.s").
However, running this command results in the script running succesfully, but I cannot get the desired output. If I run the previous command with the following option
opt-< system("Rscript --max-ppsize=500000, my_script.s", internal = TRUE)
I got the standard output to the terminal as output (as a character vector), but not the desired output.
Consider this toy examples:
save the following code in my_script.R
print("first call")
rnorm(15)
print("second call")
rnorm(20)
and run the following code from the console
a <- system("Rscript my_script.R", intern = TRUE)
a
As you can see, the output is a character vector of length 9 with the standard output to the console.
If you modify my_script.R as follow
print("first call")
i_want_this <- rnorm(20)
and then run it again
a <- system("Rscript my_script.R", intern = TRUE)
a
now the only thing stored in a is the output of the print command.
My question is: is there a way to collect the i_want_this variable as an r object (in this case a numeric vector of length 20) ?
A similar question has been asked here, without a satisfying answer.

Run Julia function every time Julia environment launches

I am moving from R, and I use the head() function a lot. I couldn't find a similar method in Julia, so I wrote one for Julia Arrays. There are couple other R functions that I'm porting to Julia as well.
I need these methods to be available for use in every Julia instance that launches, whether through IJulia or through command line. Is there a "startup script" of sorts for Julia? How can I achieve this?
PS: In case someone else is interested, this is what I wrote. A lot needs to be done for general-purpose use, but it does what I need it to for now.
function head(obj::Array; nrows=5, ncols=size(obj)[2])
if (size(obj)[1] < nrows)
println("WARNING: nrows is greater than actual number of rows in the obj Array.")
nrows = size(obj)[1]
end
obj[[1:nrows], [1:ncols]]
end
You can make a ~/.juliarc.jl file, see the Getting Started section of the manual.
As for you head function, here is how I'd do it:
function head(obj::Array; nrows=5, ncols=size(obj,2))
if size(obj,1) < nrows
warn("nrows is greater than actual number of rows in the obj Array.")
nrows = size(obj,1)
end
obj[1:nrows, 1:ncols]
end

Resources