Hello All, I have a series of steps that needs to be run many times. Hence I put them all into a function. There is no problem in function creation, but when I call the function, the steps are not getting executed or only the first step gets executed. What possibly could be the reason? Sample Function and the result: fun <- function () { # Package load into R; a <- c(library("RODBC"),library("e1071")); b <- read.csv("Path of the csv file", header=TRUE,sep=",",quote=""); c <- b[,1]; d <- b[,2]; e <- b[,3]; rm(b); # Establishing ODBC connection; conn <- odbcConnect(c,uid=d,pwd=e); } fun() Warning messages: 1: package 'RODBC' was built under R version 2.13.1 2: package 'e1071' was built under R version 2.13.1 The subsequent csv fetch and odbc connection establishment are not getting executed. Why is the function not getting executed fully? Even if I create a separate function for csv file fetch, it is not being executed. But if I simply type on the command prompt directly b <- read.csv("Path of the csv file", header=TRUE, sep=",",quote=""); it is working. Why is it like this? I am not able to figure out the mistake. Any help will be much useful. Have been struggling with this for quite some time now. Thanks Divya -- View this message in context: http://r.789695.n4.nabble.com/Problem-executing-function-tp3894359p3894359.html Sent from the R help mailing list archive at Nabble.com.
R. Michael Weylandt <michael.weylandt@gmail.com>
2011-Oct-11 15:06 UTC
[R] Problem executing function
Sounds like what you were hoping for does happen in the function environment but isn't returned to the global environment. The "proper" fix is to put the values in a list and return() them as the function output. Michael On Oct 11, 2011, at 9:21 AM, Divyam <divyamurali13 at gmail.com> wrote:> Hello All, > > I have a series of steps that needs to be run many times. Hence I put them > all into a function. There is no problem in function creation, but when I > call the function, the steps are not getting executed or only the first step > gets executed. What possibly could be the reason? > > Sample Function and the result: > > fun <- function () > { > # Package load into R; > a <- c(library("RODBC"),library("e1071")); > b <- read.csv("Path of the csv file", header=TRUE,sep=",",quote=""); > c <- b[,1]; > d <- b[,2]; > e <- b[,3]; > rm(b); > # Establishing ODBC connection; > conn <- odbcConnect(c,uid=d,pwd=e); > } > > fun() > > Warning messages: > 1: package 'RODBC' was built under R version 2.13.1 > 2: package 'e1071' was built under R version 2.13.1 > > The subsequent csv fetch and odbc connection establishment are not getting > executed. Why is the function not getting executed fully? Even if I create a > separate function for csv file fetch, it is not being executed. But if I > simply type on the command prompt directly b <- read.csv("Path of the csv > file", header=TRUE, sep=",",quote=""); it is working. Why is it like this? > I am not able to figure out the mistake. > > Any help will be much useful. Have been struggling with this for quite some > time now. > > Thanks > Divya > > -- > View this message in context: http://r.789695.n4.nabble.com/Problem-executing-function-tp3894359p3894359.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 Michael, Thanks for the reply, but still the problem exists. When I list the objects and return the result, they get printed on the console but the objects do not get created. I am really baffled and clueless as to what the problem is. Divya -- View this message in context: http://r.789695.n4.nabble.com/Problem-executing-function-tp3894359p3897092.html Sent from the R help mailing list archive at Nabble.com.
show the work you did. what is happening, probably, is that you are returning the values, but not assigning them. Sent from my iPad On Oct 12, 2011, at 3:47, Divyam <divyamurali13 at gmail.com> wrote:> Hi Michael, > > Thanks for the reply, but still the problem exists. When I list the objects > and return the result, they get printed on the console but the objects do > not get created. I am really baffled and clueless as to what the problem is. > > Divya > > -- > View this message in context: http://r.789695.n4.nabble.com/Problem-executing-function-tp3894359p3897092.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 Michael, > > Thanks for the reply, but still the problem exists. When I list theobjects> and return the result, they get printed on the console but the objectsdo> not get created. I am really baffled and clueless as to what the problemis. I bet you do not understand basic operations in R. See chapter 2.1 in R-Intro manual coming with you R installation. Object any.object does not exist untill you create it or assign a value to it. x <- 1:10 y <- x*5+10+rnorm(10) any.object <- lm(y~x) Regards Petr> > Divya > > -- > View this message in context: http://r.789695.n4.nabble.com/Problem- > executing-function-tp3894359p3897092.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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.