PAVAN
PAVAN

Reputation: 115

How to run R program script from console

Hi friends I am new to R programming. I dont know how to run a R programming script from console. I have created a test.R file and saved in d:|rpro folder and changed the directory path. In console i am trying to run the script by the command

? Rscript test.R

but i am getting the error as Error: unexpected symbol in "? Rscript test.R" Please help me

Upvotes: 7

Views: 21784

Answers (5)

Mike Wood
Mike Wood

Reputation: 1

If you have an Rscript file in some directory with the appropriate .R designator which R console can "see" the just click the R symbol (second from left) at the top of the console

Upvotes: -1

Dum
Dum

Reputation: 1501

From comments:

Try the following command

source("test.R")

Make sure you set the working directory correctly

Upvotes: 6

Anand
Anand

Reputation: 41

If you're on a windows box, then you should search for the Rscript.exe to run your .R file from the command prompt. For example here's a file that I created to print the first 10 numbers of a fibonacci series and I have run it on the Windows command prompt

c:\Program Files\R\R-3.5.1\bin>Rscript c:\users\anand_hs\Documents\fib.R
[1] 0
[1] 1
[1] 1
[1] 2
[1] 3
[1] 5
[1] 8
[1] 13
[1] 21
[1] 34

c:\Program Files\R\R-3.5.1\bin>

[edit-September 10, 2018] You can also use the source() function from within the R console to load your .R file (assuming you're trying to access functions from within a script file). Example:

source('C:/Users/anand/andybash/FuncPractice.R')

Hope this helps.

Upvotes: 1

Liz_Bundy
Liz_Bundy

Reputation: 21

The commands 'Rscript test.R' should be entered at the operating system command prompt, not inside R console.

Upvotes: 0

Harshit Singhal
Harshit Singhal

Reputation: 141

You can do this by

R CMD BATCH your_script.R your_script.log

your_script.R requires full path to r script and your_script.log will store the logs

Upvotes: 2

Related Questions