I fuzz up with issue,
I create 2 files .r and .sh in same home folder, here is my code
abc.sh
#!/bin/bash
NAME="Ali"
Rscript /home/cdsw/abc.R $NAME --save
abc.R
#args <- commandArgs(trailingOnly = TRUE), I used this option also
args <- commandArgs()
message(args)
I unable to get the output,
Here is the output
/usr/local/lib/R/bin/exec/R--sense--no-readline
Please help me out...
Related
I want to run a R script for 23 chromosomes. The files I need to read are "chr_1.txt, chr_2.txt,...,chr_23.txt"
So I have a bash file
#!/bin/bash
for chr in {1..23}; do \
sbatch torunR.sh "chr_$"
done
and another bash file (torunR.sh)
R CMD BATCH script.R
The problem is that I don't know how to read a different "chr_X.txt" files in R (script.R).
I have tried chr_$ or chr_*', for example:
geno = read.table(file="chr_*.txt") but it didn't work.
Any ideas?? Thanks!!
I'm not an expert in bash scripting so I don't know the necessity of batching, but I would modify your R script to use a command line argument created within the loop.
Your R script would look like this:
## script.R
targetFile <- commandArgs(trailingOnly = TRUE)
# optional status message
cat(sprintf("Processing file %s\n", targetFile))
geno <- read.table(file = targetFile)
Then modify your bash script to be something along the lines of
#!/bin/bash
for chr in {1..23}; do \
Rscript script.R "chr_$chr.txt"
done
Result when running bash script (with optional status message):
$ ./bashScript.sh
Processing file chr_1.txt
Processing file chr_2.txt
Processing file chr_3.txt
Processing file chr_4.txt
Processing file chr_5.txt
Processing file chr_6.txt
Processing file chr_7.txt
Processing file chr_8.txt
Processing file chr_9.txt
Processing file chr_10.txt
Processing file chr_11.txt
Processing file chr_12.txt
Processing file chr_13.txt
Processing file chr_14.txt
Processing file chr_15.txt
Processing file chr_16.txt
Processing file chr_17.txt
Processing file chr_18.txt
Processing file chr_19.txt
Processing file chr_20.txt
Processing file chr_21.txt
Processing file chr_22.txt
Processing file chr_23.txt
I've been using the here package to make my projects more portable. It works great apart from when I use cronR to schedule some of my scripts. When I run my_script.R from Rstudio I get a message from library(here):
here() starts at /home/pd/projects/my_proj
When I set script.R to run using cronR I get a different message:
here() starts at /home/pd
Which is where my_schedule.cron is stored. Ideally I want to keep my_schedule.cron where it is. I can see from the logs that my_script.R runs fine apart from when it comes to saving data because the path used by here() is incorrect. Is there anyway to get the here function to detect the project dir when my_script.R is run from cronR or the terminal?
You can modify the command cmd that is usually created with cron_rscript() by adding cd to your project folder followed by the usual part:
cmd <- "cd /home/pd/projects/my_proj && /usr/lib/R/bin/Rscript ./my_script.R >> ./my_script.log 2>&1"
cron_add(command = cmd, frequency = 'daily', at = '18:00')
If the first line of your #rstats script is wd <- here(), I will come
into your lab and SET YOUR COMPUTER ON FIRE.
Learn how to use environment variables
wd <- Sys.getenv("HOME")
wd <- file.path(wd, "projects", "my_proj")
Or use the 'Additional arguments to Rscript' element in the cronR user interface to pass something extra to the Rscript and fetch it with commandArgs().
If you don't use the cronR interface but cron_rscript, use cronR::cron_rscript(..., rscript_args = "/home/pd/projects/my_proj")
args <- commandArgs(trailingOnly = TRUE)
if(length(args) > 0){
wd <- args[1]
}
I'm trying to download a zip file from a source, unzip it and after move to another directory.
First the download:
if (!file.exists("inst/extdata/sp_resultados_universo")) {
tmp <- tempfile(fileext = ".zip")
download.file("ftp://ftp.ibge.gov.br/Censos/Censo_Demografico_2010/Resultados_do_Universo/Agregados_por_Setores_Censitarios/SP_Capital_20180416.zip", tmp, quiet = TRUE)
unzip(tmp, exdir = "inst/extdata/sp_resultados_universo", junkpaths=T)
unlink(tmp)
}
The file i want is on this directory inst/extdata/sp_resultados_universo/SP Capital/Base informa�oes setores2010 universo SP_Capital (codificação inválida)/CSV/, so when i try copy to inst/extdata/sp_resultados_universo/ i get an error
file.rename("inst/extdata/sp_resultados_universo/SP%20Capital/Base%20informa%87oes%20setores2010%20universo%20SP_Capital(condificação inválida)/CSV/Domicilio02_SP1.csv",
"inst/extdata/sp_resultados_universo/Domicilio02_SP1.csv")
Warning message:
In file.rename("inst/extdata/sp_resultados_universo/SP%20Capital/Base%20informa%87oes%20setores2010%20universo%20SP_Capital(condificação inválida)/CSV/Domicilio02_SP1.csv", :
it was not possible to rename file 'inst/extdata/sp_resultados_universo/SP%20Capital/Base%20informa%87oes%20setores2010%20universo%20SP_Capital(condificação inválida)/CSV/Domicilio02_SP1.csv'
for 'inst/extdata/sp_resultados_universo/Domicilio02_SP1.csv',
reason 'File or directory not found'
I'm translating the error message, so it could be inconsistent with english message.
I can change the directory name or move the file manually, but breaks the flow and it's not nice for reproducibility. How can i handle it inside R?
My system info:
Sys.info()
sysname
"Linux"
release
"4.9.0-6-amd64"
version
"#1 SMP Debian 4.9.88-1+deb9u1 (2018-05-07)"
machine
"x86_64"
Many thanks in advance for any help.
when using R you can interact with the linux shell (or the windows cmd line) through a call to system() where you put the quoted command just as you would use in the shell,
for instance:
system("pwd") # prints current working directory
system("date") # prints
system("ls | grep .R") # prints a list of r scripts in the current working directory
system("mv file.txt /home/new_directory/file.txt") # moves your file to another directory
I'm trying to run some demo R code for the optparse package that I got from R-bloggers. I am using ubuntu 14.04
The code is:
#!/usr/bin/env Rscript
library(optparse)
option_list = list( make_option(c("-f", "--file"),
type="character", default=NULL,
help="dataset file name",
metavar="character"),
make_option(c("-o", "--out"),
type="character", default="out.txt",
help="output file name [default=
%default]", metavar="character")
);
opt_parser = OptionParser(option_list=option_list);
opt = parse_args(opt_parser);
if (is.null(opt$file)){
print_help(opt_parser)
stop("At least one argument must be supplied (input file).n",
call.=FALSE)
}
## program...
df = read.table(opt$file, header=TRUE)
num_vars = which(sapply(df, class)=="numeric")
df_out = df[ ,num_vars]
write.table(df_out, file=opt$out, row.names=FALSE)
If the entire script is saved in a file called yasrs.R using the call:
Rscript --vanilla yasrs.R
should return the help messages.
I get an error:
Rscript --vanilla yasrs.R Error in library(optparse) : there is no package called ‘optparse’
I have installed the package (optparse) through RStudio when writing the code and also ensured that it is installed when calling from the terminal. Both terminal and RStudio are running the same R version.
Any suggestions would be appreciated.
Where did RStudio install optparse? Get that from packageDescription("optparse").
Then check the output of .libPaths() in your Rscript environment and your RStudio environment. Maybe RStudio stuck it somewhere that RScript doesn't look.
Then check that even though they may be the same version of R, they might be two different installations. What does R.home() say in each?
One or more of these things will show you why it doesn't find it. Solution is probably to write and run a little RScript that installs it, then you should be fairly sure its going to go in a location that RScript will find it in future.
I am trying to run exiftool through R to get metadata from photos using the system() command. When I run this on a mac, it works fine, but from windows I am not linking up with the cmd.exe properly and getting the following error from this code:
exif_datetime <- function(path) {
exif_cmd <- 'exiftool.pl -T -r -DateTimeOriginal '
cmd <- paste(exif_cmd, "'", path, "'" ,sep='')
exif_time <- system(cmd, intern = TRUE)
exif_time
}
photo_time <- exif_datetime('C:/Users/photo.jpg')
photo_time
Error in system(cmd, intern = TRUE) :
'CreateProcess' failed to run 'C:\Windows\exiftool.pl -T -r -DateTimeOriginal 'C:/Users/photo.jpg''
When I run the exiftool command from cmd.exe in Windows, I get the proper result. My exiftool.pl is in the C:Windows folder on my computer. Is there something regarding the PATH that I am missing? Also, I remember something about windows needing a shell, but I have not figured out if that is what I need in my case nor how to create one properly.
Thanks for all your suggestions. I found a solution that works for me, involving the shell() command. I thought that it had to be incorporated with the system() command somehow, but it appears that it will work on its own.
exif_datetime <- function(path) {
exif_call <- 'exiftool.pl'
exif_cmd<-' -r -T -DateTimeOriginal '
exif_timestamp <- shell(paste(exif_call, exif_cmd, path), intern=T)
exif_timestamp
}
photo_time <- exif_datetime('C:/Users/photo.jpg')
photo_time
[1] "2016:02:14 11:50:29"