Skip to content

cannot print a list with cat

11 messages · Ivan Krylov, Boris Steipe, Spencer Graves +4 more

#
I have a "list" containing four elements, as shown below:

 > t(mycontrol)

 ???? tol reltol steptol gradtol
[1,] 0?? 0????? 1e-08?? 1e-12

Printing this in a main program causes no problem (as shown above).
But, using the command t(mycontrol) the line gets ignored. Any idea? Thanks.
Steven Yen
#
? Mon, 24 Oct 2022 20:39:33 +0800
"Steven T. Yen" <styen at ntu.edu.tw> ?????:
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.
#
???  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.
#
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:

            
#
On 10/24/22 7:39 AM, Steven T. Yen wrote:
I'm confused.  I get:


 > (mycontrol <- list(tol=0, reltol=0,
+         steptol=1e-8, gradtol=1e-12))
$tol
[1] 0

$reltol
[1] 0

$steptol
[1] 1e-08

$gradtol
[1] 1e-12

 >
 > t(mycontrol)
      tol reltol steptol gradtol
[1,] 0   0      1e-08   1e-12


	  I don't know what you mean by "main program" vs. "the command 
t(mycontrol)".


	  ???
	  Spencer Graves


 > sessionInfo()
R version 4.2.1 (2022-06-23)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur 11.7

Matrix products: default
LAPACK: 
/Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets
[6] methods   base

loaded via a namespace (and not attached):
[1] compiler_4.2.1 tools_4.2.1
#
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:
#
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:
#
"collapse the lines" means ??

If you mean that you want to control the precision (# of decimals
places to show) then that is exactly what sprintf does. ?sprintf tells
you how. If you mean something else, please specify more clearly -- or
await a reply from someone with greater insight than I.

-- Bert
On Mon, Oct 24, 2022 at 8:28 AM Steven T. Yen <styen at ntu.edu.tw> wrote:
#
?s 16:21 de 24/10/2022, Steven T. Yen escreveu:
Hello,

Here is a way. I leave it in may code lines to make it more 
understandale, I hope.


# From Spencer's post
(mycontrol <- list(tol=0, reltol=0, steptol=1e-8, gradtol=1e-12))
#> $tol
#> [1] 0
#>
#> $reltol
#> [1] 0
#>
#> $steptol
#> [1] 1e-08
#>
#> $gradtol
#> [1] 1e-12

fmt_string <- paste0(
   "\ntol     = %e",
   "\nreltol  = %e",
   "\nsteptol = %e",
   "\ngradtol = %e"
)

msg <- sprintf(fmt_string, mycontrol$tol, mycontrol$reltol, 
mycontrol$steptol, mycontrol$gradtol)

msg
#> [1] "\ntol     = 0.000000e+00\nreltol  = 0.000000e+00\nsteptol = 
1.000000e-08\ngradtol = 1.000000e-12"

cat(msg)
#>
#> tol     = 0.000000e+00
#> reltol  = 0.000000e+00
#> steptol = 1.000000e-08
#> gradtol = 1.000000e-12

message(msg)
#>
#> tol     = 0.000000e+00
#> reltol  = 0.000000e+00
#> steptol = 1.000000e-08
#> gradtol = 1.000000e-12


You also can write the format string all in a row.


msg2 <- with(mycontrol, sprintf("\ntol     = %e\nreltol  = %e\nsteptol = 
%e\ngradtol = %e", tol, reltol, steptol, gradtol))
cat(msg2)
#>
#> tol     = 0.000000e+00
#> reltol  = 0.000000e+00
#> steptol = 1.000000e-08
#> gradtol = 1.000000e-12


Hope this helps,

Rui Barradas
#
Thanks to all, who have helped greatly. I essentially followed Rui to do:

 ? fmt_string<-paste0("\ntol???? = %.1e","\nreltol? = %.1e","\nsteptol = 
%.1e","\ngradtol = %.1e")
#msg<-sprintf(fmt_string,mycontrol$tol,mycontrol$reltol,mycontrol$steptol,mycontrol$gradtol) 
#works
msg<-with(mycontrol,sprintf(fmt_string,tol,reltol,steptol,gradtol))
 ? cat(msg)

tol???? = 0.0e+00
reltol? = 0.0e+00
steptol = 1.0e-08
gradtol = 1.0e-10

Thids has worked great! Thanks again to all.

Steven Yen
On 10/25/2022 3:23 AM, Rui Barradas wrote:
1 day later
#
\n is for TERMINATING lines.  Just like in C, C++, Java,
C#, Python, Ruby, Erlang, pretty much everything that
uses \n in strings at all.

sprintf("gradtol = %e\n", mycontrol$gradtol)
makes sense.

More generally, sprintf() takes as many arguments as
you care to give it, so
cat(sprintf("tol = %e\nreltol = %e\nsteptol = %e\ngradtol = %e\n",
   mycontrol$tol, mycontrol$reltol, mycontrol$steptol,
   mycontrol$gradtol))

R being R, I'd prefer using ?format myself.
On Tue, 25 Oct 2022 at 04:29, Steven T. Yen <styen at ntu.edu.tw> wrote: