How to view value of a variable inside a function? [duplicate] - r

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
General suggestions for debugging R?
When debugging, I would often like to know the value of a variable used in a function that has completed executing. How can that be achieved?
Example:
I have function:
MyFunction <- function() {
x <- rnorm(10) # assign 10 random normal numbers to x
return(mean(x))
}
I would like to know the values stored in x which are not available to me after the function is done executing and the function's environment is cleaned up.

You mentioned debugging, so I assume the values are not needed later in the script, you just want to check what is happening. In that case, what I always do is use browser:
MyFunction <- function() {
browser()
x <- rnorm(10) # assign 10 random normal numbers to x
return(mean(x))
}
This drops you into an interactive console inside the scope of the function, allowing you to inspect what is happening inside.
For general information about debugging in R I suggest this SO post.

MyFunction <- function() {
x <- rnorm(10) # assign 10 random normal numbers to x
return(list(x,mean(x)))
}
This will return a list where the first element is x and the second is its mean

You have many options here. The easiest is to use the <<- operator when you assign to x. It's also the most likely to get you into trouble.
> test <- function() x <- runif(1)
> x <- NA
> test()
> x
[1] NA
> test <- function() x <<- runif(1)
> test()
> x
[1] 0.7753325
Edit
#PaulHeimstra points out that you'd like this for debugging. Here's a pointer to some general tricks:
General suggestions for debugging in R
I'd recommend either setting options(error=recover) or using trace() in combination with browser().

There are already some good solutions, I'd like to add one possibility. I emphasize on the fact that you want to know the value of a variable used in a function that has completed executing. So there is maybe no need to assign those values, and you don't want (a priori) to stop execution. The solution is to simply use print. So it is not used by default but only when you want to debug, the option to print or not can be passed as a function argument:
MyFunction <- function(x, y, verbose = FALSE) {
a <- x * y
if (verbose) print(a)
b <- x - y
if (verbose) print(b)
return(a * b)
}
In general, you would run your function like this: MyFunction(10, 4) but when you want to see those intermediate results, do MyFunction(10, 4, verbose = TRUE).

Related

Environment issues in R

Just to clarify, I'm not saying that R has issues. The problem is probably on my side, but I'm really confused. I have a function (make_a()) that creates a function a(). I also have a function that uses this function in its definition (fun_using_a()):
make_a <- function(x) {
a <- function(y) {
x + y
}
a
}
fun_using_a <- function(x) {
a(x)/2
}
Now, I create another function that uses these two:
my_fun <- function(x) {
a <- make_a(1)
fun_using_a(x)
}
Calling my_fun(10) gives an error:
my_fun(10)
Error in a(x) : could not find function "a"
However, everything works fine if do essentially the same thing in the global environment:
a <- make_a(1)
fun_using_a(10)
[1] 5.5
What's going on here? Why does my_fun(10) throw an error? It seems that my understanding of R environments must be a bit off somewhere, but I just can't figure it out. When I call my_fun(), shouldn't the function a() be defined in the execution environment after the first line and thus fun_using_a() should be able to find it there (due to lazy evaluation)?
Any help will be greatly appreciated. Thanks a lot!
You would need to save the result of make_a with name a in a place where fun_using_a can see it. There isn't a single "execution environment", every function invocation creates a new one. As posted, make_a returns a function, but you didn't show it being saved anywhere until your second version of the code.
By the way, make_a is likely to have a subtle bug: since x is never evaluated until the first call to a(), its value could change. For example,
x <- 1
a <- make_a(x)
x <- 5
fun_using_a(10)
will return 7.5, not 5.5, since the value of x in a(y) will be 5 instead of 1. To fix it, force the value of x in make_a:
make_a_2 <- function(x) {
force(x)
a <- function(y) {
x + y
}
a
}

How to create and initialize a non existing variable to a default value in R?

This question originates from curiosity, I have nothing to deliver based on this.
Mimicking pass-by-reference (question here) I noticed that both approaches described in the answers obviously fail when the variable does not exist and one tries to use/reference them.
Regardless of its actual usefulness, I would be curious to know if there is a way to initialize the parameter x in the code below, and hence the "actual" parameter myVar, to a default value, with the help of the desired type passed as a string, xtype (passing the type, and in such basic form is not a requirement, it is simply the first thing that came to my mind of non-advanced R programmer).
The question whose solution generated this, here, shows better code in the chosen answer, here using my code as I understand it better
myF <- function(x, xtype) {
varName <- deparse(substitute(x))
if (!exists(varName)) {
# here should initialize x to a default value
# of the type passed in xtype
# to avoid that x <- x ... fails
# this may not have any practical usefulness, just curious
}
x <- x+1
assign(varName,x,envir=parent.frame(n = 1))
NA # sorry this is not a function
# in real life sometimes you also need procedures
}
if (exists(deparse(substitute(myVar)))) {
rm(myVar)
}
myF(myVar, "numeric")
print(myVar)
Error in myF(myVar, "numeric") : object 'myVar' not found
# as expected
Maybe this is what you are looking for (even though it's a terrible idea to write a function like this in R).
myF <- function(x, xtype) {
varName <- deparse(substitute(x))
if (!exists(varName)) {
x <- vector(xtype, 1)
} else {
x <- get(varName)
}
x <- x+1
assign(varName,x,envir=parent.frame(n = 1))
}

How does the assignment of colnames work "behind the scenes"?

(I hope that this question hasn't been asked before).
For convenience I am using abbreviations for functions like "cn" instead of "colnames". However, for colnames/rownames the abbreviated functions only work for reading purposes. I am not able to set colnames with that new "cn" function. Can anyone explain the black magic behind the colnames function? This is the example:
cn <- match.fun(colnames)
x <- matrix(1:2)
colnames(x) <- "a" # OK, works.
cn(x) <- "b" # Error in cn(x) <- "b" : could not find function "cn<-"
Thank you, echasnovski, for the link to that great website.
It has helped me a lot to better understand R!
http://adv-r.had.co.nz/Functions.html#replacement-functions
In R, special "replacement functions" like foo<- can be defined. E.g. we can define a function
`setSecondElement<-` <- function(x, value){
x[2] <- value
return(x)
}
# Let's try it:
x <- 1:3
setSecondElement(x) <- 100
print(x)
# [1] 1 100 3
The colnames<- function works essentially the same. However, "behind the scenes" it will check if x is a data.frame or matrix and set either names(x) or dimnames(x)[[2]]. Just execute the following line in R and you'll see the underlying routine.
print( `colnames<-` )
For my specific problem the solution turns out to be very simple. Remember that I'd like to have a shorter version of colnames which shall be called cn. I can either do it like this:
cn <- match.fun(colnames);
`cn<-` <- function(x, value){
colnames(x) <- value
return(x)
}
More easily, as Stéphane Laurent points out, the definition of `cn<-` can be simplified to:
`cn<-` <- `colnames<-`
There is a minor difference between these approaches. The first approach will define a new function, which calls the colnames<- function. The second approach will copy the reference from the colnames<- function and make exactly the same function call even if you use cn<-. This approach is more efficient, since 1 additinal function call will be avoided.

R code. write a floor function

I want to write a floor function in R, which returns a floating number to its nearest integer. So I tried the below function. It seems that it works if I assign a value to x and run the code inside the function, but it fails when I try to put everything in a function and call the function name later.
Does anyone know how to fix it?
Thanks!
> my_floor <- function(x) {
x <- x-0.5
as.integer(x)
return (x)
}
> y <- 3.1052255
> my_floor(y)
[1] 2.605225
No very sure what you are trying to do but if you simply want the input transformed to the nearest integer towards zero (i.e. floored as you your question goes), the one way to do it would be:
my_floor <- function(x) {
x <- trunc(x)
return (x)
}
This simply discards the non integer part of your input using R's trunc: which you might as well call directly i.e. trunc(y) will still give you the desired result. If you wish to use your function above "as is" then:
my_floor <- function(x) {
x <- x-0.5
x <- as.integer(x) #Store the result of this second step by reassigning x
return (x)
}

Saving output from a function.

I am minimizing this function below using the optim function, which works really well. My only problem is that I can't save the W matrix, I am computing inside the function when minimizing. Is there a way to save the W matrix somehow?
W<-c()
GMM_1_stage <- function(beta) {for (i in 1:(nrow(gmm_i))){
gmm_i[i,]=g_beta(i,beta)}
gmm_N=t(colSums(gmm_i))%*%colSums(gmm_i)
W<-solve((1/(nrow(A)/5))*t(gmm_i)%*%gmm_i)
return(gmm_N)
}
GMM_1<-optim(beta_MLE,GMM_1_stage)
Best regards
Here is a safer version of #mrip's answer that uses a temporary environment rather than <<-:
tempenv <- new.env()
tempenv$xx <- c()
fun<-function(x){
tempenv$xx[ length(tempenv$xx) + 1 ] <- x
x^2
}
optimize(fun,c(-1,1))
tempenv$xx
By using the temporary environment you don't need to worry about accidentally writing over an object in the global environment or <<- assigning in an unexpected place.
You can assign to an object in the global environment (or in a the closest ancestor environment where the variable is defined) using <<-. So, for example, if I wanted to keep track of every value of x during a simple optimization I could do this.
xx<-c()
fun<-function(x){
xx[length(xx)+1]<<-x
x^2
}
optimize(fun,c(-1,1))
xx
## [1] -2.360680e-01 2.360680e-01 5.278640e-01 -2.775558e-17 4.069010e-05
## [6] -4.069010e-05 -2.775558e-17
In your case, if you only want the last value of W you can replace that line in your code with:
W<<-solve((1/(nrow(A)/5))*t(gmm_i)%*%gmm_i)
If you want them all, then first set Wlist<-list(), and then in your function set
Wlist[[length(Wlist)+1]]<<-solve((1/(nrow(A)/5))*t(gmm_i)%*%gmm_i)

Resources