This is a Mac specific question. I would like to source a script that I'm editing on Textmate2, e.g. mycode.R, and make it run in iTerm (terminal would do as well). I do not need to start R, I have the top window of iTerm already running it.
So in the iTerm tab should appear the line:
> source("[path]/mycode.R", chdir = TRUE)
What I needs equivalent to what you have on Rstudio with the key combination Cmd + Shift+S. I found this answer How can I send selected text (or a line) in TextMate to R running on Terminal, but this is about sending a line, or echo the entire code, while what I need should be easier. I succeeded in sourcing to R.app using the following code
#!/bin/bash
osascript -e 'tell application "R.app" to activate'
osascript -e "tell application \"R.app\" to cmd \"source(file='"$TM_FILEPATH"',print.eval=TRUE, chdir=TRUE)\"" \
osascript -e 'tell application "TextMate" to activate'
But if I replace "R.app" by "iTerm", "iTerm2" or "Terminal", the script fails.
This does what I was looking for. I'm really not proficient with scripting, this is just result of (a lot of) trial and error. I'm sure there exists a more correct
or elegant solution. Here is the code.
#!/usr/bin/env bash
[[ -f "${TM_SUPPORT_PATH}/lib/bash_init.sh" ]] && . "${TM_SUPPORT_PATH}/lib/bash_init.sh"
curDir=''
if [[ ${#TM_DIRECTORY} -gt 0 ]]; then
curDir="$TM_DIRECTORY"
fi
osascript \
-e 'on run(theCode)' \
-e 'tell application "iTerm2"' \
-e 'tell current window' \
-e 'tell current tab' \
-e 'tell current session' \
-e 'write text (item 1 of theCode)' \
-e 'end tell' \
-e 'end tell' \
-e 'end tell' \
-e 'end tell' \
-e 'end run' -- "source(\"$TM_FILEPATH\",print.eval=TRUE, chdir=TRUE)"
Related
I have 2 R scripts called script1.R and script2.R respectively and I want to run them sequentially as pipeline. To do so, I am trying a bash script to run 2 R scripts, use inputs and arguments (for every script) and returns output (for every script). since there are 2 R scripts, we have 2 steps as follows:
1- this the command for the 1st R script:
Rscript script1.R /path/to/input /path/to/OutputDir argument1 argument2
2- this the command for the 2nd R script:
Rscript script2.R /path/to/output_of_1st_script /path/to/OutputDir argument3 default_yes
to run these 2 R scripts I have made the following bash script but does not return anything! do you know how to fix it?
#!/bin/bash
set -e;
set -u;
OUTDIR1="./output1/";
OUTDIR2="./output2/";
INDIR="./input/";
argument1=$1;
argument2=$2;
argument3=$3;
mkdir ${OUTDIR1} || true;
#to run the 1st script
ls -1 ${INDIR}/*/inputfile.txt | sort -V | while read infile; do
b=$(basename $(dirname "$infile"));
./script1.R \
-v \
run \
-o "${of}" \
-f 0 \
argument1 \
argument2 \
"${b},${infile}" \
;
done
#to run the 2nd script. input of this sript is the output from previous script
ls -1 ${OUTDIR1}/*/output.txt | sort -V | while read outfile1; do
b=$(basename $(dirname "$outfile1"));
./script1.R \
-v \
run \
-o "${of}" \
-f 0 \
argument3 \
"${b},${outfile1}" \
;
done
I'm new to terminal scripts and I'm trying to convert
osascript -e 'tell app "Terminal"
do script "ssh -t jgreen#dev-jgreen-bs pwd"
end tell'
This works with multiline as so but I want a one-line script, but I can't quite get it right. I keep getting a 2741 error, I know it is syntax I am failing with.
I have tried /, ,, \n,-e,&,to as separators.
You'll need to add a few sections to this one line command:
osascript -e 'tell app "Terminal"' -e 'do script "ssh -t jgreen#dev-jgreen-bs pwd"' -e 'end tell'
Each line in an applescript needs to be broken into sections on a single line osascript command in terminal. You add the "-e" for each section and the single apostrophe.
Hope this helps.
I wonder if I can use character set found in http://www.utf8-chartable.de/unicode-utf8-table.pl?utf8=0x&unicodeinhtml=hex to replace accented or special characters using sed or tr.
I have a script that uses sed command. Sometimes it does not work :(
it goes like this:
sed -e "s/\"//g" \
-e "s/\'//g" \
-e "s/[$]/S/g" \
-e "s/%//g" \
-e "s/&/E/g" \
-e "s/#/a/g" \
-e "s/\^//g" \
-e "s/\`//g" \
-e "s/|//g" \
-e "s/~//g" \
-e "s/¡/i/g" \
-e "s/¨//g" \
-e "s/\´//g" \
-e "s/¢/c/g" \
-e "s/£//g" \
-e "s/§//g" \
-e "s/¬//g" \
-e "s/°/o/g" \
-e "s/·/./g" \
-e "s/¹/1/g" \
-e "s/²/2/g" \
-e "s/³/3/g" \
-e "s/¿//g" \
-e "s/ª/a/g" \
-e "s/à/a/g" \
-e "s/á/a/g" \
-e "s/â/a/g" \
-e "s/ã/a/g" \
-e "s/ä/a/g" \
-e "s/å/a/g" \
-e "s/æ/ae/g" \
Os, I am thinking if I use hex or octal unicode codes to be used in sed, it would work. But I do not know how...
e.g. echo ¢ | sed 's/\x{00A2}/cent/g'
I appreciate your help.
Your script
…works fine for me. Every substitution is performed as expected, except for one:
-e "s/\'//g" \
should be
-e "s/'//g" \
(There's no need to escape the single quote, your expression is between double quotes.)
Applied to a file containing
"'$%&#^`|~¡¨´¢£§¬°·¹²³¿ªàáâãäåæ
it ouputs:
S E a i c o.123 aaaaaaaae
(Without spaces. I added them to make it easier to compare orginialm pattern and substitution.)
Hexa code
For replacing with hexadecimal code, use following syntax:
echo ¢ | sed 's/\xC2\xA2/cent/g'
Why is so? An hexadecimal value XX is given to sed with \xXX syntax (see info sed). And for your ¢ character, the third column of table on webpage you link gives 0xc2 0xa2.
Encoding
As you are trying to replace UTF-8 encoded characters, I assume your file uses UTF-8 encoding. If it is not, a quick solution would be to convert it (or a copy of it) into UTF-8 (e.g. with your favorite text editor).
I have a Makefile which creates build a programme called monitor:
fo/monitor: fo/monitor.c fo/inotify.c
(cd fo ; $(MAKE) monitor)
I have two types of system that I can run my Make on, and only wish to have have one installer.
So I would like to add an IF statement to this to check for a file, and if it exists, then to build the monitor.
fo/monitor:
if [ -f path/to/file/exists ]; \
then \
fo/monitor.c fo/inotify.c \
(cd fo ; $(MAKE) monitor) \
else \
echo "" >/dev/null \
fi \
The problem is, when I attempt to run the Makefile - it falls over becuase it does not like this code - can anyone point me in the right direction please?
The fo/monitor.c and fo/inotify.c have to be added to the targets dependencies, and not in the if statement. You can also use the -C option of make instead of using a subshell. And you do have to echo nothing in nothing.
This should be good:
fo/monitor: fo/monitor.c fo/inotify.c
if [ -f path/to/file/exists ]; then \
$(MAKE) -C fo monitor; \
fi
Another way is to depend on that target only if path/to/file/exists exists:
# add fo/monitor dependency only if path/to/file/exists exists
all : $(shell test -e path/to/file/exists && echo "fo/monitor")
fo/monitor: fo/monitor.c fo/inotify.c
${MAKE} -C ${#D}
I just started using R on Terminal because its tab function. But I have no idea how to send the selected text in TextMate to the Terminal. Could expertise show me how to write the Command in TextMate?
Thanks!
Here is the exact TextMate command that I currently use. Hope it helps!
rawText="$(cat | sed 's/ / /g;')"
osascript -e 'on run(theCode)' \
-e ' tell application "Terminal"' \
-e ' do script theCode in window 1' \
-e ' end tell' \
-e 'end run' -- "$rawText"
open "txmt://open?line=$(($TM_LINE_NUMBER+1))&column=1000000" &
TextMate is MacOS, right? Is so, then this is from the R ?connections page:
"Mac OS X users can use pipe("pbpaste") and pipe("pbcopy", "w") to read from and write to that system's clipboard."
You can "paste" from R-Clipboards into Terminal sessions. You can also send file content from TextMate:
http://manual.macromates.com/en/shell_commands#executing_commands_filtering_text