how to call an executable from R script - r

I have a CUDA code which I have compiled and have the executable of it. Now I want to call this executable from a R script and pass it arguments also from the R script itself? Is it possible? If yes, please explain how?

To call any external executables you can use the system function:
system("cuda_exe arg1 arg2")
where cuda_exe is the cuda executable, and arg* are the command line arguments passed to the script.

A more cross-platform alternative than system is system2. It'll work on Windows and other systems without a /bin/sh.
system2("cuda_exe", c("arg1", "arg2"))
It requires no shell, but shell syntax like the * glob won't work, and you'll have to learn the R way of doing things, such as list.files(pattern = ".*.csv") instead of just "*.csv". The upshot is that you won't have to fiddle with paste() to construct the command line.

Related

How to run a julia script through terminal

I have a script main.jl which prints a simple "Hello world" string:
println("Hello world!")
However, when trying to run the script through the terminal like this:
julia> main.jl
I get the error:
ERROR: type #main has no field jl
All the information I can find online suggests calling the script like I do to run it. I have assured that I'm in the correct directory - what am I doing wrong?
You are trying to run the file from the Julia REPL (as indicated by the julia> prompt at the beginning of the line). There, you have to include the file as #AndreWildberg mentions. This will run the commands from the file as if you had typed them into the REPL.
The information you found online might have been about running Julia from "normal" terminal, aka a console shell like bash on Linux. There, running julia main.jl will run the program, although the REPL method above is usually preferred for working with Julia.
(question about calling the script with arguments asked in the comment):
First of all, I'll mention that this is not the usual workflow with Julia scripts. I've been writing Julia code for years, and I had to look up how to handle command-line arguments because I've never once used them in Julia: usually what's done instead is that you define the functions you want in the file, with maybe a main function, and after doing an include, you call the main function (or whichever function you want to try out) with arguments.
Now, if your script already uses command-line arguments (and you don't want to change that), what you can do is assign to the variable that holds them, ARGS, before the include statement:
julia> push!(empty!(ARGS), "arg1")
1-element Vector{String}:
"arg1"
julia> include("main.jl")
Here we empty the ARGS to make sure any previous values are gone, then push the argument (or arguments) we want into it. You can try this out for educational purposes, but if you are new to the language, I would suggest learning and getting used to the more Julian workflow involving function calls that I've mentioned above.
The julia> prompt means your terminal is in Julia REPL mode and is expecting valid Julia code as input. The Julia code main.jl would mean that you want to return the value of a field named jl inside a variable named main. That is why you get that error. The Julia code you would use to run the contents of a file is include("main.jl"). Here the function include is passed the name of your file as a String. This is how you should run files from the REPL.
The instructions you read online are assuming your terminal is in shell mode. This is usually indicated by a $ prompt. Here your terminal is expecting code in whatever language your shell is using e.g. Bash, PowerShell, Zsh. When Julia is installed, it will (usually) add a julia command which works in any shell. The julia command by itself will transform your terminal from shell mode to REPL mode. This julia command can also take additional arguments like filenames. If you use the command julia main.jl in this environment, it will run the file using the Julia program and then exit you back to your terminal shell. This is how you should run files from the terminal shell.
A third way to run Julia files would be to install an IDE like VSCode. Then you can run code from a file with keyboard shortcuts rather than by typing commands.
See the Getting Started Documentation for more information.
Adding to Sundar R's answer, if you want to run script which takes commandline arguments from REPL, you can check this package: https://github.com/v-i-s-h/Runner.jl
It allows you to run you script from REPL with args like:
julia> #runit "main.jl arg1 arg2"
See the README.md for detailed examples.

How to avoid parsing command line arguments when building R package?

I'm developing an R package that includes a command line interface. When building the package, I would like to avoid parsing the command line arguments (build flags) since my command line argument parser doesn't recognize these build-related arguments and produces an error.
To overcome this issue, I'm currently using an approach where my build flags are hard coded to an if statement before trying to parse the arguments:
if (length(commandArgs(trailingOnly = TRUE)) > 0 &&
!(grepl("--no-multiarch", commandArgs(trailingOnly = TRUE)))) {
argv <- GetCmdlineArguments()
DoStuff(argv$parsed.argument)
}
Another approach which I haven't experimented with yet includes putting the argument parsing in a different R file which is ignored by the build via .Rbuildignore. This would, however, lead to an unfavorable situation where an extra file is needed for each R file that has a command line interface.
Is there more elegant and robust way of detecting if the package is being built instead of actually executed from the command line by the user?
I do not fully understand what you are trying to achieve, but allow me to provide some context:
first off, all R use is generally from the R prompt, not the command-line
as such, a package will only every contain R functions etc
that said, command-line work is very powerful and popular
the R ecosystem has both Rscript and r from littler both of which facilitate command-line use
typically, we ship scripts for use these frontends in inst/scripts/ or inst/examples
all those scripts can use one of the many packages to parse command-line options -- I favour docopt
consequently, my littler package has lots of examples using docopt
Could you not just do the same?

How does R system() recognize command path?

How does R's built-in system() function know where to look to invoke some arbitrary OS command specified by the command argument? For example, if I homebrew install some_command_line_program, how does R's system() function know where it is located when I type:
cmd <- "some_complicated_code_from_some_command_line_program"
system(cmd, wait=FALSE)
In other words, how is R smart enough to know where to look without any user input? If I compile from source via Github (instead of homebrew install), would system() also recognize the command?
What system does depends on your OS, you've not told us (although you've given us some clues...).
On unix-alike systems, it executes it as a command in a bash shell, which searches for the first match in the directories on the $PATH environment variable. You can see what that is in R:
> Sys.getenv("PATH")
[1] "/usr/local/heroku/bin:/usr/local/heroku/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/nobackup/rowlings/NEST4B"
In Windows, it does something else.
You can get a full path to whatever it might run with Sys.which, which uses the systems' which command on unixes and fakes it on Windows. Read the help for more.
If you compile something from source then it will be found if the file that runs the command (a shell script, an executable, a #!-script in any language) is placed in a folder in your $PATH. You can create a folder yourself, say /home/user/bin, put your executables in there, add that to your $PATH, and (possibly after logging out an in again, or restarting R, or just firing up a new shell...) then R will find it.

calling Qiime with system call from R

Hej,
When I try to call QIIME with a system call from R, i.e
system2("macqiime")
R stops responding. It's no problem with other command line programs though.
can certain programs not be called from R via system2() ?
MacQIIME version:
MacQIIME 1.8.0-20140103
Sourcing MacQIIME environment variables...
This is the same as a normal terminal shell, except your default
python is DIFFERENT (/macqiime/bin/python) and there are other new
QIIME-related things in your PATH.
(note that I am primarily interested to call QIIME from R Markdown with engine = "sh" which fails, too. But I strongly suspect the problems are related)
In my experience, when you call Qiime from unix command line, it usually creates a virtual shell of it`s own to run its commands which is different from regular system commands like ls or mv. I suspect you may not be able to run Qiime from within R unless you emulate that same shell or configuration Qiime requires. I tried to run it from a python script and was not successful.

Can I invoke linux shell commands in a R session?

I am using RStudio and at times I want to remove some files in the working directory (e.g., previously generated .csv files).
It is quite inconvenient to frequently switch to bash and execute rm. Is there any way of invoking commands in the R console?
See here
Use system (or shell) as agstudy's comment says
e.g. system("pwd")
If you are just removing files, rather than executing arbitrary commands on the shell, you would be better off following Thomas's suggestion:
?file.remove
Using this function instead of shell("rm example.csv") is safer and more portable.

Resources