Questions tagged [r-environment]
57 questions
24
votes
5 answers
Source script to separate environment in R, not the global environment
Is there a way to source() a script in R such that it is attached as a parent to the global environment (.GlobalEnv)?
Currently, when I source a script, all variables and functions of that script appear in my global (interactive) environment. I'd…

Megatron
- 15,909
- 12
- 89
- 97
16
votes
1 answer
Use `rmarkdown::render` in a restricted environment
I have the following Rmd file I called test.Rmd:
---
title: "test"
output: html_document
---
```{r}
print(y)
```
```{r}
x <- "don't you ignore me!"
print(x)
```
I want to call render the following way:
render('test.Rmd', output_format =…

moodymudskipper
- 46,417
- 11
- 121
- 167
8
votes
4 answers
Is it possible to have `dput` return source code that would run outside of the enclosing environment?
Suppose I have a closure add_y(y) which returns a function that adds y to its input.
add_y <- function(y) {
function(x) {
x + y
}
}
add_4 <- add_y(4)
So the value of add_4 is a function that adds 4 to its input. This works. I would like to…

crf
- 1,810
- 3
- 15
- 23
6
votes
0 answers
Lock environment but not .Random.seed
Is it possible to lock the global environment and still allow .Random.seed to be set or removed? The default behavior of lockEnvironment() is too aggressive
for my use case.
lockEnvironment(globalenv())
rnorm(10)
#> Error in rnorm(10) : cannot add…

landau
- 5,636
- 1
- 22
- 50
5
votes
1 answer
How to update the version of R used within a project, using renv
I have a project that used R 3.6, I have upgraded R to 4.0.2 and would like to use 4.0.2 for this project. I'm wondering how to go about doing so, or should I completely delete renv/ and rebuild?
Edit
I have done the following:
> renv::init()
This…

baxx
- 3,956
- 6
- 37
- 75
5
votes
1 answer
Return list vs environment from an R function
What advantage/disadvantage is there for using one over other in the following two cases? Case-I is returning its output as an environment and Case-II is returning its output as a list.
Case I:
function(x) {
ret <- new.env()
ret$x <- x
ret$y…

TheRimalaya
- 4,232
- 2
- 31
- 37
4
votes
1 answer
R's S3 method dispatching does not work when sourcing package
I am currently developing a package in R with the "help" of devtools. That is, the package is getting loaded into R studio via load_all(path = ...) in this stage and my functions from the R directory are made available. So far so good.
But when I…

gero
- 119
- 11
3
votes
1 answer
Expression can't find object inside a function
The code below works fine if ran outside the function - everything is being evaluated correctly, and the comparison cloud can be converted to a ggplot. However, when I want to run this as a function, the expression can no longer find the variables…

Stefan Musch
- 73
- 3
3
votes
4 answers
Is it possible to move a variable from the global environment into a separate environment?
Is it possible to move variables that reside in the global environment into a separate environment to declutter the global namespace? I understand how to create variables in a separate environment (with(env, ...)) but is there an efficient way to…

kamiks
- 184
- 7
3
votes
3 answers
Separate scripts from .GlobalEnv: Source script that source scripts
This question is similar to Source script to separate environment in R, not the global environment, but with a key twist.
Consider a script that sources another script:
# main.R
source("funs.R")
x <- 1
# funs.R
hello <- function()…

Guilherme Salomé
- 1,899
- 4
- 19
- 39
3
votes
2 answers
Partialised glue function does not work when called in different environment
I've partialised the glue function in a project I'm working on so that we can use agreed-on delimiters without having to tell glue about them all the time. But when we go to use the partialised function inside another function, it stops…

jimjamslam
- 1,988
- 1
- 18
- 32
3
votes
1 answer
How to `list2env()` (or pipe into `assign()`) within a function call?
I have a use-case for mapping a function to a vector and then assigning the results to individual objects in the parent environment - not the global environment, but the environment that the map() was called from. Specifically, this is all happening…

DHW
- 1,157
- 1
- 9
- 24
3
votes
1 answer
how to reorder the search path?
I want to attach an environment (package or other) to position 2 and I want it to stay there.
I can use library with pos=3 to ensure this most of the time, but I have issues with tidyverse :
search()
# [1] ".GlobalEnv" "tools:rstudio" …

moodymudskipper
- 46,417
- 11
- 121
- 167
3
votes
1 answer
Is it possible to create a stateful function with a single call in R?
I know that I can create a stateful function adder using the factory function adder_maker per the below:
adder_maker <- function() {x <- 0; function() {x <<- x+1; x}}
adder1 <- adder_maker()
adder1()
adder1()
environment(adder1)
The function…

logworthy
- 1,218
- 2
- 11
- 23
3
votes
1 answer
call return from child frame
How can I return a value in a function, through another function, see example here :
first_try <- function() eval(return(1),parent.frame())
second_try <- function() source(textConnection("return(2)"),parent.frame())
fun1 <- function(x){
…

moodymudskipper
- 46,417
- 11
- 121
- 167