Starting R in the source directory using Windows explorer - windows

I need to share my data and R source files with a coworker who doesn't have any experience with command line. Moreover, I work in Linux while she's under Windows. But she would like to change some constants and recalculate the scripts. So, it would be cool if she could just double click the R source file and R will be executing in the same directory where the source and data files lie. I thought about setting
Rscript -e "source(\"%1\",chdir=TRUE)"
to the association key in the registry, but the filename (%1) will contain backslashes which R will not handle.
Another way is to setwd() to the source directory in the beginning of my script, but I don't know how to obtain it. AFAIK, argv[0] will be R.exe, not the source.R.
Using GUI is not very convenient, either, because it requires to separately change directory and then to load the script.
Do I have to write a R loader (exe or cmd) for this?

A couple of hints, made as someone who works on Windows by day, and Mac OS by night. I create my projects in a Dropbox folder which is common to both machines. I follow this work practice.
I use RStudio on both my machines. I start up RStudio by right-clicking on the script file locally and this sets the working directory to the file being opened. If I then keep all paths in my script relative, then I can share my projects with myself easily :)
I start my scripts by setting a global variable, in a line that looks silly:
DIR <- getwd()
and then I use relative paths throughout the rest of the scripts. with lines like this:
new.path <- paste(DIR, "rel-path", "to", "new", "file", sep="/")
This avoids my having to tinker with profiles on each machine. It does look obvious, but it gives me one place to change the DIR value in case I want the script to point elsewhere, say, in the morning
DIR <- "~/workspace/newproject"
or, in the evening,
DIR <- "c:/R_workspace/yet/a/different/project"
I also have to be careful that I use the same R version and packages as, in a few cases, that has led to hiccups a few times.
It is a simple flow, but effective and allows seamless working across the two systems

Oops. Explorer starts R in the source directory if it is not on network folder. Initially I tried to start it on network folder. So the only thing to do is to copy the files to the local drive, or to map the network drive to a letter.

Related

Octave- How to automatically execute commands on startup

I'm new to octave, and want to run a few commands on startup automatically every time it opens.
I typed "help startup" and saw "Octave uses the file ".octaverc". I did a bit of searching online at https://www.math.utah.edu/docs/info/octave_4.html, and saw the .octaverc file should be in the following path:
OCTAVE_HOME/lib/octave/VERSION/startup/octaverc
PROBLEM:
In that directory I don't have a startup folder, only "oct" and "site". I do see hidden files, which was my first thought since the file begins with "." character. So I then used Agent Ransack in the directory, and still nothing came up.
QUESTION:
1) Do I have to make the startup folder and octaverc file myself?
2) If so, does one, both or none have to be hidden?
3) Can it be a txt file, or does it have a special extension?
4) Do I just type the commands straight into the file or is there special formatting?
NOTE:
In case I'm going about this the wrong way, there are the operations I'd like to have run on startup:
PS1('>> '), addpath('D:\Users\Me\Desktop'), clc
Thanks ahead of time for the help!!
Possible locations (and their differences) for octaverc files are specified in the documentation.
In short, these are, from more general to specific:
octave-home/share/octave/site/m/startup/octaverc (most generic, for entire system)
octave-home/share/octave/version/m/startup/octaverc (to cover for more than one octave versions installed on the system, possibly requiring different startup scripts)
~/.octaverc (where ~ is unix-speak for a user's home directory -- covering for user-specific startup files)
.octaverc files in any directory, creating specific startup conditions for specific directories
octaverc files are effectively simple script files that are executed from most generic to most specific each time octave starts. Therefore, in the presence of conflicting commands, the more specific file can effectively be used to override the more generic behaviour.
Octave also supports (but does not recommend) the use of the startup.m file, for matlab compatibility.
You might also want to check out pathdef and savepath as well.
As a more general tip, if you ever want to search for a specific keyword from the documentation (e.g. octaverc), you can type this kind of search query in duckduckgo (or google):
octaverc site:https://octave.org/doc/interpreter/
(or just download the documentation as pdf and search the pdf)
Found the solution, the file was in the following path:
OCTAVE_HOME/share/octave/site/m/startup
to find out where OCTAVE_HOME is for you, just type "OCTAVE_HOME" into your Octave command line window.
ANSWERS:
1) You do not have to make a startup octaverc file yourself
2) The file is actually not hidden, so it should be easy to find given you're looking in the right place.
3) The file doesn't have an extension. It's just octaverc.
4) Under the last line of the existing file, you can just append commands as you would type them at the Octave command line window.
the last(7.3.0) octave version placed HERE:/ does not find the THERE:/openEMS/matlab directory even it is already loaded with octaverc or addpath. It keeps looking into the work dir where openEMS is not placed and does not recognize, for instance, the 'physical_constants.m' file.

Per process drive substitution (windows)

I need to have two processes running on Windows (7), where both of them will have different substitued drives, like:
subst P:\ C:\path1
...and the second one
subst P:\ C:\path2
(Why do I need it: Old project files (C++ Builder 5) have the paths to source codes and libraries specified like P:\,L:\,....etc ... and I need them to compile on a buildserver)
How Iam solving it right now:
creating net user temporaryuser
paexec/pstools login as temporary user, runing subst and then running the build
Is there any other (or better) solution?
-> Even making a systemwide hook that would respond with a substitued drive based on a process-id, is, for me, actually a better solution.
(What cannot be done:
-> C++ Builder 5 does not accept %variables% inside project files, so that is not an option.
-> Manually substituing drives in project files is dangerous, because the whole thing goes from git and then gets commited again)
Thank you!

Trying to Set a Shortcut to a Directory

Im trying to run several different scripts on my Mac for data stored in different paths. So I put all said scripts into a folder. In order for a script to work, I have to be cd'd to a specific folder required for said script. I use the scripts to get different types of output (ex: Image volumes, MRI info, etc..).
I was wondering if there was a way to store all my scripts in one directory and assign a shortcut to it. That way, I can cd to my folder, and only type something like $ScriptsFolder/Script_1 to have it. Ive seen this done in the FSL software package, where typing $FSLDIR/data/standard would give you the items in that folder.
UPDATE:
Im using Script=/Users/ray/Documents/Script which works until I close down my terminal to start a new one. Any way to make the setup permanent?
EDITED
Found this helpful in the end.
http://www.techradar.com/how-to/computing/apple/terminal-101-creating-aliases-for-commands-1305638
I used it to create a shortcut to my scripts which are all placed in a folder for easy referencing.

Relocating ".fig" files when creating a GUI using Matlab GUIDE

I've developed a GUI for some build scripts, and am now in the process of deploying it. As the script will be deployed to a number of different machines at various points, I need to use the standard format of directories that the team use.
The GUI consists of a ".fig" file that contains the visual definition of the UI, and a m-script that defines the functionality. I need to locate these two in "fig/" and "m/" folders respectively, but I can't figure out how to. I first searched for an include statement of some kind in the m-script, as when I Run it on its own, the error message in the command window states that the ".fig" file can't be found, but there doesn't seem to be a reference to the ".fig" file anywhere, I assume that it's inferred as both files have the same name but a different extension.
I fear that Matlab's GUI system requires that both ".m" and ".fig" files are in the same location, but this will be an inelegant solution that I'd rather not go for if I can avoid it.
The next thing I'm going to try is to call a script that copies the fig file from the other directory to the same location as the m-script, when it is executed, then deletes that copy once the script exits, which again seems a clunky solution, but will allow me to adhere to the team's organisation conventions.
Does anyone else know of an undocumented means of specifying the relative location of a GUI ".fig" file?
You can export the GUIDE-generated GUI as a single .m file. Check out this blog post: GUIDE GUIs in All One File.
I'm not sure if this is a new feature, or one of those things that has always been there...

file path portability

I have a program that I need to run under *nix and windows. because the program takes file paths from files the issue is what to do about the \ vs / issue.
My current thought is to put in a regex that converts the wrong one to the right one depending on what system I'm on. This will have the effect of letting either type work on either system. Aside from the fact that now I have two problems, does anyone see any other problems?
(Other better solutions are more than welcome)
Edit: the primary issue is getting windows paths to work on unix rather than the other way around.
The / is fully supported in win32 too.
Also see this related question
Windows will generally accept either \ or /,so standardizing on / may make your problem simpler as long as you have complete control over the filenames.
Have you considered creating a "file manager" class that will handle all of the file pathing issues for you? That way in your mail application, when you're loading a data file, you can call something like this.
LoadApplicationData(FileManager.GetDataFilePath)
Then your file manager will detect the environment that it is in and return the proper file path option. That way you can also accomodate for Win32 vs. Unix locatio standards (like Program Files vs /usr or whatever) as well.
Note that Win32 paths are complex when you consider drive letters (no analog on Unix) and the special 'forks' (MacOS pre-X term - likewise no analog on Unix in general, though MacOS X has them - surprise, surprise) that can be provided. Be careful.
Create a parser for your input to create a tree structure of nodes representing directories. Then you can 'save' by walking the tree and writing whatever delimiters you want or optionally doing different things, like checking if the directory exists or writing meta files. This is actually something that I am just now thinking would be useful for my own application :-)
You didn't say what language you are using, so I'm going to selfishly assume c/c++. boost, if you are willing to use it, has a filesystem library. Of course, if you are using a dynamic language, FS abstraction libraries probably already exist there too (e.g. in perl, File::Spec is quite standard).
You haven't told us what sort of files you are reading paths in from. I am going to assume that they are config files. In which case, there are many ways, IMHO the correct answer is to design your program to avoid manipulating paths, if possible. I posted an answer here: https://stackoverflow.com/a/40980510/2345997 which is relevant.
ways:
Add a command line option which allows a user to specify the path in question instead of reading it from a config file.
Add a command line option so that the user can specify a base path. Paths in the config file will be interpreted as located under this base path.
Split your config file into three. One file will have cross platform configuration, another file will have windows only configuration and a final file will have Linux only configuration. Then the user can specify the correct path for both Windows and Linux. On windows your program will read the cross-platform config file and the windows only config file. On Linux it will read the cross-platform file and the Linux only config file.
Add preprocessing to your config file parsing. This will allow you to have one config file where the user can make your program ignore some of the lines in the file depending on which OS the program is running on. Therefore, the user will be able to specify the path to the file twice. Once for Linux, and once for Windows.
Change the design so that the files are always located in the same directory as your executable - then the user only specifies file names in the config file rather than paths to files.
Use a simple function that switches "/" to "\". Then document to the user that they must specify paths as Linux paths and this transformation will be applied for windows.
Create your own path mini-language for this and document it to the user. E.g: "/" - specifies a directory separator, {root} - expands to the root of the filesystem, {cwd} - expands to the current directory, {app} - expands to the path to your application etc... Then the user can specify file paths like: {root}/myfiles/bob.txt on both platforms.
Some paths will work on both platforms. E.g: relative paths like ../my files/bill.txt. Restrict your application to only work with these paths. Document this limitation and how your application handles paths to the user.

Resources