I have a list containing 18 elements called bx2.
I want to use bx2 in a function,
XlsMaker <- function(x) {
library("XLConnect")
a <- length(x)
b <- paste0(x,".xlsx")
for (i in 1:a){
writeWorksheetToFile(b, data = x[[i]], sheet = names(x[i]))
}
}
but when put I bx2 into the function it pulls in all the elements of the list rather than just the name of the list.
Is it possible to re-write the function so that b becomes bx2.xlsx?
The line b <- paste0(x,".xlsx") is wrong. That calls paste0 on the object x itself which is not at all what you want to do. You want to call it on the name of the object.
This in general opens a can of worms because objects can have two different names in two different places. Consider: the object named bx2 in your global environment is now named x within the function's scope. If you only want to call this function from the top level (e.g. for interactive use), you can safely get the name of the object from the parent environment (the environment you called the function from) by replacing that line with:
x_name <- deparse(substitute(x))
b <- paste0(x_name, ".xlsx")
The substitute function gets the name of x in the parent environment, as a special name object. The deparse function converts this name into a character vector of length one.
The reason I said this is only safe to use at the top level is that substitute can return surprising or unintended results if not used carefully. Hadley Wickham goes into detail on this point in his book.
I think you just want to deparse the parameter name
XlsMaker <- function(x) {
varname <- deparse(substitute(x))
library("XLConnect")
a <- length(x)
b <- paste0(varname ,".xlsx")
for (i in 1:a){
writeWorksheetToFile(b, data = x[[i]], sheet = names(x[i]))
}
}
bx2 <-list(1:3, 4:6)
XlsMaker(bx2)
Related
In R, we can reference items created within that same list, i.e.:
list(a = a <- 1, b = a)
I am curious if there is a way to write a function which takes the place of a = a <- 1. That is, if something like
`%=%` <- function(x,y) {
envir <- environment()
char_x <- deparse(substitute(x))
assign(char_x, y, parent.env(envir))
unlist(lapply(setNames(seq_along(x),char_x), function(T) y))
}
# does not work
list(a%=%1, b=a)
is possible in R (i.e. returns the list given above)?
edit: I think this boils down to asking, 'can we call list with a language object that preserves all aspects of manually coding list?' (specifically, assigns the list's names attribute the left-hand side of the language element).
It seems to me that below shows that such a solution is hopeless.
my_call <- do.call(substitute, list(expr(expr = {x = y}), list(x=quote(a), y=1)))
equals <- languageEl(my_call, which = 1)
str(equals)
do.call(list, list(equals))
Welp, the clever folk behind tibble have figured this out in their lst() function (also in package dplyr)
library(dplyr)
lst(a=1, b=a, c=c(3,4), d=c)
What a useful feature!
Trying to pass into a function what I want it to name the dataframe it creates, then save it to global environment.
I am trying to automate creating dataframes that are subsets of other dataframes by filtering for a value; since I'm creating 43 of these, I'm writing a function that can automatically:
a) subset rows containing a certain string into it's own data.frame then
b) name a dataframe after that string and save it to my global environment. (The string in a) is also the suffix I want it to name the data.frame after in b))
I can do a) fine but am having trouble with b).
Say I have a dataset which includes a column named "Team" (detailing whose team that member belongs to):
original.df <- read_csv("../original_data_set")
I create a function to split that dataset according to values in one of its columns...
split.function <- function(string){
x <- original.df
as.name(string) <<- filter(x, str_detect(`Team`, string))
}
... then save the dataframe with the name:
split.by.candidate('Team.Curt')
I keep getting:
> Error in as.name(x) <<- filter(y, str_detect(`Receiving Committee`, x)) :
object 'x' not found
But I just want to see Team.Curt saved as a data.frame in my global environment when I do this with rows including the term Team.Curt
You can use assign to create objects based on a string:
split.function <- function(string){
x <- original.df
assign(string, filter(x, str_detect(`Team`, string)), envir = .GlobalEnv)
}
Here, envir = .GlobalEnv is used to assign the value to the global environment.
Both <- and <<- assignments require that the statement hardcodes the object name. Since you want to parameterize the name, as in your cases, you must use assign().
<<- is merely a variant of <- that can be used inside a function, and does a bottom-up search of environments until it either reaches the top (.GlobalEnv) or finds an existing object of that name. In your case that's unnecessary and slightly dangerous, since if an object of that name existed in some environment halfway up the hierarchy, you'd pick it up and assign to it instead.
So just use assign(..., envir = .GlobalEnv) instead.
But both <<- or assigning directly into .GlobalEnv within functions are strongly discouraged as being disasters in waiting, or "life by a volcano" (burns-stat.com/pages/Tutor/R_inferno.pdf). See the caveats at Assign multiple objects to .GlobalEnv from within a function. tidyverse is probably a better approach for managing multiple dataframes.
To create a more compact script, I am trying to create my first function.
The general function is:
f.mean <- function(var, fig, datafile){
require(lme4)
change <- as.symbol(paste("change", var, sep=""))
base <- as.symbol(paste("baseline", var, sep = ""))
x <- substitute(lmer(change ~ base + (1|ID), data=datafile))
out<-eval(x)
name <- paste(fig,".", var, sep="")
as.symbol(name) <- out
}
}
The purpose of this function is to input var, fig and datafile and to output a new variable named fig.var containing out (eval of LMER).
Apparently it is difficult to 'change' the variable name on the left side of the <-.
What we have tried so far:
- assign(name, out)
- as.symbol(name) <<- out
- makeActive Binding("y",function() x, .GlobalEnv)
- several rename options to rename out to the specified var name
Can someone help me to assign the out value to this 'run' specific variable name? All other suggestions are welcome as well.
As #Roland comments, in R (or any) programming one should avoid indirect environment manipulators such as assign, attach, list2env, <<-, and others which are difficult to debug and break the flow of usual programming involving explicitly defined objects and methods.
Additionally, avoid flooding your global environment of potentially hundreds or thousands of similarly structured objects that may require environment mining such as ls, mget, or eapply. Simply, use one large container like a list of named elements which is more manageable and makes code more maintainable.
Specifically, be direct in assigning objects and pass string literals (var, fig) or objects (datafile) as function parameters and have function return values. And for many inputs, build lists with lapply or Map (wrapper to mapply) to retain needed objects. Consider below adjustment that builds a formula from string literals and passes into your model with results to be returned at end.
f.mean <- function(var, fig, datafile){
require(lme4)
myformula <- as.formula(paste0("change", var, " ~ baseline", var, " + (1|ID)"))
x <- lmer(myformula, data=datafile)
return(x)
}
var_list <- # ... list/vector of var character literals
fig_list <- # ... list/vector of fig character literals
# BUILD AND NAME LIST OF LMER OUTPUTS
lmer_list <- setNames(Map(f.mean, var_list, fig_list, MorArgs=df),
paste0(fig_list, ".", var_list))
# IDENTIFY NEEDED var*fig* BY ELEMENT NAME OF LARGER CONTAINER
lmer_list$fig1.var1
lmer_list$fig2.var2
lmer_list$fig3.var3
I have a function in R that structures my raw data. I create a dataframe called output and then want to make a dynamic variable name depending on the function value block.
The output object does contain a dataframe as I want, and to rename it dynamically, at the end of the function I do this (within the function):
a = assign(paste("output", block, sep=""), output)
... but after running the function there is no object output1 (if block = 1). I simply cannot retrieve the output object, neither merely output nor the dynamic output1 version.
I tried this then:
a = assign(paste("output", block, sep=""), output)
return(a)
... but still - no success.
How can I retrieve the dynamic output variable? Where is my mistake?
Environments.
assign will by default create a variable in the environment in which it's called. Read about environments here: http://adv-r.had.co.nz/Environments.html
I assume you're doing something like:
foo <- function(x){ assign("b", x); b}
If you run foo(5), you'll see it returns 5 as expected (implying that b was created successfully somewhere), but b won't exist in your current environment.
If, however, you do something like this
foo <- function(x){ assign("b", x, envir=parent.frame()); b}
Here, you're assigning not to the current environment at the time assign is called (which happens to be foo's environment). Instead, you're assigning into the parent environment (which, since you're calling this function directly, will be your environment).
All this complexity should reveal to you that this will be fairly complex, a nightmare to maintain, and a really bad idea from a maintenance perspective. You'd surely be better off with something like:
foo <- function(x) { return(x) };
b <- foo(5)
Or if you need multiple items returned:
foo <- function(x) { return(list(df=data.frame(col1=x), b=x)) }
results <- foo(5)
df <- results$df
b <- results$b
But ours is not to reason why...
I've defined a list of anonymous functions which use a variable defined in an outer scope.
funclist <- list()
for(i in 1:5)
{
funclist[[i]] <- function(x) print(i)
}
funclist[[1]]('foo')
The output is:
[1] 5
It seems that i is captured by reference. I'd like it to be captured by value, i.e. the output should be
[1] 1
Is there a way to tell R to capture i by value rather than by reference?
When you run a for loop, this creates a variable in the environment the loop is run in, and functions created in the loop are also run from this environment. So whenever you run the functions created in this way that use the index value from the loop, they only have access to the final value, and only as long as that varaible remains (try rm(i) and attempting to fun one of the functions in the list).
What you need to do is bind the index value to the function in their own environment. lapply will do this for you automatically. However, there is a gotcha with lazy evaluation. What you have to do is also force the evaluation of i before creating the anonymous function:
funclist <- lapply(1:5, function(i) {force(i); function(x) print(i)})
funclist[[1]]('foo')
[1] 1
funclist[[5]]('foo')
[1] 5
My read on what you want is to store a value inside the function's environment when the function is defined, and then hold onto that value for internal computations.
For that, you want a closure:
i <- 3
test <- local({
i <- i
function(x) x[i]
})
test(letters[1:5]) # returns 'c'
i <- 5
test(letters[1:5]) # still returns 'c' (i.e. i is local to the test closure)
Is that what you wanted?