[R] Defining and running a simple function

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Feb 2 08:49:58 CET 2006


To print something you use print().

Auto-printing ensures that the value of an expression is (usually) printed 
at the top-level prompt and not otherwise.  So your function could be

fn <- function() {
     print("hello world")
     seq(0, 0.98, 0.2)
}

(Please use a more informative subject line.)


On Thu, 2 Feb 2006, Thomas L Jones wrote:

> I am trying, with extremely limited success, to get the R software to
> do some simple computations. Specifically, I defined a test function
> as follows:
>
> fn <- function () {
>
> hw <- "hello world"
>
> hw
>
> s <- seq (0, 0.98, 0.02)
>
> s
>
> }
>
> The output should just be the words "hello world," followed by a
> sequence going from 0 to 0.98.
>
> However, when I call the little test function:
>
> fn ()
>
> the words "hello world" get lost and do not come out. Instead, the
> output begins with
>
> [1] 0.00 0.02, etc
>
> However, when I write a test function for just the words "hello
> world," the words come out just fine.
>
> Here is the setup:
>
> R-2.2.0 using the English language and the console
> Windows XP Home Edition Service Pack 2
> AVG Antivirus Free Version
> Windows built-in firewall
> The R buffered output is disabled.
> Mozilla Firefox 1.5
> Administrator privileges are enabled.
>
> Your advice?
>
> Tom Jones
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list