[R] cannot print a list with cat

Steven T. Yen @tyen @end|ng |rom ntu@edu@tw
Mon Oct 24 17:21:00 CEST 2022


Thanks to everyone. I read ? sprint and the following is best I came up 
with. If there are ways to collapse the lines I'd be glad to know. 
Otherwise, I will live with this. Thanks again.

cat(sprintf("\ntol     = %e",mycontrol$tol),
     sprintf("\nreltol  = %e",mycontrol$reltol),
     sprintf("\nsteptol = %e",mycontrol$steptol),
     sprintf("\ngradtol = %e",mycontrol$gradtol))

tol     = 0.000000e+00
reltol  = 0.000000e+00
steptol = 1.000000e-08
gradtol = 1.000000e-10

On 10/24/2022 10:02 PM, Rui Barradas wrote:
> Hello,
>
> There's also ?message.
>
>
> msg <- sprintf("(tol,reltol,steptol,gradtol): %E %E %E %E",
>
> mycontrol$tol,mycontrol$reltol,mycontrol$steptol,mycontrol$gradtol)
> message(msg)
>
>
> Hope this helps,
>
> Rui Barradas
>
> Às 14:25 de 24/10/2022, Steven T. Yen escreveu:
>> Thank, Boris and Ivan.
>>
>> The simple command suggested by Ivan ( print(t(mycontrol)) ) worked. 
>> I went along with Boris' suggestion and do/get the following:
>>
>> cat(sprintf("(tol,reltol,steptol,gradtol): %E %E %E %E",mycontrol$tol,
>> mycontrol$reltol,mycontrol$steptol,mycontrol$gradtol))
>>
>> (tol,reltol,steptol,gradtol): 0.000000E+00 0.000000E+00 1.000000E-08 
>> 1.000000E-12
>>
>> This works great. Thanks.
>>
>> Steven
>>
>> On 10/24/2022 9:05 PM, Boris Steipe wrote:
>>
>>> ???  t() is the transpose function. It just happens to return your 
>>> list unchanged. The return value is then printed to console if it is 
>>> not assigned, or returned invisibly. Transposing your list is 
>>> probably not what you wanted to do.
>>>
>>> Returned values do not get printed from within a loop or from a 
>>> source()'d script. That's why it "works" interactively, but not from 
>>> a script file.
>>>
>>> If you want to print the contents of your list, just use:
>>>    print(mycontrol)
>>>
>>> Or use some incantation with sprintf() if you want more control 
>>> about the format of what gets printed. Eg:
>>>
>>>   cat(sprintf("Tolerance: %f (%f %%)", mycontrol$tol, 
>>> mycontrol$reltol))
>>>
>>> etc.
>>>
>>>
>>> B.
>>>
>>>
>>>
>>>> On 2022-10-24, at 08:47, Ivan Krylov <krylov.r00t using gmail.com> wrote:
>>>>
>>>> В Mon, 24 Oct 2022 20:39:33 +0800
>>>> "Steven T. Yen" <styen using ntu.edu.tw> пишет:
>>>>
>>>>> Printing this in a main program causes no problem (as shown above).
>>>>> But, using the command t(mycontrol) the line gets ignored.
>>>> t() doesn't print, it returns a value. In R, there's auto-printing in
>>>> the toplevel context (see ?withAutoprint), but not when you move away
>>>> from the interactive prompt. I think that it should be possible to use
>>>> an explicit print(t(mycontrol)) to get the behaviour you desire.
>>>>
>>>> -- 
>>>> Best regards,
>>>> Ivan
>>>>
>>>> ______________________________________________
>>>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>>> 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 using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> 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.



More information about the R-help mailing list