pass character "&" by args to R - r

the following R code is to be execute by command prompt (windows)
# Collect arguments
args <- commandArgs(TRUE)
archivo <- as.character(args[1])
cat(archivo)
like this
C:\Users\Owner\Desktop>Rscript prueba.r "hola&chao"
problem is that command prompt respond
hola
'chao' is not recognized as an internal or external command,
operable program or batch file.
C:\Users\Owner\Desktop>
I need to R accepts the "&" as a character and print it all together in the cat()
What should I do?
Thanks

This isn't an R problem, but a shell problem. You actually executed
Rscript prueba.r hola&chao
instead of
Rscript prueba.r "hola&chao"
In the Windows command shell,
prog1 & prog2
executes both programs in sequence, which is why you see the output of
Rscript prueba.r hola
followed by the output of
chao
The other possibility is that Rscript is a buggy batch file.

Related

How to wait for user input in R-script ran from Windows CMD

I'm new to R and I'm stuck in a very simple task.
I want to run an R script from console, but I want the script to be able to read user inputs.
This is how I'm reading from the script:
library = readLines(n = 1L)
if(library == "1")
{
library = "GUDHI"
}
And this is how I'm running my script from R-Portable with a .bat:
#echo on
cd..
cd..
cd..
cd..
cd..
PATH C:\Users\MyUser\Desktop\App\RFolder\R-Portable\App\R-Portable\bin;%path%
cd C:\Users\MyUser\Desktop\App\RFolder
Rscript Phom.R 1
pause
When I run this .bat it throws an error (Argument is of length zero):
As if the console didn't wait for user input.
The problem is not the .bat code. If I remove the readLines functions from my script and hardcode the input, it works perfectly. I also tried the readline function with no success.
Thanks.
Solution for Interactive R script from Windows CMD:
cat("Prompt Message: ")
library = readLines(con = "stdin", 1)
I'm not sure if the prompt MUST end with ": ", but I had trouble when I removed that piece of string.
This worked for me, I hope this helps somebody.

Executing console command in R

I would like to execute this DOS command under R:
iconv -f ISO-8859-1 -t UTF-8 FileName.md > FileNameNew.md
The above command creates new file after transforming from ISO to UTF.
I have tried execute this command however unsuccessfully with:
system(paste("iconv -f ISO-8859-1 -t UTF-8 FileName.md > FileNameNew.md", sep=""))
This gives me two types of errors:
Invalid argument
No such file or directory
I don't think the issue is the second since when I run the command under R it in fact executes the command as it re-reads the FileName.md, which means he found the file. I think it is just a issue with the > and hence formulation of the command in the system(paste("")) command.
When I rund this command directly under console it works.
The problem is (most likely) simply with where the R session is located. Check this by running getwd() in R and see if it is in the same place as the file. The paste part shouldn't be needed, as it is not really pasting anything (paste combines 2 strings together, while it is one string here).
Solve this by explicitly attaching input and output to those files.
If you would insist on using paste, you could use it for instance like this:
system(paste("iconv -f ISO-8859-1 -t UTF-8 ", getwd(), "/FileName.md > ",
getwd(), "/FileNameNew.md", sep=""))

Loop for interpret the python script (.py)

I'm a beginner for Python. My question might be basic, but I cannot figure it out. What I want to do is to run the following command 100 times in which the variable "num" runs from 0 through 99.
python test.py input_num.txt -i 5 --cor_file=output_num.txt
For example:
python test.py input_0.txt -i 5 --cor_file=output_0.txt
python test.py input_1.txt -i 5 --cor_file=output_1.txt
...
:::
python test.py input_99.txt -i 5 --cor_file=output_99.txt
I know that I have to write a script for running the loop, but cannot figure it out yet. If you have any recommendation, that would be very nice.
I tried to write a script called main.py containing the following commands, but it didn't work.
#!/usr/bin/env python
import test
ext_str = '.txt'
inp = 'input_'
out = 'output_'
for num in range(0,99):
inp += 'num'
inp_str = inp + ext_str
out += 'num'
out_str = out + ext_str
python test.py inp_str -i 5 --cor_file=out_str
Thank you very much.
Best,
Pim
In your case you need to call the python interpreter on various files, e.g. you need to call the "python" shell command, it means that you have to create a shell script (and not a python script, actually you could do it with some python code but it would be way too complicated in your case):
for i in {0..99}
do
python test.py input_$i.txt -i 5 --cor_file=output_$i.txt
done
put the above code in a ".sh" file then don't forget to "chmod +x the ", then run it with ./.sh (given that your are in the current directory).
N.B.: If you need some references on bash scripting : Linux Shell Scripting Tutorial

Using python to execute .R script

After seven hours of googling and rereading through somewhat similar questions, and then lots of trial and error, I'm now comfortable asking for some guidance.
To simplify my actual task, I created a very basic R script (named test_script):
x <- c(1,2,3,4,5)
avg <- mean(x)
write.csv(avg, file = "output.csv")
This works as expected.
I'm new to python and I'm just trying to figure out how to execute the R script so that the same .csv file is created.
Notable results come from:
subprocess.call(["C:/Program Files/R/R-2.15.2/bin/R", 'C:/Users/matt/Desktop/test_script.R'])
This opens a cmd window with the typical R start-up verbiage, except there is a message which reads, "ARGUMENT 'C:/Users/matt/Desktop/test_script.R' __ ignored __"
And:
subprocess.call(['C:/Program Files/R/R-2.15.2/bin/Rscript', 'C:/Users/matt/Desktop/test_script.r'])
This flashes a cmd window and returns a 0, but no .csv file is created.
Otherwise, I've tried every suggestion I could identify on this site or any other. Any insight will be greatly appreciated. Thanks in advance for your time and efforts.
Running R --help at the command prompt prints:
Usage: R [options] [< infile] [> outfile]
or: R CMD command [arguments]
Start R, a system for statistical computation and graphics, with the
specified options, or invoke an R tool via the 'R CMD' interface.
Options:
-h, --help Print short help message and exit
--version Print version info and exit
...
-f FILE, --file=FILE Take input from 'FILE'
-e EXPR Execute 'EXPR' and exit
FILE may contain spaces but not shell metacharacers.
Commands:
BATCH Run R in batch mode
COMPILE Compile files for use with R
...
Try
call(["C:/Program Files/R/R-2.15.2/bin/R", '-f', 'C:/Users/matt/Desktop/test_script.R'])
There are also some other command-line arguments you can pass to R that may be helpful. Run R --help to see the full list.
It might be too late, but hope it helps for others:
Just add --vanilla in the call list.
subprocess.call(['C:/Program Files/R/R-2.15.2/bin/Rscript', '--vanilla', 'C:/Users/matt/Desktop/test_script.r'])

Write errors to file

Is there a way to write my R errors to a file? I run R on bash via:
R --vanilla < myprogram > myprogram.out &
When my program encounters an error (not a syntax error...like an illegal replacement or something) it stops but the error line isn't written to the output file and I don't know what the program was and a lot of the time I log out from the server while it runs.
Thanks,
Josh
Use the R CMD BATCH <infile> <outfile> syntax instead.

Resources