R: How to pass an object to a function?












0














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 '18 at 12:52










  • I have edited my original post where I have added a prototype of the menu object
    – José Carlos
    Nov 19 '18 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 '18 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 '18 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 '18 at 15:07
















0














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 '18 at 12:52










  • I have edited my original post where I have added a prototype of the menu object
    – José Carlos
    Nov 19 '18 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 '18 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 '18 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 '18 at 15:07














0












0








0







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 '18 at 13:51

























asked Nov 19 '18 at 12:42









José Carlos

68321944




68321944








  • 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 '18 at 12:52










  • I have edited my original post where I have added a prototype of the menu object
    – José Carlos
    Nov 19 '18 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 '18 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 '18 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 '18 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 '18 at 12:52










  • I have edited my original post where I have added a prototype of the menu object
    – José Carlos
    Nov 19 '18 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 '18 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 '18 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 '18 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 '18 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 '18 at 12:52












I have edited my original post where I have added a prototype of the menu object
– José Carlos
Nov 19 '18 at 13:22




I have edited my original post where I have added a prototype of the menu object
– José Carlos
Nov 19 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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',
autoActivateHeartbeat: false,
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




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




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

'app-layout' is not a known element: how to share Component with different Modules

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

WPF add header to Image with URL pettitions [duplicate]