[R-SIG-Mac] R GUI: expansion of paths in Console when dragging from Finder
Hans-Jörg Bibiko
bibiko at eva.mpg.de
Wed Feb 22 07:06:50 CET 2012
On 22 Feb 2012, at 00:28, jochen laubrock wrote:
> in R.app (GUI), dragging a folder into the Console used to expand the full path to the folder as far as I remember. Now the behavior seems to have changed. In many cases, the full path is still expanded (somewhat; the tilde abbreviation "~" is used for a users home directory). However, if the selection in the Finder is a file or directory below R's current working directory, only a relative path is inserted into the command window. This can be puzzling.
>
> What is the motivation for this change? In my opinion, the old behavior was more consistent, and also more useful because it could be used to obtain the full path to a file. I would also prefer if the behavior were consistent between the GUI and command line R (in which dragging always leads to full path expansion).
Hi Jochen,
sorry, it was my fault. I had the wrong default in mind while implementing a feature.
I've just fixed it. Now, dragging file(s) with the extension other than *.R or *.Rdata will insert the full path. If you hold down the ALT key ⌥ while dragging the relative path(s) to the current set working directory and R script file path resp. will be inserted.
Dragging *.R files will insert a "source(...)" and dragging *.Rdata files will insert a "load(...)" snippet template. This can be controlled by holding down the ALT key ⌥ or CMD key ⌘ or both.
The "source(...)" template is using the new introduced so-called snippet system (a kind of programmable macro or template system [if you know the editor "TextMate" then you will know it ;) ]).
It will insert e.g. "source('~/foo.R', chdir=TRUE)" whereby ", chdir=TRUE" is highlight to firstly let the user decide whether it should be used or not. If not simply press DEL key ⌫ and press TAB key ⇥ to go to the end of the inserted template. If the user want to change the value of the parameter 'chdir' simply press the TAB key ⇤ to highlight the next snippet 'TRUE' and press e.g. 'F'. Pressing the keys SHIFT+TAB ⇧⇥ will highlight ", chdir=TRUE" again. Thus one can navigate through the defined snippets by using the keys TAB ⇥ (next; unless invoked from the last defined snippet) and SHIFT+TAB ⇧⇥ (prev). Pressing TAB ⇥ from the last defined snippet the cursor will be placed after the entire inserted text template.
In addition the user can define its own dragging template which should be inserted according to a file extension (incl. *.R, *.Rdata to overwrite the default template).
Here a short example:
If you save a file to:
~/Library/Application Support/R/DragActions/tab/command.sh
with following content:
# get the file name
VARNAME=${R_DRAGGED_RELATIVE_FILE_PATH##*/}
# remove extension
VARNAME=${VARNAME%%.*}
# print template
echo -en "\${$R_CURRENT_SNIPPET_INDEX:$VARNAME} <- read.table(file = \"$R_DRAGGED_RELATIVE_FILE_PATH\", sep = \"\${$(($R_CURRENT_SNIPPET_INDEX+1)):\\\t}\", header = \${$(($R_CURRENT_SNIPPET_INDEX+2)):TRUE}, stringsAsFactors = \${$(($R_CURRENT_SNIPPET_INDEX+3)):FALSE})"
then if you drag e.g. the files ~/data/d1.tab and ~/data/d2.tab the following will be inserted:
d1 <- read.table(file = "~/data/d1.tab", sep = "\t", header = TRUE, stringsAsFactors = FALSE)
d2 <- read.table(file = "~/data/d2.tab", sep = "\t", header = TRUE, stringsAsFactors = FALSE)
whereby d1/d2 and the values for the parameters sep, header, stringAsFactor are defined as snippets.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: RDragTemplateExample.png
Type: image/png
Size: 30391 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-mac/attachments/20120222/9fe589b9/attachment-0001.png>
-------------- next part --------------
The content of the file "command.sh" will be interpreted as a BASH script or if you write the "magic header" like #!/usr/bin/perl as Perl script etc., i.e. one can use any scripting language here. Furthermore R.app will pass shell variables to that script like R_DRAGGED_RELATIVE_FILE_PATH and others on run-time. The output of that script will be then inserted to the dragged destination. This system makes editing in R.app very powerful.
Documentation and support for other user-defined "normal" text templates like to insert the "foo <- function(x) {}" if you type "fun⇥" will follow soon.
Many thanks for the hint!
Kind regards,
--Hans
More information about the R-SIG-Mac
mailing list