[R] consolidate three function into one

Bert Gunter bgunter.4567 at gmail.com
Mon Jan 15 19:38:23 CET 2018


"Good" tutorials are a matter of personal taste -- what's good for me may
be terrible for you, and vice-versa.

One set of recommendations is here:

https://www.rstudio.com/online-learning/#R

but you may do even better by web search on "R tutorials" or "Writing R
function tutorials" and the like.

You should certainly have a look at the "Introduction to R" tutorial that
ships with R, since it's immediately available. It's a bit old, however.

Cheers,
Bert



Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )

On Mon, Jan 15, 2018 at 10:21 AM, Ding, Yuan Chun <ycding at coh.org> wrote:

> Thank you, your suggestion is simpler and logically better.  I had
> impression that the last object in a function gets returned, so I did not
> add the print function at the bottom line of the function definition.
> Returning an object and graph the object are different process, I am a
> beginner for writing R function and need to find a good guide source about
> writing R functions.   If you know a good book or website for guidance of R
> function, please let me know.
>
>
>
> Thanks,
>
>
>
> Ding
>
>
>
> *From:* Bert Gunter [mailto:bgunter.4567 at gmail.com]
> *Sent:* Monday, January 15, 2018 10:11 AM
> *To:* Ding, Yuan Chun <ycding at coh.org>
> *Cc:* Richard M. Heiberger <rmh at temple.edu>; r-help at r-project.org
> *Subject:* Re: [R] consolidate three function into one
>
>
>
> That is certainly OK, but you can also just use
>
> print(ggsurvplot(...))
>
> as your final statement.
>
> out <- RFS( ...)
>
> would then return the ggsurvplot object *and* graph it.
>
>
>
> Any good R tutorial or a web search will provide more details on function
> returns, which you might find useful.
>
>
>
>
>
> Cheers,
>
> Bert
>
>
> Bert Gunter
>
> "The trouble with having an open mind is that people keep coming along and
> sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
>
>
> On Mon, Jan 15, 2018 at 9:57 AM, Ding, Yuan Chun <ycding at coh.org> wrote:
>
> Hi Richard,
>
> Thank you so much!!  I understand the problem now,  I assign a name to the
> "ggsurvplot" object and then add print(fig) at bottom of function
> definition, now figure gets printed on screen.
>
> Ding
>
> # function to generate RFS curves
> RFS <- function( inputfile, N ) {
>   cluster<- survfit(Surv(RFS_days2, OV_Had_a_Recurrence_CODE) ~ clusters,
>                     data = inputfile)
>   if( N==2) {palette <- c("red", "black")
>   legend.labs <- c("Cluster1", "Cluster2")
>                      }
>
>   else if(N==3) {palette <- c("red", "black", "green")
>   legend.labs <- c("Cluster1", "Cluster2", "Cluster3")
>                               }
>   else {palette <- c("red", "black","green", "blue")
>   legend.labs <- c("Cluster1", "Cluster2", "Cluster3", "Cluster4")
>            }
>
>
>   fig <-ggsurvplot(cluster, data = inputfile, risk.table = F,
>              palette = palette,
>              ylim=c(0,1),ggtheme = theme_bw(),xlab="Relapse Free Suvival
> (Days)",
>              main = "Survival curve",pval = TRUE,font.x =  16,font.y = 16,
>              font.tickslab = 14,font.legend =c(14,"plain","black"),
>              legend = "bottom",
>              legend.title = "Tree Cluster",
>              legend.labs = legend.labs,
>              lty=1, lwd=3)
>   print(fig)
>                                                      }
>
> -----Original Message-----
> From: Richard M. Heiberger [mailto:rmh at temple.edu]
> Sent: Sunday, January 14, 2018 1:34 PM
> To: Ding, Yuan Chun <ycding at coh.org>
> Cc: Bert Gunter <bgunter.4567 at gmail.com>; r-help at r-project.org
> Subject: Re: [R] consolidate three function into one
>
> FAQ 7.22
> You must print a ggplot object, for example with
> print(m52.2cluster)
>
> For the FAQ, run the line
>    system.file("../../doc/FAQ")
> in R on your computer.
> Open up the resulting filepath in your favorite editor and scroll down to
> 7.22
>
> On Sun, Jan 14, 2018 at 4:21 PM, Ding, Yuan Chun <ycding at coh.org> wrote:
> > Hi Bert,
> >
> > I am sorry to bother you on weekend.
> >
> > I am still struggling on defining a correct function.
> >
> > I first defined the function RFS (see below), then run it by provide the
> two argument.
> >
> > m52.2cluster <-RFS(inputfile =allinfo_m52, N=2 )
> >
> > I do not get error message, but no figure displays on screen. I do not
> know what is going on.
> >
> > Can you help me a little more on this issue?
> >
> > Thank you,
> >
> > Ding
> >
> > # function to generate RFS
> > RFS <- function( inputfile, N ) {
> >   cluster<- survfit(Surv(RFS_days2, OV_Had_a_Recurrence_CODE) ~ clusters,
> >                                data = inputfile)
> >
> >          if( N==2) {palette <- c("red", "black")
> >                            legend.labs <- c("Cluster1", "Cluster2")
> >                             }
> >
> >   else if(N==3) {palette <- c("red", "black", "green")
> >                       legend.labs <- c("Cluster1", "Cluster2",
> "Cluster3")
> >                             }
> >
> >                  else {palette <- c("red", "black","green", "blue")
> >                            legend.labs <- c("Cluster1", "Cluster2",
> "Cluster3", "Cluster4")
> >                           }
> >
> >   ggsurvplot(cluster, data = inputfile, risk.table = F,
> >              palette = palette,
> >              ylim=c(0,1),ggtheme = theme_bw(),xlab="Relapse Free Suvival
> (Days)",
> >              main = "Survival curve",pval = TRUE,font.x =  16,font.y =
> 16,
> >              font.tickslab = 14,font.legend =c(14,"plain","black"),
> >              legend = "bottom",
> >              legend.title = "Tree Cluster",
> >              legend.labs = legend.labs,
> >              lty=1, lwd=3)
> >                                 }
> >
> > From: Bert Gunter [mailto:bgunter.4567 at gmail.com]
> > Sent: Sunday, January 14, 2018 9:50 AM
> > To: Ding, Yuan Chun <ycding at coh.org>
> > Subject: Re: [R] consolidate three function into one
> >
> > I have not looked at your code *at all*, so I am not sure what exactly
> you mean by "consolidate."
> > Is this what you are after?
> >
> > f1 <- function(...) { code 1}
> > f2 <- function(...) { code 2}
> > f3 <- function(...) { code 3}
> > ## can be "consolidated as":
> > f <- function( someargs, ...){
> > if( expr(someargs)) f1(...)
> > else if(expr2(someargs)) f2(...)
> > else f3(...)
> > }
> > The "..." argument to f can soak up any named additional arguments you
> wish to pass to f1, f2, or f3.
> > My apologies if this is offbase or too vague, and feel free to ignore in
> that case. I don't have the patience to go trough your code in detail.
> Others may and give you what you want.
> > Cheers,
> > Bert
> >
> >
> >
> > Bert Gunter
> >
> > "The trouble with having an open mind is that people keep coming along
> and sticking things into it."
> > -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
> >
> > On Sun, Jan 14, 2018 at 9:17 AM, Ding, Yuan Chun <ycding at coh.org<mailto:
> ycding at coh.org>> wrote:
> > HI R users,
> >
> > I construct dendrogram tree and cut the tree into different clusters,
> then generate survival curves by the following three functions. All
> variables are included in an inputfile.
> >
> > Can you help me to consolidate the following three function into one
> functions?  I thought about using if  else function, but not sure how to do
> it.
> >
> > Thank you,
> >
> > Ding
> >
> > # function to generate RFS
> > RFS2cluster <- function( inputfile ) {
> >   cluster2<- survfit(Surv(RFS_days, OV_Had_a_Recurrence_CODE) ~ clusters,
> >                                data = inputfile)
> >
> >   ggsurvplot(cluster2, data = inputfile, risk.table = F,
> >              palette = c("red", "black"),
> >              ylim=c(0,1),
> >              ggtheme = theme_bw(),
> >              xlab="Relapse Free Suvival (Days)",
> >              main = "Survival curve",
> >              pval = TRUE,
> >              font.x =  16,
> >              font.y = 16,
> >              font.tickslab = 14,
> >              font.legend =c(14,"plain","black"),
> >              legend = "bottom",
> >              legend.title = "Tree Cluster",
> >              legend.labs = c("Cluster1", "Cluster2"), lty=1, lwd=3) }
> >
> > RFS3cluster <- function( inputfile ) {
> >   cluster3<- survfit(Surv(RFS_days, OV_Had_a_Recurrence_CODE) ~ clusters,
> >                      data = inputfile)
> >
> >   ggsurvplot(cluster3, data = inputfile, risk.table = F,
> >              palette = c("red", "black", "green"),
> >              ylim=c(0,1),
> >              ggtheme = theme_bw(),
> >              xlab="Relapse Free Suvival (Days)",
> >              main = "Survival curve",
> >              pval = TRUE,
> >              font.x =  16,
> >              font.y = 16,
> >              font.tickslab = 14,
> >              font.legend =c(14,"plain","black"),
> >              legend = "bottom",
> >              legend.title = "Tree Cluster",
> >              legend.labs = c("Cluster1", "Cluster2", "Cluster3"),
> > lty=1, lwd=3) }
> >
> > RFS4cluster <- function( inputfile ) {
> >   cluster4<- survfit(Surv(RFS_days, OV_Had_a_Recurrence_CODE) ~ clusters,
> >                      data = inputfile)
> >
> >   ggsurvplot(cluster4, data = inputfile, risk.table = F,
> >              palette = c("red", "black", "green", "blue"),
> >              ylim=c(0,1),
> >              ggtheme = theme_bw(),
> >              xlab="Relapse Free Suvival (Days)",
> >              main = "Survival curve",
> >              pval = TRUE,
> >              font.x =  16,
> >              font.y = 16,
> >              font.tickslab = 14,
> >              font.legend =c(14,"plain","black"),
> >              legend = "bottom",
> >              legend.title = "Tree Cluster",
> >              legend.labs = c("Cluster1", "Cluster2", "Cluster3",
> > "Cluster4"), lty=1, lwd=3) }
> >
> > -----Original Message-----
> > From: R-help
> > [mailto:r-help-bounces at r-project.org<mailto:r-help-bounces at r-project.o
> > rg>] On Behalf Of imane hajar
> > Sent: Friday, January 12, 2018 7:42 AM
> > To: r-help at r-project.org<mailto:r-help at r-project.org>
> > Subject: [R] Help with packages (methods, stats, stats4)
> >
> > [Attention: This email came from an external source. Do not open
> > attachments or click on links from unknown senders or unexpected
> > emails.]
> >
> >
> >
> >
> >
> > hello,
> > Can you please give me a hand with this problem,well i can't install
> > these
> > packages:
> > - stats
> > - methods
> > - stats4
> >
> > when i tried the following command :  *library(help = "stats") * , it
> gave me this output (*see picture*), so i contacted the Maintainer of the
> package at (*R-core at r-project.org<mailto:R-core at r-project.org> <
> R-core at r-project.org<mailto:R-core at r-project.org>>*) but he said that i
> write to the wrong place.
> >
> > (i want to install those packages in order to use the "DVstats"
> > package)
> >
> > (i have the latest version of R (3.4.3 ) and Rstudio(1.2.240) )
> >
> > thank you
> > Regards
> >
> >
> > ---------------------------------------------------------------------
> > -SECURITY/CONFIDENTIALITY WARNING-
> > This message (and any attachments) are intended solely for the
> > individual or entity to which they are addressed. This communication
> > may contain information that is privileged, confidential, or exempt
> > from disclosure under applicable law (e.g., personal health
> > information, research data, financial information). Because this
> > e-mail has been sent without encryption, individuals other than the
> > intended recipient may be able to view the information, forward it to
> > others or tamper with the information without the knowledge or consent
> > of the sender. If you are not the intended recipient, or the employee
> > or person responsible for delivering the message to the intended
> > recipient, any dissemination, distribution or copying of the
> > communication is strictly prohibited. If you received the
> > communication in error, please notify the sender immediately by
> > replying to this message and deleting the message and any accompanying
> > files from your system. If, due to the security risks, you do not wish
> > to r  eceive further communications via e-mail, please reply to this
> > message and inform the sender that you do not wish to receive further
> > e-mail from the sender. (LCP301)
> > ---------------------------------------------------------------------
> >
> > ______________________________________________
> > R-help at r-project.org<mailto:R-help at r-project.org> mailing list -- To
> > UNSUBSCRIBE and more, see 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.
> >
> >
> >         [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > 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.
>
>
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list