[R] extract printed value from a function
Peter Ehlers
ehlers at ucalgary.ca
Thu Feb 24 16:07:29 CET 2011
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 at 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 at 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
>
More information about the R-help
mailing list