How to run initialize commands in Octave? - initialization

How do you set up Octave software to run initialization commands when it starts? For example, set the prompt (PS1) and cd to the project directory?
Thanks.

I have Octave installed in C:\Octave, so I did what you ask in file at location C:\Octave\3.2.4_gcc-4.4.0\share\octave\site\m\startup. File is called System-wide startup file for Octave and code I put in there is:
PS1('>> ');
addpath('{$path-to-my-octave-files}');
But any code is OK, I guess.

You could
write a script that does the start up routines you want and call octave afterwards
use
octave --persist --eval 'some_code_to_evaluate'
or
set the exec path with
octave --exec-path path_to_your_subprogramms
Personally, I wouldn't want octave to cd to the project directory, since projects directories can change. Furthermore, other features like the --eval command are not that easy to use anymore if you always have some default code running beforehand.

Related

How do you run Octave in Jupyter?

I want to run Octave in JupyterLab on a M1 Macbook. I have installed JupyterLab using pip and Octave-6.3.0-beta1.dmg file as per the prerequisites.
I have also installed Octave kernel using pip install octave_kernel --user. Now I am somewhat stuck. This documentation says, "We require the Octave executable to run the kernel. Add that executable's directory to the PATH environment variable or use the OCTAVE_EXECUTABLE to point to the executable itself." Can someone help me with this? What am I supposed to type in the terminal?
PATH is an "environment variable". It basically makes anything that runs in your computer know few software and their location. That way, when you do e.g. python my_code.py it knows what python is.
Read the link above too to learn how to add stuff to PATH, in your case, the location of Octave. The location should be something like C:\Octave\Octave-5.1.0.0\mingw64\bin, where the folder contains octave.bat, for a windows machine. Just find wherever Octave is in you MAC.
There is another similar question about this: How to add Octave to PATH environment variable in Mac Sierra?

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.

Why am i able to run unix commands on my PC?

How am i able to execute UNIX commands on my PC Command prompt? Note i do not have cygwin installed, although i was going to before i discovered this.
This is a development machine so i have a lot installed on it like ruby, python, git, github, node and so on.
What does this mean? can i use this without cygwin?
Here is a list of programs installed on my PC program list
How am I able to execute UNIX commands on my PC Command prompt?
You can use the where command in a cmd shell to find out the exact location of your Unix commands, for example:
where ls
This assumes, of course, that ls is located somewhere in your current PATH.
The location returned will show you in which directory your Unix commands are installed and may be enough for you to determine how they were installed.
The where command is roughly equivalent to the Unix which command.
By default, the search is done in the current directory and in the
PATH.
Syntax
WHERE [/r Dir] [/q] [/f] [/t] Pattern ...
WHERE [/q] [/f] [/t] [$ENV:Pattern
Source where
Further Reading
An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
where - Locate and display files in a directory tree.
Running Unix commands in windows can be done by having a tool like Cygwin which has those commands.
You can also get many of those commands compiled for windows and then run them using the command with the full path or only the command if the executable is in a path known by adding the paths to the executable files in Windows by :
1) Running in the terminal: PATH %PATH%;C:\<new_path>
2) Creating command aliases like: doskey np=C:\<new_path>\new_command.exe $*. $* is used to be able to transmit parameters

any way to schedule an R program to run daily?

I am using R program to collect and update data from some local and online sources, which are updated frequently.
Since these sources are fixed, there is no argument to pass to the program, and everything is routine.
Now my supervisor wants me to set this as a scheduled daily task. I know it is impossible for .r file. Is there any way to compile the r file to executable file? such as .exe, .bat, ... ...
I don't need the executable file to be standalone, I can keep R in my computer.
any suggestion is appreciated.
You need to use the standard OS facilities (cron/at on Unix) to run R with the appropriate argument.
E.g., if you add the functions you need to .Rprofile, you can do
R --no-save --no-restore -q -e 'MyFunc(my,args)'
Alternatively, you might want to use Batch Execution of R.
For Windows I have hundreds of scripts that are set up with bat files similar to the below. It assumes that you have a NameOfScript.bat and a NameOfScript.r in the same folder and then run the .bat file from Scheduler and it logs everything from stdout/err to NameOfScript_yyyy-mm-dd.log in the same folder. I normally have the log folder seperate but adding that can be done just by changing the definition of LOG_FILE. Also passes in the folder it's in to R just in case you need to output some files in the folder.
IF DEFINED ProgramFiles(x86) (
SET R_SCRIPT="%ProgramFiles(x86)%\\R\\R-2.15.2\\bin\\Rscript.exe"
) ELSE (
SET R_SCRIPT="%ProgramFiles%\\R\\R-2.15.2\\bin\\Rscript.exe"
)
IF NOT EXIST %R_SCRIPT% GOTO FAIL
SET SCRIPT_DIR=%~dp0
SET SCRIPT_DIR=%SCRIPT_DIR:\=\\%
SET BATCH_FILE=%0
SET BATCH_FILE=%BATCH_FILE:"=%
SET SCRIPT_TO_RUN="%BATCH_FILE:.bat=.r%"
SET day=%DATE:~0,2%
SET month=%DATE:~3,2%
SET year=%DATE:~6,4%
SET yyyymmdd=%year%-%month%-%day%
SET LOG_FILE="%BATCH_FILE:.bat=%"_%yyyymmdd%.log
SET SCRIPT_DIR="%SCRIPT_DIR%"
%R_SCRIPT% --internet2 --max-mem-size=2047M --no-restore --no-save --args %SCRIPT_DIR% < %SCRIPT_TO_RUN% >> %LOG_FILE% 2>&1
PAUSE
EXIT /B 0
:FAIL
ECHO RScript not found. Failed process
You could also call the R script from C#, and run the C# project as a scheduled task.

run a "source" bash shell script in qmake

I want to use the intel compiler for Qt, but using the intel compiler implies running the script
$ source /opt/intel/bin/compilervars.sh intel64
Of course, I could add this to ~/.bashrc, but this would not run it in QtCreator, where it still complains about missing icpc. So I want it to be a part of the main mkspec qmake file.
How can I execute that full bash command in qmake?
Short Answer: Using QMAKE_EXTRA_TARGETS and PRE_TARGET_DEPS, you can execute source /opt/intel/bin/compilersvars.sh intel64, but simply sourcing them will not solve your issue.
Long Answer: The QMake file is converted into a Makefile. Make then executes the Makefile. The problem you will run into is that Make executes each command in its own shell. Thus, simply sourcing the script will only affect one command, the command that executes the script.
There are a couple of possible ways to make things work:
Execute the script before starting Qt-Creator. I've actually done this for some projects where I needed to have special environment variables setup. To make my life easier, I created a shell command to setup the environment and then launch Qt-Creator.
Within Qt-Creator, modify the Build Environment for the project I've also used this trick. In your case, simply look at the environment setup by the script and change the "Build Environment" settings under the project tab for your project to match those setup by the script.
It might also be possible to modify QMake's compiler commands, but I am not sure you can make it execute two commands instead of one (source the script then execute the compiler). Further more, this will make the project very un-transportable to other systems.
You can create a shell script that does more or less the following:
#! /usr/bin/env sh
# Remove the script's path from the PATH variable to avoid recursive calls
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
export PATH=${PATH/$SCRIPT_DIR:/}
# Set the environment up
source /opt/intel/bin/compilervars.sh intel64
# Call make with the given arguments
make "$#"
Save it into a file named "make" in an empty directory somewhere, make it executable, then change the build environment in QT Creator to prepend PATH with the script's directory:
PATH=[dir-of-make-script]:${Env:PATH}
You can do this either in the project settings or once and for all in the kit settings.
Like this, QT Creator will be fooled into calling your make script which sets up your environment before calling the actual make binary. I use a similar technique under Windows with Linux cross-toolchains and it has been working well so far.

Resources