4

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

PAVAN
  • 85
  • 1
  • 1
  • 8

5 Answers5

3

From comments:

Try the following command

source("test.R")

Make sure you set the working directory correctly

Dum
  • 1,431
  • 2
  • 9
  • 23
1

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

1

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.

Anand
  • 41
  • 6
-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

  • The OP required the correct syntax for running a script in the console. While your suggestion may work, it does not answer the OP's specific question. – L Tyrone Mar 27 '23 at 22:00
-2

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

Liz_Bundy
  • 1
  • 2