[R] Problem executing function
Duncan Murdoch
murdoch.duncan at gmail.com
Wed Oct 12 15:22:21 CEST 2011
Comments inline below.
On 12/10/2011 7:47 AM, Divyam wrote:
> sorry correction in the first and result hence received code:
>
> ok. I tested it in two ways. I want to externalise my odbcConnection details
> dsn, uid, and pwd. Hence I created a csv file to have these information.
> Like I showed in the sample function initially, the order of the steps were
> 1) loading of the packages,
> 2) fetching the csv file,
> 3)assigning the dsn, uid, and pwd separately to values,
> 4) establish the connection by using these values .
>
> The sample function I created thus was:
>
> 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];
>
>
> # Establishing ODBC connection;
> connection<- odbcConnect(c, uid=d, pwd=e);
> X<-list(a=a, b=b, c=c,d=d, e=e, connection=connection);
> return(X);
>
> }
> # function call
>
> > fun()
This creates the 6 objects a,b,c,d,e and connection, and stores them in
the list. Then R prints the list, but you didn't save it anywhere
> $a
> [1] "RODBC" "class" "stats" "graphics" "grDevices" "utils"
> [7] "datasets" "methods" "base" "e1071" "RODBC" "class"
> [13] "stats" "graphics" "grDevices" "utils" "datasets" "methods"
> [19] "base" "e1071" "RODBC" "class" "stats"
> [25] "graphics" "grDevices" "utils" "datasets" "methods" "base"
>
> $b
> DSN uid pwd
> 1 x xy abcdef
>
> $c
> [1] x
> Levels: x
>
> $d
> [1] xy
> Levels: xy
>
> $e
> [1] abcdef
> Levels: abcdef
>
> $connection
> RODBC Connection 1
> Details:
> case=nochange
> DSN=x
> Description=test
> UID=xy
> PWD=******
> APP=RStudio
>
> Warning messages:
> 1: package 'RODBC' was built under R version 2.13.1
> 2: package 'e1071' was built under R version 2.13.1
> 3: package 'dummies' was built under R version 2.13.2
>
> To see if the objects were created, I typed
> > b
> Error: object 'b' not found
You need to study "scoping" in R. The variable named "b" was local to
your function call. You copied it's value into X, and returned that,
but the variable b does not live beyond the function call.
If you had done
value <- fun()
then you could retrieve the copy as
value$b
Duncan Murdoch
> >connection
> Error: object 'connection' not found
>
> However, when I simply give the code for fetch directly on the command
> prompt, it works.
>
> > b<- read.csv("path of the csv file", header=TRUE, sep=",",
> > quote="");
>
> object b gets created under data. I checked the path of the file and its
> name. They are correct. So I thought I'll directly give the dsn, uid, and
> pwd information in odbcConnection instead of externalizing it to see how the
> function responds. This is what I did:
>
>
> > fun<- function()
> {
> a<- c(library("RODBC"),library("e1071"));
> connection<- odbcConnect("x",uid="xy",pwd="abcdef");
> X<-list(b=b,connection=connection);
> return(X);
> }
>
> # function call and result
>
> > fun ()
>
> $a
> [1] "e1071" "RODBC" "class" "stats" "graphics"
> [7] "grDevices" "utils" "datasets" "methods" "base"
> [13] "e1071" "RODBC" "class" "stats" "graphics" "grDevices"
> [19] "utils" "datasets" "methods" "base" "e1071"
> [25] "RODBC" "class" "stats" "graphics" "grDevices" "utils"
> [31] "datasets" "methods" "base"
>
> $connection
> RODBC Connection 4
> Details:
> case=nochange
> DSN=x
> Description=test
> UID=xy
> PWD=******
> APP=RStudio
>
> when I try to see the connection object by typing in the command prompt it
> throws an error saying the object is not found:
> > connection
> Error: object 'connection' not found
>
> This is what I meant by the function being created, printed but object not
> created. I should also mention that these steps run perfectly fine when not
> assigned to a function (both the methods). So i guess the code is ok. I am
> missing something but am not able to figure that out. By the looks of it I
> think I have assigned the values to be returned to some object. Any
> thoughts?
>
> Thanks
> Divya
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Problem-executing-function-tp3894359p3897696.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.
More information about the R-help
mailing list