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













1















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.










share|improve this question
























  • 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






  • 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












  • 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















1















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.










share|improve this question
























  • 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






  • 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












  • 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













1












1








1








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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 at 0:43







Calum You

















asked Mar 8 at 0:01









Calum YouCalum You

8,0721930




8,0721930












  • 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






  • 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












  • 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

















  • 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






  • 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












  • 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
















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












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



);













draft saved

draft discarded


















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















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.




draft saved


draft discarded














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





















































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

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived