Às 18:33 de 07/11/2022, akshay kulkarni escreveu:
Dear Rui,
                    Actually, I am replacing a big for loop by the lapply() 
function, and report the progress:

lapply(TP, function(i) { BODY; print(i)})

Can you please adjust your solution in this light?

THanking you,
Yours sincerely,
AKSHAY M KULKARNI
________________________________
From: Rui Barradas <ruipbarra...@sapo.pt>
Sent: Monday, November 7, 2022 11:59 PM
To: akshay kulkarni <akshay...@hotmail.com>; R help Mailing list 
<r-help@r-project.org>
Subject: Re: [R] print and lapply....

Às 17:17 de 07/11/2022, akshay kulkarni escreveu:
Dear members,
                               I have the following code and output:

TP <- 1:4
lapply(TP,function(x){print(x);x^2})
[1] 1
[1] 2
[1] 3
[1] 4
[[1]]
[1] 1

[[2]]
[1] 4

[[3]]
[1] 9

[[4]]
[1] 16

How do I make the print function output x along with x^2, i.e not at the 
beginning but before each of x^2?

Many thanks in advance....

THanking you,
Yours sincerely
AKSHAY M KULKARNI

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.
Hello,

Here are two options, with ?cat and with ?message.


TP <- 1:4
lapply(TP, function(x){
    cat("x =", x, "x^2 =", x^2, "\n")
})

lapply(TP, function(x){
    msg <- paste("x =", x, "x^2 =", x^2)
    message(msg)
})


Hope this helps,

Rui Barradas



Hello,


What do you want the lapply loop to return? If you have a BODY doing computations, do you want the lapply to return those values and report the progress?

I have chosen cat or message over print because

 - cat returns invisible(NULL),
 - message returns invisible()
 - print returns a value, what it prints.

Can you adapt the code below to your use case?



TP <- 1:4
lapply(TP, function(x, verbose = TRUE){
  # BODY
  y <- rnorm(100, mean = x)

  # show progress
  if(verbose)
    cat("x =", x, "x^2 =", x^2, "\n")

  #return value
  c(x = x, mean = mean(y))
})

lapply(TP, function(x, verbose = TRUE){
  # BODY
  y <- rnorm(100, mean = x)

  # show progress
  if(verbose) {
    msg <- paste("x =", x, "x^2 =", x^2)
    message(msg)
  }

  #return value
  c(x = x, mean = mean(y))
})



Hope this helps,

Rui Barradas

______________________________________________
R-help@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.

Reply via email to