How to run R codes inside a shell script?

I have an R file (myfile.R). I want to run it using a shell script. How can i do this? I tried this:

#! /bin/bash
Rscript myfile.R

but it gives me this error: rscript: command not found

I also tried this:

#! /bin/bash
R --not-save-- < myfile.R 

It also gives this error: R: command not found

What is the best way to do this?

+5
source share
5 answers

The idea is not to write a shell, but to write an R script. This is for Rscript, and our littler package offered /usr/bin/reven before that.

So just

 #!/usr/bin/Rscript

 cat("Hello, world\n")
 # rest of your code below 

or use

 #!/usr/bin/r

for littler - I have several cron jobs that do just that.

, , , Rscript r /usr/bin, , . Debian Ubuntu.

+6

, . , :

R CMD BATCH [options] infile [outfile] &

.

+2

Cygwin Windows:

#!/cygdrive/c/Progra~1/R/R-3.3.0/bin/x64/Rscript.exe

# dummy example

cat("Hello StackOverflow\n")
+1

R R, youre

user@ubuntu:~/RCode/RCode$ R
> source("data_acquisition.R")
> load_libraries()
> df <- acquire_nps()
> head(df)
...
0

, R ( Linux), , , , R sh.

: Mac-Os ( Mojave)

R-: 3.6

1: R Mac, Brew.

$ brew install r (- R -)

2: sh. (StartR.sh)

$ touch startR.sh

3: startR.sh

$ vim startR.sh

( )

#!/bin/sh
Rscript sample.r
echo " r sample run"
exit

(--- R sample.R -)

sayHello & lt; - function() {

( "") }

() enter image description here

( , , )

: , sample.R , startR.sh

4: .sh

$ chmod 750 startR.sh

5: .sh

$ sh startR.sh

, .

During startup - Warning messages: 1: LC_COLLATE installation failed using "C" 2: LC_TIME installation failed using "C" 3: LC_MESSAGES installation failed using "C" 4: LC_MONETARY installation failed using "C" " 1 " hello "r startup pattern

enter image description here

0
source

Source: https://habr.com/ru/post/1547657/


All Articles