Re: [R] How to capture console output in a numeric format
Hmmm. Caution would seem more appropriate than fear. My point was that if you want the results as numbers rather than characters, it seems perverse to convert them to characters with print; you should use a suitable mechanism to return them as numbers. If you're nervous of returning values via the environment list and scoping rules it seems curious to accept return via console output. I used '-' as shorthand for a suitable mechanism. Depending on your circumstances, '-' may not be OK- in this example it seems very clear where it will find an existing definition of the variable being assigned. Others in this thread have covered more robust approaches, but as long as you're careful with environments and scoping it seems there are several ways to do what you want. Best regards, Keith J Ravi Varadhan rvarad...@jhmi.edu wrote in message news:2f9ea67ef9ae1c48a147cb41be2e15c303e...@dom-eb-mail1.win.ad.jhu.edu... I did think of this solution, Keith, but I am generally uncomfortable (may be paranoid is a better word) with the use of `-'. Perhaps, my fear is unjustified in this particular situation. Thanks, Ravi. --- Ravi Varadhan, Ph.D. Assistant Professor, Division of Geriatric Medicine and Gerontology School of Medicine Johns Hopkins University Ph. (410) 502-2619 email: rvarad...@jhmi.edu -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Keith Jewell Sent: Friday, June 24, 2011 11:49 AM To: r-h...@stat.math.ethz.ch Subject: Re: [R] How to capture console output in a numeric format If you don't want the information as character, why are you printing it rather than storing it in a matrix? Why not something along the lines of this... fr - function(x) { ## Rosenbrock Banana function on.exit(aMatrix - rbind(aMatrix,(cbind(x1, x2, f x1 - x[1] x2 - x[2] f - 100 * (x2 - x1 * x1)^2 + (1 - x1)^2 f } aMatrix - NULL ans - optim(c(-1.2,1), fr) aMatrix HTH Keith J -Original Message- Ravi Varadhan rvarad...@jhmi.edu wrote in message news:2f9ea67ef9ae1c48a147cb41be2e15c303e...@dom-eb-mail1.win.ad.jhu.edu... Thank you very much, Jim. That works! I did know that I could process the character strings using regex, but was also wondering if there was a direct way to get this. Suppose, in the current example I would like to obtain a 3-column matrix that contains the parameters and the function value: fr - function(x) { ## Rosenbrock Banana function on.exit(print(cbind(x1, x2, f))) x1 - x[1] x2 - x[2] f - 100 * (x2 - x1 * x1)^2 + (1 - x1)^2 f } fvals - capture.output(ans - optim(c(-1.2,1), fr)) Now, I need to tweak your solution to get the 3-column matrix. It would be nice, if there was a more direct way to get the numerical output, perhaps a numeric option in capture.output(). Best, Ravi. --- Ravi Varadhan, Ph.D. Assistant Professor, Division of Geriatric Medicine and Gerontology School of Medicine Johns Hopkins University Ph. (410) 502-2619 email: rvarad...@jhmi.edu __ 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] How to capture console output in a numeric format
try this: fr - function(x) { ## Rosenbrock Banana function +on.exit(print(f)) +x1 - x[1] +x2 - x[2] +f - 100 * (x2 - x1 * x1)^2 + (1 - x1)^2 +f + } fvals - capture.output(ans - optim(c(-1.2,1), fr)) # convert to numeric fvals - as.numeric(sub(^.* , , fvals)) fvals [1] 24.20 7.095296 15.08 4.541696 [5] 6.029216 4.456256 8.879936 7.777856 [9] 4.728125 5.167901 4.21 4.437670 [13] 4.178989 4.326023 4.070813 4.221489 [17] 4.039810 4.896359 4.009379 4.077130 [21] 4.020798 3.993600 4.024586 4.117625 [25] 3.993115 3.976081 3.971089 4.023905 [29] 3.980807 3.952577 3.932179 3.935345 On Fri, Jun 24, 2011 at 10:39 AM, Ravi Varadhan rvarad...@jhmi.edu wrote: Hi, I would like to know how to capture the console output from running an algorithm for further analysis. I can capture this using capture.output() but that yields a character vector. I would like to extract the actual numeric values. Here is an example of what I am trying to do. fr - function(x) { ## Rosenbrock Banana function on.exit(print(f)) x1 - x[1] x2 - x[2] f - 100 * (x2 - x1 * x1)^2 + (1 - x1)^2 f } fvals - capture.output(ans - optim(c(-1.2,1), fr)) Now, `fvals' contains character elements, but I would like to obtain the actual numerical values. How can I do this? Thanks very much for any suggestions. Best, Ravi. --- Ravi Varadhan, Ph.D. Assistant Professor, Division of Geriatric Medicine and Gerontology School of Medicine Johns Hopkins University Ph. (410) 502-2619 email: rvarad...@jhmi.edumailto:rvarad...@jhmi.edu [[alternative HTML version deleted]] __ 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. -- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? __ 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] How to capture console output in a numeric format
One possibility involves applying a regular expression via gsub: as.numeric(gsub(^\\[1\\] ,,fvals)) On Friday 24 June 2011 14:39:31 Ravi Varadhan wrote: Hi, I would like to know how to capture the console output from running an algorithm for further analysis. I can capture this using capture.output() but that yields a character vector. I would like to extract the actual numeric values. Here is an example of what I am trying to do. fr - function(x) { ## Rosenbrock Banana function on.exit(print(f)) x1 - x[1] x2 - x[2] f - 100 * (x2 - x1 * x1)^2 + (1 - x1)^2 f } fvals - capture.output(ans - optim(c(-1.2,1), fr)) Now, `fvals' contains character elements, but I would like to obtain the actual numerical values. How can I do this? Thanks very much for any suggestions. Best, Ravi. --- Ravi Varadhan, Ph.D. Assistant Professor, Division of Geriatric Medicine and Gerontology School of Medicine Johns Hopkins University Ph. (410) 502-2619 email: rvarad...@jhmi.edumailto:rvarad...@jhmi.edu [[alternative HTML version deleted]] __ 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] How to capture console output in a numeric format
Thank you very much, Jim. That works! I did know that I could process the character strings using regex, but was also wondering if there was a direct way to get this. Suppose, in the current example I would like to obtain a 3-column matrix that contains the parameters and the function value: fr - function(x) { ## Rosenbrock Banana function on.exit(print(cbind(x1, x2, f))) x1 - x[1] x2 - x[2] f - 100 * (x2 - x1 * x1)^2 + (1 - x1)^2 f } fvals - capture.output(ans - optim(c(-1.2,1), fr)) Now, I need to tweak your solution to get the 3-column matrix. It would be nice, if there was a more direct way to get the numerical output, perhaps a numeric option in capture.output(). Best, Ravi. --- Ravi Varadhan, Ph.D. Assistant Professor, Division of Geriatric Medicine and Gerontology School of Medicine Johns Hopkins University Ph. (410) 502-2619 email: rvarad...@jhmi.edu -Original Message- From: jim holtman [mailto:jholt...@gmail.com] Sent: Friday, June 24, 2011 10:48 AM To: Ravi Varadhan Cc: r-help@r-project.org Subject: Re: [R] How to capture console output in a numeric format try this: fr - function(x) { ## Rosenbrock Banana function +on.exit(print(f)) +x1 - x[1] +x2 - x[2] +f - 100 * (x2 - x1 * x1)^2 + (1 - x1)^2 +f + } fvals - capture.output(ans - optim(c(-1.2,1), fr)) # convert to numeric fvals - as.numeric(sub(^.* , , fvals)) fvals [1] 24.20 7.095296 15.08 4.541696 [5] 6.029216 4.456256 8.879936 7.777856 [9] 4.728125 5.167901 4.21 4.437670 [13] 4.178989 4.326023 4.070813 4.221489 [17] 4.039810 4.896359 4.009379 4.077130 [21] 4.020798 3.993600 4.024586 4.117625 [25] 3.993115 3.976081 3.971089 4.023905 [29] 3.980807 3.952577 3.932179 3.935345 On Fri, Jun 24, 2011 at 10:39 AM, Ravi Varadhan rvarad...@jhmi.edu wrote: Hi, I would like to know how to capture the console output from running an algorithm for further analysis. I can capture this using capture.output() but that yields a character vector. I would like to extract the actual numeric values. Here is an example of what I am trying to do. fr - function(x) { ## Rosenbrock Banana function on.exit(print(f)) x1 - x[1] x2 - x[2] f - 100 * (x2 - x1 * x1)^2 + (1 - x1)^2 f } fvals - capture.output(ans - optim(c(-1.2,1), fr)) Now, `fvals' contains character elements, but I would like to obtain the actual numerical values. How can I do this? Thanks very much for any suggestions. Best, Ravi. --- Ravi Varadhan, Ph.D. Assistant Professor, Division of Geriatric Medicine and Gerontology School of Medicine Johns Hopkins University Ph. (410) 502-2619 email: rvarad...@jhmi.edumailto:rvarad...@jhmi.edu [[alternative HTML version deleted]] __ 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. -- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? __ 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] How to capture console output in a numeric format
Try using dput(f) instead of print(f) and use eval(parse(text=capture.output(...))). I would tend to use something like OUTPUTS - list() fr - function(x) { on.exit(OUTPUTS[[length(OUTPUTS)+1]] - list(x=x,f=f)) ... compute and return f } so you don't have to use capture.output. (You can use trace() to automate this sort of thing.) Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Ravi Varadhan Sent: Friday, June 24, 2011 7:40 AM To: r-help@r-project.org Subject: [R] How to capture console output in a numeric format Hi, I would like to know how to capture the console output from running an algorithm for further analysis. I can capture this using capture.output() but that yields a character vector. I would like to extract the actual numeric values. Here is an example of what I am trying to do. fr - function(x) { ## Rosenbrock Banana function on.exit(print(f)) x1 - x[1] x2 - x[2] f - 100 * (x2 - x1 * x1)^2 + (1 - x1)^2 f } fvals - capture.output(ans - optim(c(-1.2,1), fr)) Now, `fvals' contains character elements, but I would like to obtain the actual numerical values. How can I do this? Thanks very much for any suggestions. Best, Ravi. --- Ravi Varadhan, Ph.D. Assistant Professor, Division of Geriatric Medicine and Gerontology School of Medicine Johns Hopkins University Ph. (410) 502-2619 email: rvarad...@jhmi.edumailto:rvarad...@jhmi.edu [[alternative HTML version deleted]] __ 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] How to capture console output in a numeric format
Here is one approach (different from capture.output) that I believe accomplishes what you are trying to do: frtop - function(){ out - numeric(0) topenv - environment() fr - function(x) { ## Rosenbrock Banana function x1 - x[1] x2 - x[2] f - 100 * (x2 - x1 * x1)^2 + (1 - x1)^2 topenv$out - c(topenv$out, f) f } ans - optim(c(-1.2,1), fr) list(ans=ans, out=out) } out - frtop() Now out is a list with 2 components, the first is the return value from optim, the second is a vector with all the f values from the evaluation of the fr function. The basic idea is to wrap everything in a toplevel function, so that it has its own local environment, then define the out vector in the toplevel environment. We also have a link to that environment through the topenv variable. Now fr is defined inside of frtop so it can access that environment (lexical scoping) and so when we assign to topenv$out it is doing the assignment in the environment of frtop. Then we just run optim which calls fr a bunch of times, then output a list with the output and the values at each call. Hope this helps, -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Ravi Varadhan Sent: Friday, June 24, 2011 8:40 AM To: r-help@r-project.org Subject: [R] How to capture console output in a numeric format Hi, I would like to know how to capture the console output from running an algorithm for further analysis. I can capture this using capture.output() but that yields a character vector. I would like to extract the actual numeric values. Here is an example of what I am trying to do. fr - function(x) { ## Rosenbrock Banana function on.exit(print(f)) x1 - x[1] x2 - x[2] f - 100 * (x2 - x1 * x1)^2 + (1 - x1)^2 f } fvals - capture.output(ans - optim(c(-1.2,1), fr)) Now, `fvals' contains character elements, but I would like to obtain the actual numerical values. How can I do this? Thanks very much for any suggestions. Best, Ravi. --- Ravi Varadhan, Ph.D. Assistant Professor, Division of Geriatric Medicine and Gerontology School of Medicine Johns Hopkins University Ph. (410) 502-2619 email: rvarad...@jhmi.edumailto:rvarad...@jhmi.edu [[alternative HTML version deleted]] __ 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] How to capture console output in a numeric format
On Jun 24, 2011, at 11:10 AM, Ravi Varadhan wrote: Thank you very much, Jim. That works! I did know that I could process the character strings using regex, but was also wondering if there was a direct way to get this. Suppose, in the current example I would like to obtain a 3-column matrix that contains the parameters and the function value: fr - function(x) { ## Rosenbrock Banana function on.exit(print(cbind(x1, x2, f))) x1 - x[1] x2 - x[2] f - 100 * (x2 - x1 * x1)^2 + (1 - x1)^2 f } fvals - capture.output() Now, I need to tweak your solution to get the 3-column matrix. I don't understand the 3 column matrix part of your request. Are you asking for something that is not printed to the console? I only get a sequence of single element vectors at my console when I execute: ans - optim(c(-1.2,1), fr) It would be nice, if there was a more direct way to get the numerical output, perhaps a numeric option in capture.output(). But maybe Snow's answer is a complete solution. Best, Ravi. -- David Winsemius, MD West Hartford, CT __ 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] How to capture console output in a numeric format
Ravi, Consider using an environment (i.e. a 'reference' object) to store the results, avoiding string manipulation, and the potential for loss of precision: fr - function(x, env) { ## Rosenbrock Banana function x1 - x[1] x2 - x[2] f - 100 * (x2 - x1 * x1)^2 + (1 - x1)^2 if(exists('fout', env)) fout - rbind(get('fout', env), c(x1, x2, f)) else fout - c(x1=x1, x2=x2, f=f) assign('fout', fout, env) f } out - new.env() ans - optim(c(-1.2, 1), fr, env=out) out$fout Best, Matt On Fri, 2011-06-24 at 15:10 +, Ravi Varadhan wrote: Thank you very much, Jim. That works! I did know that I could process the character strings using regex, but was also wondering if there was a direct way to get this. Suppose, in the current example I would like to obtain a 3-column matrix that contains the parameters and the function value: fr - function(x) { ## Rosenbrock Banana function on.exit(print(cbind(x1, x2, f))) x1 - x[1] x2 - x[2] f - 100 * (x2 - x1 * x1)^2 + (1 - x1)^2 f } fvals - capture.output(ans - optim(c(-1.2,1), fr)) Now, I need to tweak your solution to get the 3-column matrix. It would be nice, if there was a more direct way to get the numerical output, perhaps a numeric option in capture.output(). Best, Ravi. --- Ravi Varadhan, Ph.D. Assistant Professor, Division of Geriatric Medicine and Gerontology School of Medicine Johns Hopkins University Ph. (410) 502-2619 email: rvarad...@jhmi.edu -Original Message- From: jim holtman [mailto:jholt...@gmail.com] Sent: Friday, June 24, 2011 10:48 AM To: Ravi Varadhan Cc: r-help@r-project.org Subject: Re: [R] How to capture console output in a numeric format try this: fr - function(x) { ## Rosenbrock Banana function +on.exit(print(f)) +x1 - x[1] +x2 - x[2] +f - 100 * (x2 - x1 * x1)^2 + (1 - x1)^2 +f + } fvals - capture.output(ans - optim(c(-1.2,1), fr)) # convert to numeric fvals - as.numeric(sub(^.* , , fvals)) fvals [1] 24.20 7.095296 15.08 4.541696 [5] 6.029216 4.456256 8.879936 7.777856 [9] 4.728125 5.167901 4.21 4.437670 [13] 4.178989 4.326023 4.070813 4.221489 [17] 4.039810 4.896359 4.009379 4.077130 [21] 4.020798 3.993600 4.024586 4.117625 [25] 3.993115 3.976081 3.971089 4.023905 [29] 3.980807 3.952577 3.932179 3.935345 On Fri, Jun 24, 2011 at 10:39 AM, Ravi Varadhan rvarad...@jhmi.edu wrote: Hi, I would like to know how to capture the console output from running an algorithm for further analysis. I can capture this using capture.output() but that yields a character vector. I would like to extract the actual numeric values. Here is an example of what I am trying to do. fr - function(x) { ## Rosenbrock Banana function on.exit(print(f)) x1 - x[1] x2 - x[2] f - 100 * (x2 - x1 * x1)^2 + (1 - x1)^2 f } fvals - capture.output(ans - optim(c(-1.2,1), fr)) Now, `fvals' contains character elements, but I would like to obtain the actual numerical values. How can I do this? Thanks very much for any suggestions. Best, Ravi. --- Ravi Varadhan, Ph.D. Assistant Professor, Division of Geriatric Medicine and Gerontology School of Medicine Johns Hopkins University Ph. (410) 502-2619 email: rvarad...@jhmi.edumailto:rvarad...@jhmi.edu [[alternative HTML version deleted]] __ 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. -- Matthew S. Shotwell Assistant Professor, Department of Biostatistics School of Medicine, Vanderbilt University 1161 21st Ave. S2323 MCN Office CC2102L Nashville, TN 37232-2158 __ 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] How to capture console output in a numeric format
If you don't want the information as character, why are you printing it rather than storing it in a matrix? Why not something along the lines of this... fr - function(x) { ## Rosenbrock Banana function on.exit(aMatrix - rbind(aMatrix,(cbind(x1, x2, f x1 - x[1] x2 - x[2] f - 100 * (x2 - x1 * x1)^2 + (1 - x1)^2 f } aMatrix - NULL ans - optim(c(-1.2,1), fr) aMatrix HTH Keith J -Original Message- Ravi Varadhan rvarad...@jhmi.edu wrote in message news:2f9ea67ef9ae1c48a147cb41be2e15c303e...@dom-eb-mail1.win.ad.jhu.edu... Thank you very much, Jim. That works! I did know that I could process the character strings using regex, but was also wondering if there was a direct way to get this. Suppose, in the current example I would like to obtain a 3-column matrix that contains the parameters and the function value: fr - function(x) { ## Rosenbrock Banana function on.exit(print(cbind(x1, x2, f))) x1 - x[1] x2 - x[2] f - 100 * (x2 - x1 * x1)^2 + (1 - x1)^2 f } fvals - capture.output(ans - optim(c(-1.2,1), fr)) Now, I need to tweak your solution to get the 3-column matrix. It would be nice, if there was a more direct way to get the numerical output, perhaps a numeric option in capture.output(). Best, Ravi. --- Ravi Varadhan, Ph.D. Assistant Professor, Division of Geriatric Medicine and Gerontology School of Medicine Johns Hopkins University Ph. (410) 502-2619 email: rvarad...@jhmi.edu __ 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] How to capture console output in a numeric format
I did think of this solution, Keith, but I am generally uncomfortable (may be paranoid is a better word) with the use of `-'. Perhaps, my fear is unjustified in this particular situation. Thanks, Ravi. --- Ravi Varadhan, Ph.D. Assistant Professor, Division of Geriatric Medicine and Gerontology School of Medicine Johns Hopkins University Ph. (410) 502-2619 email: rvarad...@jhmi.edu -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Keith Jewell Sent: Friday, June 24, 2011 11:49 AM To: r-h...@stat.math.ethz.ch Subject: Re: [R] How to capture console output in a numeric format If you don't want the information as character, why are you printing it rather than storing it in a matrix? Why not something along the lines of this... fr - function(x) { ## Rosenbrock Banana function on.exit(aMatrix - rbind(aMatrix,(cbind(x1, x2, f x1 - x[1] x2 - x[2] f - 100 * (x2 - x1 * x1)^2 + (1 - x1)^2 f } aMatrix - NULL ans - optim(c(-1.2,1), fr) aMatrix HTH Keith J -Original Message- Ravi Varadhan rvarad...@jhmi.edu wrote in message news:2f9ea67ef9ae1c48a147cb41be2e15c303e...@dom-eb-mail1.win.ad.jhu.edu... Thank you very much, Jim. That works! I did know that I could process the character strings using regex, but was also wondering if there was a direct way to get this. Suppose, in the current example I would like to obtain a 3-column matrix that contains the parameters and the function value: fr - function(x) { ## Rosenbrock Banana function on.exit(print(cbind(x1, x2, f))) x1 - x[1] x2 - x[2] f - 100 * (x2 - x1 * x1)^2 + (1 - x1)^2 f } fvals - capture.output(ans - optim(c(-1.2,1), fr)) Now, I need to tweak your solution to get the 3-column matrix. It would be nice, if there was a more direct way to get the numerical output, perhaps a numeric option in capture.output(). Best, Ravi. --- Ravi Varadhan, Ph.D. Assistant Professor, Division of Geriatric Medicine and Gerontology School of Medicine Johns Hopkins University Ph. (410) 502-2619 email: rvarad...@jhmi.edu __ 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] How to capture console output in a numeric format
On Jun 24, 2011, at 11:27 AM, Matt Shotwell wrote: Ravi, Consider using an environment (i.e. a 'reference' object) to store the results, avoiding string manipulation, and the potential for loss of precision: fr - function(x, env) { ## Rosenbrock Banana function x1 - x[1] x2 - x[2] f - 100 * (x2 - x1 * x1)^2 + (1 - x1)^2 if(exists('fout', env)) fout - rbind(get('fout', env), c(x1, x2, f)) So _that's_ what a reference object is? This seems to give the same results in this example. Am I committing any sins by sneaking around the get()? if(exists('fout', env)) fout - rbind(env[['fout']], c(x1, x2, f)) # seems more direct Thinking I also might be able to avoid the later assign(), I tried these without success. fr - function(x, env) { ## Rosenbrock Banana function x1 - x[1] x2 - x[2] f - 100 * (x2 - x1 * x1)^2 + (1 - x1)^2 if(exists('fout', env)) env[['fout']] - rbind(env[['fout']], c(x1, x2, f)) else fout - c(x1=x1, x2=x2, f=f) f } out - new.env() ans - optim(c(-1.2, 1), fr, env=out) out$fout # NULL Is there no '[[-' for environments? (Also tried '-' but I know that is sinful/ ) -- David. else fout - c(x1=x1, x2=x2, f=f) assign('fout', fout, env) f } out - new.env() ans - optim(c(-1.2, 1), fr, env=out) out$fout Best, Matt On Fri, 2011-06-24 at 15:10 +, Ravi Varadhan wrote: Thank you very much, Jim. That works! I did know that I could process the character strings using regex, but was also wondering if there was a direct way to get this. Suppose, in the current example I would like to obtain a 3-column matrix that contains the parameters and the function value: fr - function(x) { ## Rosenbrock Banana function on.exit(print(cbind(x1, x2, f))) x1 - x[1] x2 - x[2] f - 100 * (x2 - x1 * x1)^2 + (1 - x1)^2 f } fvals - capture.output(ans - optim(c(-1.2,1), fr)) Now, I need to tweak your solution to get the 3-column matrix. It would be nice, if there was a more direct way to get the numerical output, perhaps a numeric option in capture.output(). Best, Ravi. --- Ravi Varadhan, Ph.D. Assistant Professor, Division of Geriatric Medicine and Gerontology School of Medicine Johns Hopkins University Ph. (410) 502-2619 email: rvarad...@jhmi.edu -Original Message- From: jim holtman [mailto:jholt...@gmail.com] Sent: Friday, June 24, 2011 10:48 AM To: Ravi Varadhan Cc: r-help@r-project.org Subject: Re: [R] How to capture console output in a numeric format try this: fr - function(x) { ## Rosenbrock Banana function +on.exit(print(f)) +x1 - x[1] +x2 - x[2] +f - 100 * (x2 - x1 * x1)^2 + (1 - x1)^2 +f + } fvals - capture.output(ans - optim(c(-1.2,1), fr)) # convert to numeric fvals - as.numeric(sub(^.* , , fvals)) fvals [1] 24.20 7.095296 15.08 4.541696 [5] 6.029216 4.456256 8.879936 7.777856 [9] 4.728125 5.167901 4.21 4.437670 [13] 4.178989 4.326023 4.070813 4.221489 [17] 4.039810 4.896359 4.009379 4.077130 [21] 4.020798 3.993600 4.024586 4.117625 [25] 3.993115 3.976081 3.971089 4.023905 [29] 3.980807 3.952577 3.932179 3.935345 On Fri, Jun 24, 2011 at 10:39 AM, Ravi Varadhan rvarad...@jhmi.edu wrote: Hi, I would like to know how to capture the console output from running an algorithm for further analysis. I can capture this using capture.output() but that yields a character vector. I would like to extract the actual numeric values. Here is an example of what I am trying to do. fr - function(x) { ## Rosenbrock Banana function on.exit(print(f)) x1 - x[1] x2 - x[2] f - 100 * (x2 - x1 * x1)^2 + (1 - x1)^2 f } fvals - capture.output(ans - optim(c(-1.2,1), fr)) Now, `fvals' contains character elements, but I would like to obtain the actual numerical values. How can I do this? Thanks very much for any suggestions. Best, Ravi. --- Ravi Varadhan, Ph.D. Assistant Professor, Division of Geriatric Medicine and Gerontology School of Medicine Johns Hopkins University Ph. (410) 502-2619 email: rvarad...@jhmi.edumailto:rvarad...@jhmi.edu [[alternative HTML version deleted]] __ 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. -- Matthew S. Shotwell Assistant Professor, Department of Biostatistics School of Medicine, Vanderbilt University 1161 21st Ave. S2323 MCN
Re: [R] How to capture console output in a numeric format
On Fri, Jun 24, 2011 at 11:10 AM, Ravi Varadhan rvarad...@jhmi.edu wrote: Thank you very much, Jim. That works! I did know that I could process the character strings using regex, but was also wondering if there was a direct way to get this. Suppose, in the current example I would like to obtain a 3-column matrix that contains the parameters and the function value: fr - function(x) { ## Rosenbrock Banana function on.exit(print(cbind(x1, x2, f))) x1 - x[1] x2 - x[2] f - 100 * (x2 - x1 * x1)^2 + (1 - x1)^2 f } fvals - capture.output(ans - optim(c(-1.2,1), fr)) Now, I need to tweak your solution to get the 3-column matrix. It would be nice, if there was a more direct way to get the numerical output, perhaps a numeric option in capture.output(). This works in general assuming that we only wish to keep those output lines having the same number of numerics as the most frequently output length, i.e. 3 in your example above and 1 in the original example. Also it does not require modification of the function to be optimized. The first line extracts all numerics that have a decimal in them (so that, for example, the 1 in [1] is not extracted). The next two lines count how many numbers are in each extracted line and returns the most frequently occurring non-zero length as n so that n is 3 here. The final line extracts those lines and rbind's them. fvals is from your post. library(gsubfn) fvals.numeric - strapply(fvals, [-0-9]+[.][0-9]*, as.numeric) tab - table(sapply(fvals.numeric, length)) n - as.numeric(names(which.max(tab[names(tab) 0]))) do.call(rbind, Filter(function(x) length(x) == n, fvals.numeric)) If we knew in advance that we wanted only lines with 3 numbers and that those three numbers are each prefaced by a space (both of which are the case in your example) then we could simplify it to just this: library(gsubfn) fvals.numeric - strapply(fvals, [-0-9.]+, as.numeric) do.call(rbind, fn$Filter(x ~ length(x) == 3, fvals.numeric)) There is more on strapply and fn$ at http://gsubfn.googlecode.com -- Statistics Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at 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.
Re: [R] How to capture console output in a numeric format
On Fri, 2011-06-24 at 12:09 -0400, David Winsemius wrote: On Jun 24, 2011, at 11:27 AM, Matt Shotwell wrote: Ravi, Consider using an environment (i.e. a 'reference' object) to store the results, avoiding string manipulation, and the potential for loss of precision: fr - function(x, env) { ## Rosenbrock Banana function x1 - x[1] x2 - x[2] f - 100 * (x2 - x1 * x1)^2 + (1 - x1)^2 if(exists('fout', env)) fout - rbind(get('fout', env), c(x1, x2, f)) So _that's_ what a reference object is? Well, environments have 'pass-by-reference' behavior. That is, when they are passed to a function, modifications to the environment persist outside the function call. This is distinct from the Reference class (?methods::ReferenceClass). But there are similar concepts. The methods of a reference class can modify the class fields in a 'by-reference' fashion. However, the fields need not be passed to a method. This seems to give the same results in this example. Am I committing any sins by sneaking around the get()? if(exists('fout', env)) fout - rbind(env[['fout']], c(x1, x2, f)) # seems more direct 'env$fout' works here too. Thinking I also might be able to avoid the later assign(), I tried these without success. fr - function(x, env) { ## Rosenbrock Banana function x1 - x[1] x2 - x[2] f - 100 * (x2 - x1 * x1)^2 + (1 - x1)^2 if(exists('fout', env)) env[['fout']] - rbind(env[['fout']], c(x1, x2, f)) else fout - c(x1=x1, x2=x2, f=f) f } this would work with 'env$fout - c(x1=x1, x2=x2, f=f)' following the 'else'. Hence, David's version might look like this: fr - function(x, env) { ## Rosenbrock Banana function x1 - x[1] x2 - x[2] f - 100 * (x2 - x1 * x1)^2 + (1 - x1)^2 if(exists('fout', env)) env$fout - rbind(env$fout, c(x1, x2, f)) else env$fout - c(x1=x1, x2=x2, f=f) f } out - new.env() ans - optim(c(-1.2, 1), fr, env=out) out$fout -Matt out - new.env() ans - optim(c(-1.2, 1), fr, env=out) out$fout # NULL Is there no '[[-' for environments? (Also tried '-' but I know that is sinful/ ) -- David. else fout - c(x1=x1, x2=x2, f=f) assign('fout', fout, env) f } out - new.env() ans - optim(c(-1.2, 1), fr, env=out) out$fout Best, Matt On Fri, 2011-06-24 at 15:10 +, Ravi Varadhan wrote: Thank you very much, Jim. That works! I did know that I could process the character strings using regex, but was also wondering if there was a direct way to get this. Suppose, in the current example I would like to obtain a 3-column matrix that contains the parameters and the function value: fr - function(x) { ## Rosenbrock Banana function on.exit(print(cbind(x1, x2, f))) x1 - x[1] x2 - x[2] f - 100 * (x2 - x1 * x1)^2 + (1 - x1)^2 f } fvals - capture.output(ans - optim(c(-1.2,1), fr)) Now, I need to tweak your solution to get the 3-column matrix. It would be nice, if there was a more direct way to get the numerical output, perhaps a numeric option in capture.output(). Best, Ravi. --- Ravi Varadhan, Ph.D. Assistant Professor, Division of Geriatric Medicine and Gerontology School of Medicine Johns Hopkins University Ph. (410) 502-2619 email: rvarad...@jhmi.edu -Original Message- From: jim holtman [mailto:jholt...@gmail.com] Sent: Friday, June 24, 2011 10:48 AM To: Ravi Varadhan Cc: r-help@r-project.org Subject: Re: [R] How to capture console output in a numeric format try this: fr - function(x) { ## Rosenbrock Banana function +on.exit(print(f)) +x1 - x[1] +x2 - x[2] +f - 100 * (x2 - x1 * x1)^2 + (1 - x1)^2 +f + } fvals - capture.output(ans - optim(c(-1.2,1), fr)) # convert to numeric fvals - as.numeric(sub(^.* , , fvals)) fvals [1] 24.20 7.095296 15.08 4.541696 [5] 6.029216 4.456256 8.879936 7.777856 [9] 4.728125 5.167901 4.21 4.437670 [13] 4.178989 4.326023 4.070813 4.221489 [17] 4.039810 4.896359 4.009379 4.077130 [21] 4.020798 3.993600 4.024586 4.117625 [25] 3.993115 3.976081 3.971089 4.023905 [29] 3.980807 3.952577 3.932179 3.935345 On Fri, Jun 24, 2011 at 10:39 AM, Ravi Varadhan rvarad...@jhmi.edu wrote: Hi, I would like to know how to capture the console output from running an algorithm for further analysis. I can capture this using capture.output() but that yields a character vector. I would like
Re: [R] How to capture console output in a numeric format
On Fri, Jun 24, 2011 at 11:16 AM, William Dunlap wdun...@tibco.com wrote: Try using dput(f) instead of print(f) and use eval(parse(text=capture.output(...))). I would tend to use something like OUTPUTS - list() fr - function(x) { on.exit(OUTPUTS[[length(OUTPUTS)+1]] - list(x=x,f=f)) ... compute and return f } so you don't have to use capture.output. (You can use trace() to automate this sort of thing.) A variation of this is to use a proto object (or to use environments directly at the expense of slightly more code). Below we keep all the loose ends (i.e. aMatrix and fr) in p. Note that p$fr automatically fills in the first argument of fr so that the version that ultimately gets passed to optim only has one argument, x. fr - function(p, x) { ## Rosenbrock Banana function on.exit(p$aMatrix - rbind(p$aMatrix,cbind(x1, x2, f))) x1 - x[1] x2 - x[2] f - 100 * (x2 - x1 * x1)^2 + (1 - x1)^2 f } library(proto) p - proto(fr = fr, aMatrix = NULL) fvals - capture.output(ans - optim(c(-1.2,1), p$fr)) p$aMatrix By the way, in my example using gsubfn another possibility might be to assume that the last line of output defines the number of fields that are to be used. That gives this which is intermediate in length between the other two gsubfn solutions: fr - function(x) { ## Rosenbrock Banana function on.exit(print(cbind(x1, x2, f))) x1 - x[1] x2 - x[2] f - 100 * (x2 - x1 * x1)^2 + (1 - x1)^2 f } fvals - capture.output(ans - optim(c(-1.2,1), fr)) library(gsubfn) fvals.numeric - strapply(fvals, [-0-9]+[.][0-9]*, as.numeric) n - length(tail(fvals.numeric, 1)[[1]]) do.call(rbind, Filter(function(x) length(x) == n, fvals.numeric)) -- Statistics Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at 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.