Hi>i have aproblem withe execution of my function>first, i wrote my function in the script of R >nom_fonction <- function(arg1[=expr1], arg2[=expr2], ...){bloc d'instructions }> when i want to have the result i mean the laste instruction in the bloc of > instruction , i try to >wrote the name of function>source(aj.fun)Error in readLines(file, warn = FALSE) : 'con' is not a connection>return(aj.fun)Error: no function to return from, jumping to top level thanks for helping hafida, univ of algeria -- View this message in context: http://r.789695.n4.nabble.com/Execution-of-a-function-tp4639424.html Sent from the R help mailing list archive at Nabble.com.
On Tue, Aug 7, 2012 at 11:26 AM, hafida <hafida-06 at hotmail.fr> wrote:> Hi > >>i have aproblem withe execution of my function > >>first, i wrote my function in the script of R >>nom_fonction <- function(arg1[=expr1], arg2[=expr2], ...){ > bloc d'instructions > } > >> when i want to have the result i mean the laste instruction in the bloc of >> instruction , i try to >>wrote the name of function > >>source(aj.fun) > Error in readLines(file, warn = FALSE) : 'con' is not a connectionsource() takes a file name, not a function name. Cheers, Michael> >>return(aj.fun) > Error: no function to return from, jumping to top levelDon't use return() outside of functions.> > thanks for helping > hafida, univ of algeria > > > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Execution-of-a-function-tp4639424.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.
HI, Suppose, I have a function like this: fun1<-function(x){ ?ifelse(x[1]<0, ?sum(x[min(which(x<0)):(which.max(x>0)-1)]), sum(x[min(which(x<0)):max(which(x<0))]) ) ?} #And I save it as "aj.fun" (-- filename) #Then,>source("aj.fun") > fun1function(x){ ?ifelse(x[1]<0, ?sum(x[min(which(x<0)):(which.max(x>0)-1)]), sum(x[min(which(x<0)):max(which(x<0))]) ) ?} a1<-c(-5,-2,4,2,-4,-6,3)> fun1(a1)#[1] -7 Hope this helps. A.K. ----- Original Message ----- From: hafida <hafida-06 at hotmail.fr> To: r-help at r-project.org Cc: Sent: Tuesday, August 7, 2012 12:26 PM Subject: [R] Execution of a function Hi>i have? aproblem withe execution of my function>first, i wrote my function in the script of R >nom_fonction <- function(arg1[=expr1], arg2[=expr2], ...){bloc d'instructions }> when i want to have the result i mean the laste instruction in the bloc of > instruction ,? i try to >wrote the name of function>source(aj.fun)Error in readLines(file, warn = FALSE) : 'con' is not a connection>return(aj.fun)Error: no function to return from, jumping to top level thanks for helping hafida, univ of algeria -- View this message in context: http://r.789695.n4.nabble.com/Execution-of-a-function-tp4639424.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.
Please do keep your replies on the R help list. On Tue, Aug 7, 2012 at 4:17 PM, hafida goual <hafida-06 at hotmail.fr> wrote:> > HI >> >>I know my questions are debile, but please I'm debutante. > >>> source("functionaj") > Error in file(filename, "r", encoding = encoding) : > cannot open the connection > In addition: Warning message: > In file(filename, "r", encoding = encoding) : > cannot open file 'functionaj': No such file or directory > >> source(functionaj) > Error in source(functionaj) : object 'functionaj' not found > >> source('functionaj') > Error in file(filename, "r", encoding = encoding) : > cannot open the connection > In addition: Warning message: > In file(filename, "r", encoding = encoding) : > cannot open file 'functionaj': No such file or directory >Unlike Matlab, R makes no connection between the name of a function and the name of the file wherein it is defined. Say I am at my shell prompt and I create a trivial file like so: $ echo "test <- function(x) return(x)" > test.R I can then start R and see if my function exists: R> test(3) # Error because test() is not yet defined. But then I can source my file R> source("test.R") R> test(3) # it works! [1] 3 But the name doesn't have to make any sense: back at the shell prompt echo "test2 <- function(x) return(x + 1)" > flub.R now in R R> source("flub.R") R> flub(3) # Does not exist R> test2(3) # Works as expected. [1] 4 If you are on Windows, the command source(file.choose()) might be of help. Hope this helps clarify things, Michael>>THANKS AGAIN AGAIN > hafida >
HI Arun and all>my function is: >aj.fun <- function(j, i, X, z, E, beta0, beta1){+ n <- length(X) + iX <- order(X) + iz <- order(z) + e1 <- -(beta)*z[ iz[1:(i - 1)] ] + numer <- E[j] - sum( X[ iX[1:(i - 1)] ] * exp(e1) ) + e2 <- -(beta)*z[ iz[i:n] ] + denom <- sum( exp(e2) ) + numer/denom + }> >how excute this function > >thank you >hafida-- View this message in context: http://r.789695.n4.nabble.com/Execution-of-a-function-tp4639424p4639469.html Sent from the R help mailing list archive at Nabble.com.