1

I've written a text file containing script for R. I've gotten it to run under Windows from a .bat file running a .txt file under R with CMD BATCH.

I'm trying to replicate that (minus the clickability) in the Terminal

I've changed the permissions for program execution, I've set the file to have the shebang, and have tried rewriting for it a few different programmes such as

#!/usr/bin/R

library(rvest)

library(plyr)

which returns an error "Syntax error near unexpected symbol 'rvest'

and

#!/home/robert/Téléchargements/R-3.2.3/src/unix/Rscript.c

library(rvest)

library(plyr)

which also returns an error "Syntax error near unexpected symbol 'rvest'

Separately, on both of these I changed the file extension from nothing to .R

In one case it gave the same error, in the other it started a session of R but didn't execute the commands.

I realise it's a messy question, but I'm having difficulty getting these ducks in a row.

curly
  • 43
  • 1
  • 7

1 Answers1

0

Ultimately, this was what worked:

R < /home/robert/R/scraper1.R --no-save

But here's the rest of my answer in case that doesn't work for someone else:


I'm not sure if you've seen any of these, but here's some stuff to try:

Dupe?

The top answer from a very similar post: I'm going to assume you googled your question before posting it, so I'm sure you've already seen this, but it's not referenced in your question, so here it is [Source]:

Content of script.r:

#!/usr/bin/Rscript

cat("Hello")

Invocation from command line:

./script.r


?Rscript

?Rscript allows you to run R scripts in a Unix-esque system [Source]:

    ## example #! script for a Unix-alike

    #! /path/to/Rscript --vanilla --default-packages=utils
    args <- commandArgs(TRUE)
    res <- try(install.packages(args))
    if(inherits(res, "try-error")) q(status=1) else q()


Batch

Here's something from an old R mail pipe [Source]:

  1. Place the line: R --vanilla < foo.txt foo.results into a file named foo.batch. No other text should be in the file.

  2. Make this file executable via chmod 755 foo.batch

  3. At the command line try at -f foo.batch now or perhaps, batch -f foo.batch.

  4. If this does not work, ask your system administrator how to set up a batch process.

The advantage of the batch process is 1. you need not be logged in, 2. your job will take a lower priority than interactive jobs.


R -e

Loading two libraries and running an R command [Source]

R -e 'library("rmarkdown");library("knitr");rmarkdown::render("NormalDevconJuly.Rmd")'

R -e 'library("markdown");rpubsUpload("normalDev","NormalDevconJuly.html")'


Other

  • R < scriptname.R --no-save [Source]
  • $ source("scriptname.R") [Source]
Community
  • 1
  • 1
Travis Heeter
  • 13,002
  • 13
  • 87
  • 129
  • Your answer's given me a couple of things to test, I'll play around it with it for a few hours. Fingers-crossed! – curly Jan 21 '17 at 16:35
  • R < /home/robert/R/scraper1.R --no-save for the win! Thank you! – curly Jan 21 '17 at 16:42
  • Cool, glad to hear. – Travis Heeter Jan 21 '17 at 16:49
  • Also, just fyi, if you put tick marks around in-line code, it will be easier to decipher it from the rest of your text: `\`this\`` will look like `this`. It's not a quote, but the tick mark underneath the tilde (~), typically the key under Esc. – Travis Heeter Jan 21 '17 at 16:57