These are just four ways. There are many more.
Where are configuration variables defined?
Often they're scattered across the script.
Inefficient and prone to errors.
Mostly this is good, but storing credentials in a script is a very bad idea!
Storing credentials in the environment is somewhat better.
Actually there was quite a long time before RStudio!
Write a hello-world.sh
script.
The first line tells the shell what interpreter to use.
Make it executable.
$ chmod u+x hello-world.sh
Run it.
$ ./hello-world.sh
Hello World!
When to use Rscript
?
Write a hello-world.R
script.
Pipe it through the R interpreter.
$ cat hello-world.R | R --slave
Hello World!
When to just use R
?
By default name
is set to "World"
.
$ cat hello-world-variable.R | R --slave
Hello World!
Sprinkle some shell magic! Use sed
to substitute "useRs"
in place of "World"
.
$ sed 's/World/useRs/' hello-world-variable.R | R --slave
Hello useRs!
By default name
is set to "World"
.
$ cat hello-world-environment.R | R --slave
Hello World!
Defining a NAME
environment variable causes name
to be set to "useRs"
.
$ NAME=useRs && cat hello-world-environment.R | R --slave
Hello useRs!
Buffon's Needle experiment.
Suppose we have a floor made of parallel strips of wood, each the same width, and we drop a needle onto the floor. What is the probability that the needle will lie across a line between two strips?
Changing the value of RATIO
.
$ sed "s/\(^RATIO =\).*/\1 0.25/" buffon-needle-configuration.R
# CONFIGURATION ---------------------------------------------------------------
RATIO = 0.25
SAMPLES = 500000 # Number of times that the needle is dropped.
SEED = 13 # Random seed for repeatability.
# -----------------------------------------------------------------------------
Can change the value of SAMPLES
too.
Can also change the value of both RATIO
and SAMPLES
.
But I only need it for half an hour.
And my budget is $15.
If you have
then you can have this massive computer.
You can spin it up for just half an hour. And it'll be within budget.
|
![]() |
View demo on .
Code available here on .