[R] extract printed value from a function
Hello all, This shouldn't be difficult, but I am not able to extract a printed value from a function and assign it to an object. In my case, library(DAAG) twotPermutation(c(2,3,4),c(3,6,5),plotit=F) [1] 0.298 I would like to assign this result to an object. Thanks, Duarte __ 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] extract printed value from a function
On 2011-02-24 03:26, Duarte Viana wrote: Hello all, This shouldn't be difficult, but I am not able to extract a printed value from a function and assign it to an object. In my case, library(DAAG) twotPermutation(c(2,3,4),c(3,6,5),plotit=F) [1] 0.298 I would like to assign this result to an object. Two suggestions: 1. Modify the code for twotPermutation by removing the invisible() at the end. 2. Use capture.output to capture the result and then convert to numeric: pvstring - capture.output( twotPermutation( c(2,3,4), c(3,6,5), plotit=FALSE) )) Now you can use strsplit or substr to extract the numeric part: pv - as.numeric(strsplit(pvstring, )[[1]][2]) or pv - as.numeric( substr(pvstring, nchar(pvstring) - 5, nchar(pvstring))) Peter Ehlers Thanks, Duarte __ 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] extract printed value from a function
On Feb 24, 2011, at 7:22 AM, Peter Ehlers wrote: On 2011-02-24 03:26, Duarte Viana wrote: Hello all, This shouldn't be difficult, but I am not able to extract a printed value from a function and assign it to an object. In my case, library(DAAG) twotPermutation(c(2,3,4),c(3,6,5),plotit=F) [1] 0.298 I would like to assign this result to an object. A third suggestion which has virtues of simplicity and keeping the result numeric: x - print( twotPermutation(c(2,3,4),c(3,6,5),plotit=F) ) x [1] 0.1 x - print(invisible (0.2)) [1] 0.2 x [1] 0.2 -- Two suggestions: 1. Modify the code for twotPermutation by removing the invisible() at the end. 2. Use capture.output to capture the result and then convert to numeric: pvstring - capture.output( twotPermutation( c(2,3,4), c(3,6,5), plotit=FALSE) )) Now you can use strsplit or substr to extract the numeric part: pv - as.numeric(strsplit(pvstring, )[[1]][2]) or pv - as.numeric( substr(pvstring, nchar(pvstring) - 5, nchar(pvstring))) Peter Ehlers Thanks, Duarte __ 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. 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] extract printed value from a function
On 2011-02-24 06:32, David Winsemius wrote: On Feb 24, 2011, at 7:22 AM, Peter Ehlers wrote: On 2011-02-24 03:26, Duarte Viana wrote: Hello all, This shouldn't be difficult, but I am not able to extract a printed value from a function and assign it to an object. In my case, library(DAAG) twotPermutation(c(2,3,4),c(3,6,5),plotit=F) [1] 0.298 I would like to assign this result to an object. A third suggestion which has virtues of simplicity and keeping the result numeric: x- print( twotPermutation(c(2,3,4),c(3,6,5),plotit=F) ) Does that work? Not for me. x [1] 0.1 This may be some 'other' x. DAAG's twotPermutation() has an explicit print(signif(pval, 3)) as its penultimate statement, followed by invisible() I don't know what, if any, advantage this code has over simply returning invisible(signif(pval, 3)) Peter Ehlers x- print(invisible (0.2)) [1] 0.2 x [1] 0.2 -- Two suggestions: 1. Modify the code for twotPermutation by removing the invisible() at the end. 2. Use capture.output to capture the result and then convert to numeric: pvstring- capture.output( twotPermutation( c(2,3,4), c(3,6,5), plotit=FALSE) )) Now you can use strsplit or substr to extract the numeric part: pv- as.numeric(strsplit(pvstring, )[[1]][2]) or pv- as.numeric( substr(pvstring, nchar(pvstring) - 5, nchar(pvstring))) Peter Ehlers Thanks, Duarte __ 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. 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] extract printed value from a function
I don't think that third suggestion actually works: x - print( twotPermutation(c(2,3,4),c(3,6,5),plotit=F) ) [1] 0.289 NULL x NULL twotPermutation returns the value of invisible() which is NULL: x - print(invisible()) x This conflicts with the documented behaviour of twotPermutation which states that it should return the p-value (Value: The p-value for the test of the hypothesis that the mean of x1 differs from x2) and makes no mention of returning it invisibly, or printing the value when it is returned. I suggest that the function is in error and should be corrected by replacing the final two lines --- print(signif(pval, 3)) invisible() - with simply -- signif(pval, 3) - Relying on the default behaviour of R to print the value when it is not assigned. I will copy this suggestion to the listed maintainer (W. John Braun br...@stats.uwo.ca) David Winsemius dwinsem...@comcast.net wrote in message news:53d4d49f-f8e1-44c0-b663-8b24d8420...@comcast.net... On Feb 24, 2011, at 7:22 AM, Peter Ehlers wrote: On 2011-02-24 03:26, Duarte Viana wrote: Hello all, This shouldn't be difficult, but I am not able to extract a printed value from a function and assign it to an object. In my case, library(DAAG) twotPermutation(c(2,3,4),c(3,6,5),plotit=F) [1] 0.298 I would like to assign this result to an object. A third suggestion which has virtues of simplicity and keeping the result numeric: x - print( twotPermutation(c(2,3,4),c(3,6,5),plotit=F) ) x [1] 0.1 x - print(invisible (0.2)) [1] 0.2 x [1] 0.2 -- Two suggestions: 1. Modify the code for twotPermutation by removing the invisible() at the end. 2. Use capture.output to capture the result and then convert to numeric: pvstring - capture.output( twotPermutation( c(2,3,4), c(3,6,5), plotit=FALSE) )) Now you can use strsplit or substr to extract the numeric part: pv - as.numeric(strsplit(pvstring, )[[1]][2]) or pv - as.numeric( substr(pvstring, nchar(pvstring) - 5, nchar(pvstring))) Peter Ehlers Thanks, Duarte __ 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] extract printed value from a function
On Feb 24, 2011, at 10:07 AM, Peter Ehlers wrote: On 2011-02-24 06:32, David Winsemius wrote: On Feb 24, 2011, at 7:22 AM, Peter Ehlers wrote: On 2011-02-24 03:26, Duarte Viana wrote: Hello all, This shouldn't be difficult, but I am not able to extract a printed value from a function and assign it to an object. In my case, library(DAAG) twotPermutation(c(2,3,4),c(3,6,5),plotit=F) [1] 0.298 I would like to assign this result to an object. A third suggestion which has virtues of simplicity and keeping the result numeric: x- print( twotPermutation(c(2,3,4),c(3,6,5),plotit=F) ) Does that work? Not for me. Sorry. Not for me either. I had assumed from your comments about the use of invisible that the value was returned in invisible() but see now that you were being more specific. The author is effectively blanking the result and the help page is misleading when stating that the p-value is the Value. He would be more accurate if stating that the p-value is printed as a side effect but that no value is returned. x [1] 0.1 This may be some 'other' x. DAAG's twotPermutation() has an explicit print(signif(pval, 3)) as its penultimate statement, followed by invisible() I don't know what, if any, advantage this code has over simply returning invisible(signif(pval, 3)) Peter Ehlers x- print(invisible (0.2)) [1] 0.2 x [1] 0.2 -- Two suggestions: 1. Modify the code for twotPermutation by removing the invisible() at the end. 2. Use capture.output to capture the result and then convert to numeric: pvstring- capture.output( twotPermutation( c(2,3,4), c(3,6,5), plotit=FALSE) )) Now you can use strsplit or substr to extract the numeric part: pv- as.numeric(strsplit(pvstring, )[[1]][2]) or pv- as.numeric( substr(pvstring, nchar(pvstring) - 5, nchar(pvstring))) Peter Ehlers Thanks, Duarte __ 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. David Winsemius, MD West Hartford, CT 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] extract printed value from a function
PE == Peter Ehlers ehl...@ucalgary.ca on Thu, 24 Feb 2011 07:07:29 -0800 writes: PE On 2011-02-24 06:32, David Winsemius wrote: On Feb 24, 2011, at 7:22 AM, Peter Ehlers wrote: On 2011-02-24 03:26, Duarte Viana wrote: Hello all, This shouldn't be difficult, but I am not able to extract a printed value from a function and assign it to an object. In my case, library(DAAG) twotPermutation(c(2,3,4),c(3,6,5),plotit=F) [1] 0.298 I would like to assign this result to an object. A third suggestion which has virtues of simplicity and keeping the result numeric: x- print( twotPermutation(c(2,3,4),c(3,6,5),plotit=F) ) PE Does that work? Not for me. x [1] 0.1 PE This may be some 'other' x. DAAG's twotPermutation() has an PE explicit PE print(signif(pval, 3)) PE as its penultimate statement, followed by PE invisible() PE I don't know what, if any, advantage this code has over simply PE returning PE invisible(signif(pval, 3)) Indeed! Your proposition is definitely better than the current code. Even better would be to separate computation and printing completely, and that would even involve the rounding step, i.e. a decent twotPermutation() function would return an interesting object of class, say twotPerm (with the unrounded 'pval') which has its simple print.twotPerm - function(x, ...) { ... } method. Martin PE Peter Ehlers x- print(invisible (0.2)) [1] 0.2 x [1] 0.2 -- Two suggestions: 1. Modify the code for twotPermutation by removing the invisible() at the end. 2. Use capture.output to capture the result and then convert to numeric: pvstring- capture.output( twotPermutation( c(2,3,4), c(3,6,5), plotit=FALSE) )) Now you can use strsplit or substr to extract the numeric part: pv- as.numeric(strsplit(pvstring, )[[1]][2]) or pv- as.numeric( substr(pvstring, nchar(pvstring) - 5, nchar(pvstring))) Peter Ehlers Thanks, Duarte __ 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] extract printed value from a function
Let me first thank you all for the replies. Actually I also tried the print() function, and indeed it did not work. The function capture.output() Peter said did the job. Thank you for sending the proposition for the new code to the maintainer. Duarte On Thu, Feb 24, 2011 at 4:51 PM, Martin Maechler maech...@stat.math.ethz.ch wrote: PE == Peter Ehlers ehl...@ucalgary.ca on Thu, 24 Feb 2011 07:07:29 -0800 writes: PE On 2011-02-24 06:32, David Winsemius wrote: On Feb 24, 2011, at 7:22 AM, Peter Ehlers wrote: On 2011-02-24 03:26, Duarte Viana wrote: Hello all, This shouldn't be difficult, but I am not able to extract a printed value from a function and assign it to an object. In my case, library(DAAG) twotPermutation(c(2,3,4),c(3,6,5),plotit=F) [1] 0.298 I would like to assign this result to an object. A third suggestion which has virtues of simplicity and keeping the result numeric: x- print( twotPermutation(c(2,3,4),c(3,6,5),plotit=F) ) PE Does that work? Not for me. x [1] 0.1 PE This may be some 'other' x. DAAG's twotPermutation() has an PE explicit PE print(signif(pval, 3)) PE as its penultimate statement, followed by PE invisible() PE I don't know what, if any, advantage this code has over simply PE returning PE invisible(signif(pval, 3)) Indeed! Your proposition is definitely better than the current code. Even better would be to separate computation and printing completely, and that would even involve the rounding step, i.e. a decent twotPermutation() function would return an interesting object of class, say twotPerm (with the unrounded 'pval') which has its simple print.twotPerm - function(x, ...) { ... } method. Martin PE Peter Ehlers x- print(invisible (0.2)) [1] 0.2 x [1] 0.2 -- Two suggestions: 1. Modify the code for twotPermutation by removing the invisible() at the end. 2. Use capture.output to capture the result and then convert to numeric: pvstring- capture.output( twotPermutation( c(2,3,4), c(3,6,5), plotit=FALSE) )) Now you can use strsplit or substr to extract the numeric part: pv- as.numeric(strsplit(pvstring, )[[1]][2]) or pv- as.numeric( substr(pvstring, nchar(pvstring) - 5, nchar(pvstring))) Peter Ehlers Thanks, Duarte __ 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.