Running a script
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.
当前内容版权归 cookbook-r.com 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 cookbook-r.com .