R: How to pass an object to a function?
up vote
0
down vote
favorite
I'm newbie in R and I'm trying to pass an object to a function but I've got an error when I try to access to a slot of this object.
These are my main code:
x <- new("menu", competition=0, stats=0, query="")
y <- show(x)
show_graphics(y)
After second instruction I've got the object "y" created correctly like you can see in this screencap:
Then, I call to a the function "show_graphics" and pass this object "y" like a parameter. But when I try to access to an slot in the first insctruction of my function:
show_graphics <- function(menu){
data <- getData(menu@query)
......
}
I've got an error that tell me that there is an error to try to access
to the slot "query" of an object to the basic class "function" without
slots.
How can I pass correctly an object to a function? Or how can I get the values from an object passed to a function?
Am I doing something wrong?
Edit I:
This is the code to create the object "menu":
setClass("menu", slots=list(competition="numeric", stats="numeric", query="character"))
show <- function(object) 0
setGeneric("show")
setMethod("show", "menu", function(object){
object@competition <- 14
object@stats <- 1
object@query <- "query"
return (object)
})
Edit II:
getData is another function, here is the code of a prototype:
getData <- function(query){
return (query)
}
r function oop
|
show 1 more comment
up vote
0
down vote
favorite
I'm newbie in R and I'm trying to pass an object to a function but I've got an error when I try to access to a slot of this object.
These are my main code:
x <- new("menu", competition=0, stats=0, query="")
y <- show(x)
show_graphics(y)
After second instruction I've got the object "y" created correctly like you can see in this screencap:
Then, I call to a the function "show_graphics" and pass this object "y" like a parameter. But when I try to access to an slot in the first insctruction of my function:
show_graphics <- function(menu){
data <- getData(menu@query)
......
}
I've got an error that tell me that there is an error to try to access
to the slot "query" of an object to the basic class "function" without
slots.
How can I pass correctly an object to a function? Or how can I get the values from an object passed to a function?
Am I doing something wrong?
Edit I:
This is the code to create the object "menu":
setClass("menu", slots=list(competition="numeric", stats="numeric", query="character"))
show <- function(object) 0
setGeneric("show")
setMethod("show", "menu", function(object){
object@competition <- 14
object@stats <- 1
object@query <- "query"
return (object)
})
Edit II:
getData is another function, here is the code of a prototype:
getData <- function(query){
return (query)
}
r function oop
1
I'm not able to re-produce. When I try to run thex <- new(..
line, I get the error:Error in getClass(Class, where = topenv(parent.frame())) : “menu” is not a defined class
. Are you able to include all relevant code, please?
– Jonny Phelps
Nov 19 at 12:52
I have edited my original post where I have added a prototype of the menu object
– José Carlos
Nov 19 at 13:22
Thank you. I'm still struggling unfortunately.y <- show(x)
gives me the errorError in show(x) : object 'competition' not found
&show_graphics(y)
gives me the errorError in getData(menu@query) : could not find function "getData"
. Might be worth making a simpler version of the problem that's fully re-producible
– Jonny Phelps
Nov 19 at 13:31
Sorry, I have forget to return the object. I have updated the code from Edit I and created an function prototype of getData in Edit II
– José Carlos
Nov 19 at 13:38
Thank you. It's weird, I don't get any error when runningshow_graphics(y)
now (if I remove the ......). Unrelated but you could try usingR6
classes for object orientated approach. It's well supported with good documentation: adv-r.hadley.nz/r6.html
– Jonny Phelps
Nov 19 at 15:07
|
show 1 more comment
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm newbie in R and I'm trying to pass an object to a function but I've got an error when I try to access to a slot of this object.
These are my main code:
x <- new("menu", competition=0, stats=0, query="")
y <- show(x)
show_graphics(y)
After second instruction I've got the object "y" created correctly like you can see in this screencap:
Then, I call to a the function "show_graphics" and pass this object "y" like a parameter. But when I try to access to an slot in the first insctruction of my function:
show_graphics <- function(menu){
data <- getData(menu@query)
......
}
I've got an error that tell me that there is an error to try to access
to the slot "query" of an object to the basic class "function" without
slots.
How can I pass correctly an object to a function? Or how can I get the values from an object passed to a function?
Am I doing something wrong?
Edit I:
This is the code to create the object "menu":
setClass("menu", slots=list(competition="numeric", stats="numeric", query="character"))
show <- function(object) 0
setGeneric("show")
setMethod("show", "menu", function(object){
object@competition <- 14
object@stats <- 1
object@query <- "query"
return (object)
})
Edit II:
getData is another function, here is the code of a prototype:
getData <- function(query){
return (query)
}
r function oop
I'm newbie in R and I'm trying to pass an object to a function but I've got an error when I try to access to a slot of this object.
These are my main code:
x <- new("menu", competition=0, stats=0, query="")
y <- show(x)
show_graphics(y)
After second instruction I've got the object "y" created correctly like you can see in this screencap:
Then, I call to a the function "show_graphics" and pass this object "y" like a parameter. But when I try to access to an slot in the first insctruction of my function:
show_graphics <- function(menu){
data <- getData(menu@query)
......
}
I've got an error that tell me that there is an error to try to access
to the slot "query" of an object to the basic class "function" without
slots.
How can I pass correctly an object to a function? Or how can I get the values from an object passed to a function?
Am I doing something wrong?
Edit I:
This is the code to create the object "menu":
setClass("menu", slots=list(competition="numeric", stats="numeric", query="character"))
show <- function(object) 0
setGeneric("show")
setMethod("show", "menu", function(object){
object@competition <- 14
object@stats <- 1
object@query <- "query"
return (object)
})
Edit II:
getData is another function, here is the code of a prototype:
getData <- function(query){
return (query)
}
r function oop
r function oop
edited Nov 19 at 13:51
asked Nov 19 at 12:42
José Carlos
66621844
66621844
1
I'm not able to re-produce. When I try to run thex <- new(..
line, I get the error:Error in getClass(Class, where = topenv(parent.frame())) : “menu” is not a defined class
. Are you able to include all relevant code, please?
– Jonny Phelps
Nov 19 at 12:52
I have edited my original post where I have added a prototype of the menu object
– José Carlos
Nov 19 at 13:22
Thank you. I'm still struggling unfortunately.y <- show(x)
gives me the errorError in show(x) : object 'competition' not found
&show_graphics(y)
gives me the errorError in getData(menu@query) : could not find function "getData"
. Might be worth making a simpler version of the problem that's fully re-producible
– Jonny Phelps
Nov 19 at 13:31
Sorry, I have forget to return the object. I have updated the code from Edit I and created an function prototype of getData in Edit II
– José Carlos
Nov 19 at 13:38
Thank you. It's weird, I don't get any error when runningshow_graphics(y)
now (if I remove the ......). Unrelated but you could try usingR6
classes for object orientated approach. It's well supported with good documentation: adv-r.hadley.nz/r6.html
– Jonny Phelps
Nov 19 at 15:07
|
show 1 more comment
1
I'm not able to re-produce. When I try to run thex <- new(..
line, I get the error:Error in getClass(Class, where = topenv(parent.frame())) : “menu” is not a defined class
. Are you able to include all relevant code, please?
– Jonny Phelps
Nov 19 at 12:52
I have edited my original post where I have added a prototype of the menu object
– José Carlos
Nov 19 at 13:22
Thank you. I'm still struggling unfortunately.y <- show(x)
gives me the errorError in show(x) : object 'competition' not found
&show_graphics(y)
gives me the errorError in getData(menu@query) : could not find function "getData"
. Might be worth making a simpler version of the problem that's fully re-producible
– Jonny Phelps
Nov 19 at 13:31
Sorry, I have forget to return the object. I have updated the code from Edit I and created an function prototype of getData in Edit II
– José Carlos
Nov 19 at 13:38
Thank you. It's weird, I don't get any error when runningshow_graphics(y)
now (if I remove the ......). Unrelated but you could try usingR6
classes for object orientated approach. It's well supported with good documentation: adv-r.hadley.nz/r6.html
– Jonny Phelps
Nov 19 at 15:07
1
1
I'm not able to re-produce. When I try to run the
x <- new(..
line, I get the error: Error in getClass(Class, where = topenv(parent.frame())) : “menu” is not a defined class
. Are you able to include all relevant code, please?– Jonny Phelps
Nov 19 at 12:52
I'm not able to re-produce. When I try to run the
x <- new(..
line, I get the error: Error in getClass(Class, where = topenv(parent.frame())) : “menu” is not a defined class
. Are you able to include all relevant code, please?– Jonny Phelps
Nov 19 at 12:52
I have edited my original post where I have added a prototype of the menu object
– José Carlos
Nov 19 at 13:22
I have edited my original post where I have added a prototype of the menu object
– José Carlos
Nov 19 at 13:22
Thank you. I'm still struggling unfortunately.
y <- show(x)
gives me the error Error in show(x) : object 'competition' not found
& show_graphics(y)
gives me the error Error in getData(menu@query) : could not find function "getData"
. Might be worth making a simpler version of the problem that's fully re-producible– Jonny Phelps
Nov 19 at 13:31
Thank you. I'm still struggling unfortunately.
y <- show(x)
gives me the error Error in show(x) : object 'competition' not found
& show_graphics(y)
gives me the error Error in getData(menu@query) : could not find function "getData"
. Might be worth making a simpler version of the problem that's fully re-producible– Jonny Phelps
Nov 19 at 13:31
Sorry, I have forget to return the object. I have updated the code from Edit I and created an function prototype of getData in Edit II
– José Carlos
Nov 19 at 13:38
Sorry, I have forget to return the object. I have updated the code from Edit I and created an function prototype of getData in Edit II
– José Carlos
Nov 19 at 13:38
Thank you. It's weird, I don't get any error when running
show_graphics(y)
now (if I remove the ......). Unrelated but you could try using R6
classes for object orientated approach. It's well supported with good documentation: adv-r.hadley.nz/r6.html– Jonny Phelps
Nov 19 at 15:07
Thank you. It's weird, I don't get any error when running
show_graphics(y)
now (if I remove the ......). Unrelated but you could try using R6
classes for object orientated approach. It's well supported with good documentation: adv-r.hadley.nz/r6.html– Jonny Phelps
Nov 19 at 15:07
|
show 1 more comment
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374897%2fr-how-to-pass-an-object-to-a-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
I'm not able to re-produce. When I try to run the
x <- new(..
line, I get the error:Error in getClass(Class, where = topenv(parent.frame())) : “menu” is not a defined class
. Are you able to include all relevant code, please?– Jonny Phelps
Nov 19 at 12:52
I have edited my original post where I have added a prototype of the menu object
– José Carlos
Nov 19 at 13:22
Thank you. I'm still struggling unfortunately.
y <- show(x)
gives me the errorError in show(x) : object 'competition' not found
&show_graphics(y)
gives me the errorError in getData(menu@query) : could not find function "getData"
. Might be worth making a simpler version of the problem that's fully re-producible– Jonny Phelps
Nov 19 at 13:31
Sorry, I have forget to return the object. I have updated the code from Edit I and created an function prototype of getData in Edit II
– José Carlos
Nov 19 at 13:38
Thank you. It's weird, I don't get any error when running
show_graphics(y)
now (if I remove the ......). Unrelated but you could try usingR6
classes for object orientated approach. It's well supported with good documentation: adv-r.hadley.nz/r6.html– Jonny Phelps
Nov 19 at 15:07