Dear All: I create a file named :"P_Value" with only one simple function: P_Value <- function( Table ) { S = fisher.test(Table, alternative = "two.sided"); return(S$p.value); } However, it seems that it's impossible to use this function directly, because no matter where I save this file, R always reports a missing function. For example, > M = matrix(c(25194,2783,307,20),2) > P_Value(M) Error: could not find function "P_Value" Sincerely thanks a lot for any suggestions. Lowie -- Without dream, life is incomplete! [[alternative HTML version deleted]]
Dear Lowie, To use a function in R that you have saved in a file, you need to source it in first (see ?source). Try this, source(file="pathtoyourfile/P_Value.R") #or whatever your file is named exactly Once you do that, you should be able to use your function. If you always want it to load at startup, you can modify your Rprofile, I believe. (see ?Rprofile) Josh On Sun, May 2, 2010 at 11:19 AM, A-A_lowie li <lowieli at gmail.com> wrote:> Dear All: > > ? ? ? ?I create a file named :"P_Value" with only one simple function: > > ? ? ? ?P_Value <- function( Table ) { > ? ? ? ? ? ?S = fisher.test(Table, alternative = "two.sided"); > ? ? ? ? ? ?return(S$p.value); > ? ? ? ?} > > ? ? ? ?However, it seems that it's impossible to use this function > directly, because no matter > ? ? ? ?where I save this file, R always reports a missing function. For > example, > > ? ? ? > M = matrix(c(25194,2783,307,20),2) > ? ? ? > P_Value(M) > ? ? ? Error: could not find function "P_Value" > > ? ? ? Sincerely thanks a lot for any suggestions. > > Lowie > > -- > Without dream, life is incomplete! > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Joshua Wiley Senior in Psychology University of California, Riverside http://www.joshuawiley.com/
Augusto.Sanabria at ga.gov.au
2010-May-03 06:18 UTC
[R] How could I use a function saved in one file ? [SEC=UNCLASSIFIED]
Lowie, You can save the function in a file called 'P_value.txt' or something like it (I prefer 'P_value.r'). Then you load the function into your R session by Using: Source("P_value.txt",sep="") ). It is better if you keep all your R functions into The same directory, say directory "my_rfunc", Then you load your function using: Source(paste(my_rfunc,"P_value.txt",sep="") ) Hope it helps, Augusto -------------------------------------------- Augusto Sanabria. MSc, PhD. Mathematical Modeller Risk & Impact Analysis Group Geospatial & Earth Monitoring Division Geoscience Australia (www.ga.gov.au) Cnr. Jerrabomberra Av. & Hindmarsh Dr. Symonston ACT 2601 Ph. (02) 6249-9155 -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of A-A_lowie li Sent: Monday, 3 May 2010 4:20 To: r-help at r-project.org Subject: [R] How could I use a function saved in one file ? Dear All: I create a file named :"P_Value" with only one simple function: P_Value <- function( Table ) { S = fisher.test(Table, alternative = "two.sided"); return(S$p.value); } However, it seems that it's impossible to use this function directly, because no matter where I save this file, R always reports a missing function. For example, > M = matrix(c(25194,2783,307,20),2) > P_Value(M) Error: could not find function "P_Value" Sincerely thanks a lot for any suggestions. Lowie -- Without dream, life is incomplete! [[alternative HTML version deleted]] ______________________________________________ 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.
Bill.Venables at csiro.au
2010-May-03 06:18 UTC
[R] How could I use a function saved in one file ?
Try using source("P_Value") before anything else. Also, P_Value can be written as a one-liner: P_Value <- function(Table) fisher.test(Table)$p.value so you don't really need a separate function at all. Bill Venables CSIRO/CMIS Cleveland Laboratories -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of A-A_lowie li Sent: Monday, 3 May 2010 4:20 AM To: r-help at r-project.org Subject: [R] How could I use a function saved in one file ? Dear All: I create a file named :"P_Value" with only one simple function: P_Value <- function( Table ) { S = fisher.test(Table, alternative = "two.sided"); return(S$p.value); } However, it seems that it's impossible to use this function directly, because no matter where I save this file, R always reports a missing function. For example, > M = matrix(c(25194,2783,307,20),2) > P_Value(M) Error: could not find function "P_Value" Sincerely thanks a lot for any suggestions. Lowie -- Without dream, life is incomplete! [[alternative HTML version deleted]] ______________________________________________ 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.