[R] script works in Rstudio but not with Rscript

Henrik Bengtsson henrik.bengtsson at ucsf.edu
Thu Apr 9 02:17:18 CEST 2015


On Wed, Apr 8, 2015 at 11:20 AM, Milt Epstein <mepstein at illinois.edu> wrote:
> OK, this suggestion brings up some interesting results.  No solution,
> however.  But it's interesting ... and maybe some helpful leads.
>
> The basic/short answer to your question is no, it doesn't work with "R
> -f script".
>
> The longer answer: Note that the script is currently set up to be
> called with a command line argument, "./affinity_propagation.R 3".  So
> first I tried "R -f affinity_propagation.R 3".  That failed, but
> because it wasn't using the "3" as an argument.  So I modified the
> code to hard-code the value 3 in there and not read the command line
> argument using commandArgs(TRUE).  Then I ran the script using
> "./affinity_propagation.R" -- and it worked!  "R -f
> affinity_propagation.R" also worked.
>
> Then I noticed the --args command line option for R, and I ran "R -f
> affinity_propagation.R --args 3" on the original script, and it
> failed, the same error as I described below.
>
> Also, when you say "Try loading methods", do you mean just a line like
> the following?:
>
> library(methods)
>
> I tried that as well, and it didn't help, same error.

Yes, that's what I meant.  My suggestion was just a guess given that
it is not that uncommon to see cases where a script works with 'R' but
not with 'Rscript'.  From your answers/tests, it's seems clear that
your problem now has to do with parsing arguments.

/Henrik

>
> So, given all that, any new ideas?  Is the call to commandArgs()
> screwing something up?  Can that be fixed somehow?  Is there another
> library/function I could/should use to read command line arguments?
>
> Hmmm, playing around with things some more, it looks like if I do:
>
> num <- as.integer(args[1])
>
> rather than:
>
> num <- args[1]
>
> things work.  So maybe this is an issue with types, scalar vs. array,
> number vs. string?
>
> Milt Epstein
> Programmer in Computational Genomics
> Institute for Genomic Biology (IGB)
> University of Illinois at Urbana-Champaign (UIUC)
> mepstein at illinois.edu
>
>
> On Wed, 8 Apr 2015, Henrik Bengtsson wrote:
>
>> Does it work with R -f script?  If so, then it's because Rscript does not
>> attaching methods package by default, but R does. Try loading methods at
>> the top of your script.
>>
>> My $.02
>>
>> Henrik
>> On Apr 8, 2015 07:41, "Milt Epstein" <mepstein at illinois.edu> wrote:
>>
>> > Greetings.  I am new to R, but have quite a bit of experience
>> > programming with other languages (e.g., Perl, Java, Python, shell
>> > scripting).  I'm now working on a project where I need to use R.  A
>> > colleague wrote a number of small scripts that work fine in Rstudio,
>> > but a couple of them don't work when run using Rscript (which we're
>> > planning on doing).  The behavior is basically the same on a few
>> > different machines (two of them are Linux, one I think is a Mac).  To
>> > run the scripts using Rscript, we put one of the following lines at
>> > the top of the script:
>> >
>> > #!/usr/bin/Rscript
>> > #!/usr/bin/env Rscript
>> >
>> > or called the script using Rscript:
>> >
>> > Rscript scriptname
>> >
>> > In all cases, the behavior is the same.
>> >
>> > Here's one of the scripts:
>> >
>> > #!/usr/bin/env Rscript
>> >
>> > library(apcluster)
>> > options(stringsAsFactors = FALSE)
>> >
>> > args <- commandArgs(TRUE)
>> > num <- args[1]
>> >
>> > numClusters <- num
>> > mydata <- read.csv("input_data.csv")
>> > xData <- mydata[, 1]
>> > yData <- mydata[, 2]
>> >
>> > fit <- apclusterK(negDistMat(r=2), mydata, K = numClusters)
>> > #Leave the rest commented for now
>> > #output <- data.frame(xData, yData, c(as.data.frame(fit[1]),
>> > as.data.frame(fit[2]), as.data.frame(fit[3])))
>> > #write.csv <- write.table(output, file = "output_AP.csv", sep = ",",
>> > row.names = FALSE, col.names = FALSE)
>> >
>> > Here's a call to the script:
>> >
>> > $ ./affinity_propagation.R 3
>> >
>> > Attaching
>> >
>> > The following object is masked package::
>> >
>> >     heatmap
>> >
>> > Trying p = -15.41969
>> >    Number of clusters: 17
>> > Error in tmpk - K : non-numeric argument to binary operator
>> > Calls: apclusterK ... apclusterK -> .local -> apclusterK -> apclusterK ->
>> > .local
>> > Execution halted
>> >
>> > The line with the expression "tmpk - K" is from the code for the
>> > apclusterK() function (from the apcluster library).  The
>> > definition/value of tmpk is set using a call to the function length().
>> > I mention this because the failure we're getting with the other script
>> > also seems to involve an expression with call to length().  Here's the
>> > error from calling that script:
>> >
>> > $ ./spectral_clustering.R 3
>> > Loading required package: methods
>> > Error in length(tmpsig) * nc : non-numeric argument to binary operator
>> > Calls: specc -> specc -> .local -> matrix
>> > Execution halted
>> >
>> > The script is calling the specc() function in the kernlab library.
>> >
>> > Any ideas what's going on here, why it's not working and what we can
>> > do to get it to work?  Is there something that needs to be set or run
>> > in .Renviron or .Rprofile, say?
>> >
>> > Thanks.
>> >
>> > Milt Epstein
>> > Programmer in Computational Genomics
>> > Institute for Genomic Biology (IGB)
>> > University of Illinois at Urbana-Champaign (UIUC)
>> > mepstein at illinois.edu
>> >
>> > ______________________________________________
>> > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> > https://stat.ethz.ch/mailman/listinfo/r-help
>> > PLEASE do read the posting guide
>> > http://www.R-project.org/posting-guide.html
>> > and provide commented, minimal, self-contained, reproducible code.
>> >
>>
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list