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:



enter image description here



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)
}









share|improve this question




















  • 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 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










  • 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















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:



enter image description here



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)
}









share|improve this question




















  • 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 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










  • 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













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:



enter image description here



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)
}









share|improve this question















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:



enter image description here



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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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 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










  • 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














  • 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 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










  • 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








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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

To store a contact into the json file from server.js file using a class in NodeJS

Redirect URL with Chrome Remote Debugging Android Devices

Dieringhausen