Problem

You want to run R code from a text file.

Solution

Use the source() function.

# First, go to the proper directory
setwd('/home/username/desktop/rcode')

source('analyze.r')

Note that if you want your script to produce text output, you must use the print() or cat() function.

x <- 1:10

# In a script, this will do nothing
x

# Use the print function:
print(x)
#> [1]  1  2  3  4  5  6  7  8  9 10


# Simpler output: no row/column numbers, no text wrapping
cat(x)
#> 1  2  3  4  5  6  7  8  9 10

Another alternative is to run source() with the print.eval=TRUE option.