How can I put the output of a function factory in an R package if it relies on input not in its arguments?R specify function environmentHow can I view the source code for a function?How should I organize R package code and documentation when method functions share common arguments?Method dispatch for functions inside dplyr::doCreating a reactive and memoizable function outside of shiny contextShiny: add regression line without changing dataHow to delay evaluation of function passed as argument to purrr::pmapFormat error message avoiding newlines in RmarkdownReplace one symbol in an expression with multiple valuesQuasiquotation and ifelse : Unquoting not resolving as expectedEvaluate expression with in-function variable calculation
awk assign to multiple variables at once
Why do some congregations only make noise at certain occasions of Haman?
Delete multiple columns using awk or sed
Which was the first story featuring espers?
The Digit Triangles
Pre-mixing cryogenic fuels and using only one fuel tank
Which Article Helped Get Rid of Technobabble in RPGs?
How to draw a matrix with arrows in limited space
Has the laser at Magurele, Romania reached a tenth of the Sun's power?
Plot of a tornado shape like surface
Why is the Sun approximated as a black body at ~ 5800 K?
Is there a way to have vectors outlined in a Vector Plot?
Creating two special characters
Taxes on Dividends in a Roth IRA
Is it allowed to activate the ability of multiple planeswalkers in a single turn?
Make a Bowl of Alphabet Soup
Review your own paper in Mathematics
Why is so much work done on numerical verification of the Riemann Hypothesis?
What's the name of the logical fallacy where a debater extends a statement far beyond the original statement to make it true?
How can ping know if my host is down
Can I say "fingers" when referring to toes?
Showing a sum is positive
Strong empirical falsification of quantum mechanics based on vacuum energy density?
Shouldn’t conservatives embrace universal basic income?
How can I put the output of a function factory in an R package if it relies on input not in its arguments?
R specify function environmentHow can I view the source code for a function?How should I organize R package code and documentation when method functions share common arguments?Method dispatch for functions inside dplyr::doCreating a reactive and memoizable function outside of shiny contextShiny: add regression line without changing dataHow to delay evaluation of function passed as argument to purrr::pmapFormat error message avoiding newlines in RmarkdownReplace one symbol in an expression with multiple valuesQuasiquotation and ifelse : Unquoting not resolving as expectedEvaluate expression with in-function variable calculation
I am using a function factory defined by someone else and I cannot change it. It is common to need to generate several functions with this factory at the beginning of every run. In my attempt at a toy example, it's as if I need many such power_
functions frequently. Currently as a user of otherpackage
, I must include lines like power2 <- power_factory(2)
in my scripts.
otherpackage::power_factory <- function(exp)
function(x)
x ^ exp
power2 <- power_factory(2)
power3 <- power_factory(3)
I would like to avoid these lines by writing my own package, so I can instead use mypackage::power2()
in my scripts. Normally, if I wanted to wrap these functions in a package, I could just use the factory to build them inside the package. However, this factory depends on some outside input, here credential
, that I cannot write inside my package. This makes the factory error if I put power2 <- power_factory(2)
inside the package:
# credential <- "my_secret"
otherpackage::power_factory <- function(exp)
print(credential)
function(x)
x ^ exp
power2 <- power_factory(2)
#> Error in print(credential): object 'credential' not found
Created on 2019-03-07 by the reprex package (v0.2.1)
Is there a way around this problem? The end goal, as above, is to be able to call mypackage::power2()
instead of needing to do power2 <- power_factory(2)
and many variations on such at the start of each script.
r
add a comment |
I am using a function factory defined by someone else and I cannot change it. It is common to need to generate several functions with this factory at the beginning of every run. In my attempt at a toy example, it's as if I need many such power_
functions frequently. Currently as a user of otherpackage
, I must include lines like power2 <- power_factory(2)
in my scripts.
otherpackage::power_factory <- function(exp)
function(x)
x ^ exp
power2 <- power_factory(2)
power3 <- power_factory(3)
I would like to avoid these lines by writing my own package, so I can instead use mypackage::power2()
in my scripts. Normally, if I wanted to wrap these functions in a package, I could just use the factory to build them inside the package. However, this factory depends on some outside input, here credential
, that I cannot write inside my package. This makes the factory error if I put power2 <- power_factory(2)
inside the package:
# credential <- "my_secret"
otherpackage::power_factory <- function(exp)
print(credential)
function(x)
x ^ exp
power2 <- power_factory(2)
#> Error in print(credential): object 'credential' not found
Created on 2019-03-07 by the reprex package (v0.2.1)
Is there a way around this problem? The end goal, as above, is to be able to call mypackage::power2()
instead of needing to do power2 <- power_factory(2)
and many variations on such at the start of each script.
r
When you loadmypackage
, the namespace is typically locked once loaded, so the user later wanting to definepower2
does not necessarily work. There are cheats around this, but I have yet to hear somebody recommend solid rationale why this should be allowed and easy. Is there a reason you cannot make it an argument of the factory, as inpower_factory(2, credential)
?
– r2evans
Mar 8 at 0:38
The reason is that I cannot editpower_factory
since it comes from a different, non-public package (and I can't speak to the reasons for the author choosing to make the interface like this). I am not sure what you mean by the "user later wanting to definepower2
", I'll add some clarifying text
– Calum You
Mar 8 at 0:41
2
Where and when iscredential
defined? You can write your own function factory as a wrapper around the other factory, thereby safe-guarding acquisition of the credential, such asmyfactory <- function(...) credential <- get_credential(); otherpackage::power_factor(...);
– r2evans
Mar 8 at 2:25
You can achieve this by creating an environmentenv
with the credential and then setting thepower_factor
function environment toenv
. See the McFlick's answer here: stackoverflow.com/questions/12279076/…. In particular the helper functionwith_env
– dipetkov
Mar 8 at 2:56
I was able to get the wrapper working r2evans, will accept if you submit as answer
– Calum You
Mar 14 at 20:57
add a comment |
I am using a function factory defined by someone else and I cannot change it. It is common to need to generate several functions with this factory at the beginning of every run. In my attempt at a toy example, it's as if I need many such power_
functions frequently. Currently as a user of otherpackage
, I must include lines like power2 <- power_factory(2)
in my scripts.
otherpackage::power_factory <- function(exp)
function(x)
x ^ exp
power2 <- power_factory(2)
power3 <- power_factory(3)
I would like to avoid these lines by writing my own package, so I can instead use mypackage::power2()
in my scripts. Normally, if I wanted to wrap these functions in a package, I could just use the factory to build them inside the package. However, this factory depends on some outside input, here credential
, that I cannot write inside my package. This makes the factory error if I put power2 <- power_factory(2)
inside the package:
# credential <- "my_secret"
otherpackage::power_factory <- function(exp)
print(credential)
function(x)
x ^ exp
power2 <- power_factory(2)
#> Error in print(credential): object 'credential' not found
Created on 2019-03-07 by the reprex package (v0.2.1)
Is there a way around this problem? The end goal, as above, is to be able to call mypackage::power2()
instead of needing to do power2 <- power_factory(2)
and many variations on such at the start of each script.
r
I am using a function factory defined by someone else and I cannot change it. It is common to need to generate several functions with this factory at the beginning of every run. In my attempt at a toy example, it's as if I need many such power_
functions frequently. Currently as a user of otherpackage
, I must include lines like power2 <- power_factory(2)
in my scripts.
otherpackage::power_factory <- function(exp)
function(x)
x ^ exp
power2 <- power_factory(2)
power3 <- power_factory(3)
I would like to avoid these lines by writing my own package, so I can instead use mypackage::power2()
in my scripts. Normally, if I wanted to wrap these functions in a package, I could just use the factory to build them inside the package. However, this factory depends on some outside input, here credential
, that I cannot write inside my package. This makes the factory error if I put power2 <- power_factory(2)
inside the package:
# credential <- "my_secret"
otherpackage::power_factory <- function(exp)
print(credential)
function(x)
x ^ exp
power2 <- power_factory(2)
#> Error in print(credential): object 'credential' not found
Created on 2019-03-07 by the reprex package (v0.2.1)
Is there a way around this problem? The end goal, as above, is to be able to call mypackage::power2()
instead of needing to do power2 <- power_factory(2)
and many variations on such at the start of each script.
r
r
edited Mar 8 at 0:43
Calum You
asked Mar 8 at 0:01
Calum YouCalum You
8,0721930
8,0721930
When you loadmypackage
, the namespace is typically locked once loaded, so the user later wanting to definepower2
does not necessarily work. There are cheats around this, but I have yet to hear somebody recommend solid rationale why this should be allowed and easy. Is there a reason you cannot make it an argument of the factory, as inpower_factory(2, credential)
?
– r2evans
Mar 8 at 0:38
The reason is that I cannot editpower_factory
since it comes from a different, non-public package (and I can't speak to the reasons for the author choosing to make the interface like this). I am not sure what you mean by the "user later wanting to definepower2
", I'll add some clarifying text
– Calum You
Mar 8 at 0:41
2
Where and when iscredential
defined? You can write your own function factory as a wrapper around the other factory, thereby safe-guarding acquisition of the credential, such asmyfactory <- function(...) credential <- get_credential(); otherpackage::power_factor(...);
– r2evans
Mar 8 at 2:25
You can achieve this by creating an environmentenv
with the credential and then setting thepower_factor
function environment toenv
. See the McFlick's answer here: stackoverflow.com/questions/12279076/…. In particular the helper functionwith_env
– dipetkov
Mar 8 at 2:56
I was able to get the wrapper working r2evans, will accept if you submit as answer
– Calum You
Mar 14 at 20:57
add a comment |
When you loadmypackage
, the namespace is typically locked once loaded, so the user later wanting to definepower2
does not necessarily work. There are cheats around this, but I have yet to hear somebody recommend solid rationale why this should be allowed and easy. Is there a reason you cannot make it an argument of the factory, as inpower_factory(2, credential)
?
– r2evans
Mar 8 at 0:38
The reason is that I cannot editpower_factory
since it comes from a different, non-public package (and I can't speak to the reasons for the author choosing to make the interface like this). I am not sure what you mean by the "user later wanting to definepower2
", I'll add some clarifying text
– Calum You
Mar 8 at 0:41
2
Where and when iscredential
defined? You can write your own function factory as a wrapper around the other factory, thereby safe-guarding acquisition of the credential, such asmyfactory <- function(...) credential <- get_credential(); otherpackage::power_factor(...);
– r2evans
Mar 8 at 2:25
You can achieve this by creating an environmentenv
with the credential and then setting thepower_factor
function environment toenv
. See the McFlick's answer here: stackoverflow.com/questions/12279076/…. In particular the helper functionwith_env
– dipetkov
Mar 8 at 2:56
I was able to get the wrapper working r2evans, will accept if you submit as answer
– Calum You
Mar 14 at 20:57
When you load
mypackage
, the namespace is typically locked once loaded, so the user later wanting to define power2
does not necessarily work. There are cheats around this, but I have yet to hear somebody recommend solid rationale why this should be allowed and easy. Is there a reason you cannot make it an argument of the factory, as in power_factory(2, credential)
?– r2evans
Mar 8 at 0:38
When you load
mypackage
, the namespace is typically locked once loaded, so the user later wanting to define power2
does not necessarily work. There are cheats around this, but I have yet to hear somebody recommend solid rationale why this should be allowed and easy. Is there a reason you cannot make it an argument of the factory, as in power_factory(2, credential)
?– r2evans
Mar 8 at 0:38
The reason is that I cannot edit
power_factory
since it comes from a different, non-public package (and I can't speak to the reasons for the author choosing to make the interface like this). I am not sure what you mean by the "user later wanting to define power2
", I'll add some clarifying text– Calum You
Mar 8 at 0:41
The reason is that I cannot edit
power_factory
since it comes from a different, non-public package (and I can't speak to the reasons for the author choosing to make the interface like this). I am not sure what you mean by the "user later wanting to define power2
", I'll add some clarifying text– Calum You
Mar 8 at 0:41
2
2
Where and when is
credential
defined? You can write your own function factory as a wrapper around the other factory, thereby safe-guarding acquisition of the credential, such as myfactory <- function(...) credential <- get_credential(); otherpackage::power_factor(...);
– r2evans
Mar 8 at 2:25
Where and when is
credential
defined? You can write your own function factory as a wrapper around the other factory, thereby safe-guarding acquisition of the credential, such as myfactory <- function(...) credential <- get_credential(); otherpackage::power_factor(...);
– r2evans
Mar 8 at 2:25
You can achieve this by creating an environment
env
with the credential and then setting the power_factor
function environment to env
. See the McFlick's answer here: stackoverflow.com/questions/12279076/…. In particular the helper function with_env
– dipetkov
Mar 8 at 2:56
You can achieve this by creating an environment
env
with the credential and then setting the power_factor
function environment to env
. See the McFlick's answer here: stackoverflow.com/questions/12279076/…. In particular the helper function with_env
– dipetkov
Mar 8 at 2:56
I was able to get the wrapper working r2evans, will accept if you submit as answer
– Calum You
Mar 14 at 20:57
I was able to get the wrapper working r2evans, will accept if you submit as answer
– Calum You
Mar 14 at 20:57
add a comment |
0
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
);
);
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%2f55054757%2fhow-can-i-put-the-output-of-a-function-factory-in-an-r-package-if-it-relies-on-i%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
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%2f55054757%2fhow-can-i-put-the-output-of-a-function-factory-in-an-r-package-if-it-relies-on-i%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
When you load
mypackage
, the namespace is typically locked once loaded, so the user later wanting to definepower2
does not necessarily work. There are cheats around this, but I have yet to hear somebody recommend solid rationale why this should be allowed and easy. Is there a reason you cannot make it an argument of the factory, as inpower_factory(2, credential)
?– r2evans
Mar 8 at 0:38
The reason is that I cannot edit
power_factory
since it comes from a different, non-public package (and I can't speak to the reasons for the author choosing to make the interface like this). I am not sure what you mean by the "user later wanting to definepower2
", I'll add some clarifying text– Calum You
Mar 8 at 0:41
2
Where and when is
credential
defined? You can write your own function factory as a wrapper around the other factory, thereby safe-guarding acquisition of the credential, such asmyfactory <- function(...) credential <- get_credential(); otherpackage::power_factor(...);
– r2evans
Mar 8 at 2:25
You can achieve this by creating an environment
env
with the credential and then setting thepower_factor
function environment toenv
. See the McFlick's answer here: stackoverflow.com/questions/12279076/…. In particular the helper functionwith_env
– dipetkov
Mar 8 at 2:56
I was able to get the wrapper working r2evans, will accept if you submit as answer
– Calum You
Mar 14 at 20:57