Skip to content
Prev 319675 / 398502 Next

Question on assignment

Hello,

Maybe using ?parent.frame. Something like


fn2 <- function(x) {
	x1 <- function(y = x) {
		env <- parent.frame()
		env$y1 <- log(y)
		y2 <- y^2
		return(0)
	}
	### I want the variable 'y1' is available here.
	### WITHOUT using the return() within x1()
	try(x1(), silent = TRUE)
	print(y1)  # available
	return(class(try(x1(), silent = TRUE)))
}

fn2(5)
fn2("sd")  # This will throw an error in print(y1)

The example needs to be refined but y1 is available to the parent.env of 
x1(), if x1() succeeds.


Hope this helps,

Rui Barradas

Em 16-03-2013 13:27, Christofer Bogaso escreveu: