==========================myf=function(ds=1){ x=rnorm(10) y=rnorm(10) { #start of if if (ds==1) { list(x,y) } else (ds==2) { plot(x,y) } } # end of if } # end of function ========================== Hi All, the problem i am having here is, that I want to be able to control the display, lf ds=1, i want to just have a list, but it seem to always plot... Thanks. casper -- View this message in context: http://n4.nabble.com/writing-function-plot-and-if-problem-tp1839091p1839091.html Sent from the R help mailing list archive at Nabble.com.
Tena koe Caspar Try removing the (ds==2) - I'm not sure how you expect R to interpret that. HTH .... Peter Alspach> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of casperyc > Sent: Wednesday, 14 April 2010 10:42 a.m. > To: r-help at r-project.org > Subject: [R] writing function ( 'plot' and 'if') problem > > > ==========================> myf=function(ds=1){ > x=rnorm(10) > y=rnorm(10) > > { #start of if > if (ds==1) > { > list(x,y) > } > > else (ds==2) > { > plot(x,y) > } > > } # end of if > > } # end of function > ==========================> > Hi All, > > the problem i am having here is, > that I want to be able to control the display, > lf ds=1, i want to just have a list, > > but it seem to always plot... > > Thanks. > > casper > -- > View this message in context: http://n4.nabble.com/writing-function- > plot-and-if-problem-tp1839091p1839091.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting- > guide.html > and provide commented, minimal, self-contained, reproducible code.
On Apr 13, 2010, at 5:41 PM, casperyc wrote:> > ==========================> myf=function(ds=1){ > x=rnorm(10) > y=rnorm(10) > > { #start of if > if (ds==1) > { > list(x,y) > } > > else (ds==2)?"if" That seems odd. I would expect this function to return TRUE or FAKSE rather than proceeding to a plot function. There is not usually a condition following the else{} step. -- David.> { > plot(x,y) > } > > } # end of if > > } # end of function > ==========================> > Hi All, > > the problem i am having here is, > that I want to be able to control the display, > lf ds=1, i want to just have a list, > > but it seem to always plot... > > Thanks. > > casper > -- > View this message in context: http://n4.nabble.com/writing-function-plot-and-if-problem-tp1839091p1839091.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
On Apr 13, 2010, at 5:41 PM, casperyc wrote:> > ==========================> myf=function(ds=1){ > x=rnorm(10) > y=rnorm(10) > > { #start of if > if (ds==1) > { > list(x,y) > } > > else (ds==2) > { > plot(x,y) > } > > } # end of if > > } # end of function > ==========================> > Hi All, > > the problem i am having here is, > that I want to be able to control the display, > lf ds=1, i want to just have a list,aPerhaps taking out the entirely extraneous condition for else and "linking up" the else clause: myf=function(ds=1){ x=rnorm(10) y=rnorm(10) #start of if if (ds==1) { list(x,y) } else { plot(x,y) } } # end of if > myf(1) [[1]] [1] -0.5738623 0.6937577 -1.0801386 0.9465693 1.0054440 -1.5523713 1.5912727 0.5426036 1.8980418 -0.5922542 [[2]] [1] -1.4519795 -0.1556495 -2.5242081 -0.6671736 0.5947123 -0.6051199 -1.3711271 0.1647910 0.1616131 -0.2563056 -- David.> > but it seem to always plot... > > Thanks. > > casper > -- > View this message in context: http://n4.nabble.com/writing-function-plot-and-if-problem-tp1839091p1839091.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Seemingly Similar Threads
- use of 'apply' for 'hist'
- R numerical integration
- The fastest way to select and execute a few selected functions inside a function
- Calling the curve function with a character object converted into an expression
- Infinite recursion in S3 methods crashes R on windows (related to PR#8203?)