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" Commented May 17, 2017 at 2:48
  • 1
    If I type the command source("test.R") it is showing the output. Commented May 17, 2017 at 2:50
  • 1
    'Rscript test.R' should be entered at the operating system command prompt, not inside R. Commented May 17, 2017 at 8:37

5 Answers 5

7

From comments:

Try the following command

source("test.R")

Make sure you set the working directory correctly

Sign up to request clarification or add additional context in comments.

Comments

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 Comment

This is from the command line, not the 'R console' as the author intended; however it solved my problem.
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.

Comments

0

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

Comments

-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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.