Re: [R] Controlling visibility of top-level functions
One option that I have not seen mentioned yet, and may be the most similar to the Python approach is to name the Invisible function starting with a . (the period). Then the function will not appear when you do `ls()` but is still accessible. It however is not limited in scope and it is still callable by anyone (and `ls(all=TRUE)` will show it). This does not give the control that packages or local environments do, but it is a quick and easy way to have something that hidden. On Fri, Jan 31, 2014 at 11:46 AM, Paul A. Steckler st...@stecksoft.com wrote: I'm fairly new to R, and have checked the R FAQ and done an RSiteSearch for help on this topic, to no avail. I want to write some R code that has functions at the top-level that are not visible when the code is loaded. So in fun1 - function(...) { ... } fun2 - function(...) { ... fun1 ...} I'd like fun2 to be callable, but have fun1 be invisible. That is, the scope of fun1 is limited to the file in which it's defined. In Python, I believe that prepending an underscore to a variable name limits its scope in this way. Is there a similar mechanism in R? -- Paul __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Gregory (Greg) L. Snow Ph.D. 538...@gmail.com __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Controlling visibility of top-level functions
I'm fairly new to R, and have checked the R FAQ and done an RSiteSearch for help on this topic, to no avail. I want to write some R code that has functions at the top-level that are not visible when the code is loaded. So in fun1 - function(...) { ... } fun2 - function(...) { ... fun1 ...} I'd like fun2 to be callable, but have fun1 be invisible. That is, the scope of fun1 is limited to the file in which it's defined. In Python, I believe that prepending an underscore to a variable name limits its scope in this way. Is there a similar mechanism in R? -- Paul __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Controlling visibility of top-level functions
On 31/01/2014 1:46 PM, Paul A. Steckler wrote: I'm fairly new to R, and have checked the R FAQ and done an RSiteSearch for help on this topic, to no avail. I want to write some R code that has functions at the top-level that are not visible when the code is loaded. So in fun1 - function(...) { ... } fun2 - function(...) { ... fun1 ...} I'd like fun2 to be callable, but have fun1 be invisible. That is, the scope of fun1 is limited to the file in which it's defined. In Python, I believe that prepending an underscore to a variable name limits its scope in this way. Is there a similar mechanism in R? There are a couple ways. The heavyweight way is to write a package that exports fun2 but not fun1. fun2 can see fun1, but the rest of the world can't. Scope isn't limited to one file, any function in the package can see it. The lightweight way is to define fun1 and fun2 in a local scope, e.g. fun3 - local({ fun1 - function(...) { ... } fun2 - function(...) { ... fun1 ...} fun2 }) This way fun1 and fun2 can see each other but nobody else can see them, and fun3 is a copy of fun2 that is visible in the workspace. You don't need a third name, I just changed it to make the explanation easier. Duncan Murodch __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Controlling visibility of top-level functions
On 01/02/14 08:13, Duncan Murdoch wrote: On 31/01/2014 1:46 PM, Paul A. Steckler wrote: I'm fairly new to R, and have checked the R FAQ and done an RSiteSearch for help on this topic, to no avail. I want to write some R code that has functions at the top-level that are not visible when the code is loaded. So in fun1 - function(...) { ... } fun2 - function(...) { ... fun1 ...} I'd like fun2 to be callable, but have fun1 be invisible. That is, the scope of fun1 is limited to the file in which it's defined. In Python, I believe that prepending an underscore to a variable name limits its scope in this way. Is there a similar mechanism in R? There are a couple ways. The heavyweight way is to write a package that exports fun2 but not fun1. fun2 can see fun1, but the rest of the world can't. Scope isn't limited to one file, any function in the package can see it. The lightweight way is to define fun1 and fun2 in a local scope, e.g. fun3 - local({ fun1 - function(...) { ... } fun2 - function(...) { ... fun1 ...} fun2 }) This way fun1 and fun2 can see each other but nobody else can see them, and fun3 is a copy of fun2 that is visible in the workspace. You don't need a third name, I just changed it to make the explanation easier. Would it not be more perspicuous to do: fun2 - local({ fun1 - function(...) { ... } function(...) { ... fun1 ...} }) Having fun3 kicking around seems to be an effect brought to you by the Department of Redundancy Department. cheers, Rolf __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Controlling visibility of top-level functions
Thanks for the suggestions. I'm going to need to create an R package, eventually, so I'll use that mechanism to control visibility. -- Paul On Fri, Jan 31, 2014 at 11:13 AM, Duncan Murdoch murdoch.dun...@gmail.com wrote: On 31/01/2014 1:46 PM, Paul A. Steckler wrote: I'm fairly new to R, and have checked the R FAQ and done an RSiteSearch for help on this topic, to no avail. I want to write some R code that has functions at the top-level that are not visible when the code is loaded. So in fun1 - function(...) { ... } fun2 - function(...) { ... fun1 ...} I'd like fun2 to be callable, but have fun1 be invisible. That is, the scope of fun1 is limited to the file in which it's defined. In Python, I believe that prepending an underscore to a variable name limits its scope in this way. Is there a similar mechanism in R? There are a couple ways. The heavyweight way is to write a package that exports fun2 but not fun1. fun2 can see fun1, but the rest of the world can't. Scope isn't limited to one file, any function in the package can see it. The lightweight way is to define fun1 and fun2 in a local scope, e.g. fun3 - local({ fun1 - function(...) { ... } fun2 - function(...) { ... fun1 ...} fun2 }) This way fun1 and fun2 can see each other but nobody else can see them, and fun3 is a copy of fun2 that is visible in the workspace. You don't need a third name, I just changed it to make the explanation easier. Duncan Murodch __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Controlling visibility of top-level functions
I think you will find that using namespaces and export directives are a better way of controlling visibility with packages. That is their intended purpose. Cheers, Bert Bert Gunter Genentech Nonclinical Biostatistics (650) 467-7374 Data is not information. Information is not knowledge. And knowledge is certainly not wisdom. H. Gilbert Welch On Fri, Jan 31, 2014 at 2:28 PM, Paul A. Steckler st...@stecksoft.com wrote: Thanks for the suggestions. I'm going to need to create an R package, eventually, so I'll use that mechanism to control visibility. -- Paul On Fri, Jan 31, 2014 at 11:13 AM, Duncan Murdoch murdoch.dun...@gmail.com wrote: On 31/01/2014 1:46 PM, Paul A. Steckler wrote: I'm fairly new to R, and have checked the R FAQ and done an RSiteSearch for help on this topic, to no avail. I want to write some R code that has functions at the top-level that are not visible when the code is loaded. So in fun1 - function(...) { ... } fun2 - function(...) { ... fun1 ...} I'd like fun2 to be callable, but have fun1 be invisible. That is, the scope of fun1 is limited to the file in which it's defined. In Python, I believe that prepending an underscore to a variable name limits its scope in this way. Is there a similar mechanism in R? There are a couple ways. The heavyweight way is to write a package that exports fun2 but not fun1. fun2 can see fun1, but the rest of the world can't. Scope isn't limited to one file, any function in the package can see it. The lightweight way is to define fun1 and fun2 in a local scope, e.g. fun3 - local({ fun1 - function(...) { ... } fun2 - function(...) { ... fun1 ...} fun2 }) This way fun1 and fun2 can see each other but nobody else can see them, and fun3 is a copy of fun2 that is visible in the workspace. You don't need a third name, I just changed it to make the explanation easier. Duncan Murodch __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Controlling visibility of top-level functions
On 14-01-31 5:09 PM, Rolf Turner wrote: On 01/02/14 08:13, Duncan Murdoch wrote: On 31/01/2014 1:46 PM, Paul A. Steckler wrote: I'm fairly new to R, and have checked the R FAQ and done an RSiteSearch for help on this topic, to no avail. I want to write some R code that has functions at the top-level that are not visible when the code is loaded. So in fun1 - function(...) { ... } fun2 - function(...) { ... fun1 ...} I'd like fun2 to be callable, but have fun1 be invisible. That is, the scope of fun1 is limited to the file in which it's defined. In Python, I believe that prepending an underscore to a variable name limits its scope in this way. Is there a similar mechanism in R? There are a couple ways. The heavyweight way is to write a package that exports fun2 but not fun1. fun2 can see fun1, but the rest of the world can't. Scope isn't limited to one file, any function in the package can see it. The lightweight way is to define fun1 and fun2 in a local scope, e.g. fun3 - local({ fun1 - function(...) { ... } fun2 - function(...) { ... fun1 ...} fun2 }) This way fun1 and fun2 can see each other but nobody else can see them, and fun3 is a copy of fun2 that is visible in the workspace. You don't need a third name, I just changed it to make the explanation easier. Would it not be more perspicuous to do: fun2 - local({ fun1 - function(...) { ... } function(...) { ... fun1 ...} }) Having fun3 kicking around seems to be an effect brought to you by the Department of Redundancy Department. Sure, that's how I'd write it, but then it's harder to figure out how to say fun1 and fun2 can see each other. (Of course, fun1 and fun2 can see fun3 in my version, but in a different way, and not necessarily in a more complicated example.) Duncan __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.