I am working on a piece of code that, among other variables, passes the header from a bash-script to R. This may seem silly or stupid, but for my particular needs, it is exactly what I want. So, I have a bash-script:
#!/bin/bash
Rscript script.R "c("column1","column2","column3")"
I have simplified it, but the essentials are there: it starts an instance of Rscript, with the desired header passed as an argument. The R-script contains the following pieces or relevant code:
args<-commandArgs(TRUE) # enable arguments
header <- args[1] # store the first argument in a variable
Now, I want to change the header of my data to the header that I passed as an argument. The following pieces of code all work as desired when I run it from GUI (in my case, Rstudio):
(1) colnames(data) <- header
(2) colnames(data) <- paste(header, sep=" ")
(3) for (i in 1:length(header)){colnames(data)[i] <- header[i]}
All these commands chop up the header in 3 pieces, so that all three columns get a new header (respectively "column1", "column2" and "column3"). However, if I run this from my bash-script like described above (calling Rscript), it does not work. Instead, it gives this output:
c(column1,column2,column3) Chromosome
1 rs10 7
2 rs1000000 12
3 rs10000010 4
4 rs10000012 4
5 rs10000013 4
6 rs10000017 4
Position
1 92221824
2 125456933
3 21227772
4 1347325
5 36901464
6 84997149
...and clearly, this is not what I want. Neither of the three commands listed above work as desired now. This confuses me, since I expect results from my code to be the same regardless of the way I run it, be it Rstudio or Rscript.
Does anyone has an explanation / solution for this? Any ideas are much appreciated.