7

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

7
  • Have a look at this: stackoverflow.com/questions/3506007/… Commented May 16, 2017 at 15:24
  • 1
    You should not type ? in front of your command. Rscript test.R does the trick. Commented May 16, 2017 at 15:41
  • > Rscript test.R Error: unexpected symbol in "Rscript test.R"
    – PAVAN
    Commented May 17, 2017 at 2:48
  • 1
    If I type the command source("test.R") it is showing the output.
    – PAVAN
    Commented May 17, 2017 at 2:50
  • 1
    'Rscript test.R' should be entered at the operating system command prompt, not inside R.
    – user20637
    Commented May 17, 2017 at 8:37

5 Answers 5

5

From comments:

Try the following command

source("test.R")

Make sure you set the working directory correctly

2

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
  • This is from the command line, not the 'R console' as the author intended; however it solved my problem.
    – Matt
    Commented Jul 28, 2023 at 20:30
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.

-1

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

-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

1
  • 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
    Commented Mar 27, 2023 at 22:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.