Re: [R] print and lapply....

2022-11-08 Thread akshay kulkarni
Dear Rui,
  THanks a lot!

THanking you,
Yours sincerely
AKSHAY M KULKARNI

From: Rui Barradas 
Sent: Tuesday, November 8, 2022 8:51 PM
To: akshay kulkarni ; R help Mailing list 

Subject: Re: [R] print and lapply

�s 14:47 de 08/11/2022, akshay kulkarni escreveu:
> Dear Rui,
>The replies from you, Bert, Tim and solved my problem. My 
> last question: what if I put print inside the body of the function passed on 
> to lapply, instead of separately in the function argument of apply? Is this 
> what you insinuated in your reply?
>
> THanking you,
> yours sincerely,
> AKSHAY M KULKARNI
> 
> From: Rui Barradas 
> Sent: Tuesday, November 8, 2022 2:20 AM
> To: akshay kulkarni ; R help Mailing list 
> 
> Subject: Re: [R] print and lapply
>
> �s 19:22 de 07/11/2022, akshay kulkarni escreveu:
>> Dear Rui,
>>THanks for your reply...The point is the loop is a 
>> scraping code, and in your examples you have assumed that the body acts on 
>> i, the loop variable. Can you adapt your code to JUST PRINT the loop 
>> variable i ?
>>
>> By the by, I think I have stumbled upon the answer: The lapply() caches the 
>> result, and prints the output of the function in question  immediately after 
>> printing the final i. The i's get printed serially, as the function 
>> progresses
>>
>>> lapply(1:4,function(x){print(x);Sys.sleep(x^2);x^2})
>> [1] 1
>> [1] 2
>> [1] 3
>> [1] 4
>> [[1]]
>> [1] 1
>>
>> [[2]]
>> [1] 4
>>
>> [[3]]
>> [1] 9
>>
>> [[4]]
>> [1] 16
>>
>> Here x^2 's print only after 4 is printed on the console
>>
>> tHanks anyways for your reply
>>
>> THanking you,
>> Yours sincerely,
>> AKSHAY M KULKARNI
>> 
>> From: Rui Barradas 
>> Sent: Tuesday, November 8, 2022 12:24 AM
>> To: akshay kulkarni ; R help Mailing list 
>> 
>> Subject: Re: [R] print and lapply
>>
>> �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 
>>> Sent: Monday, November 7, 2022 11:59 PM
>>> To: akshay kulkarni ; R help Mailing list 
>>> 
>>> 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,
>>>
>>> 

Re: [R] print and lapply....

2022-11-08 Thread Rui Barradas

Às 14:47 de 08/11/2022, akshay kulkarni escreveu:

Dear Rui,
   The replies from you, Bert, Tim and solved my problem. My 
last question: what if I put print inside the body of the function passed on to 
lapply, instead of separately in the function argument of apply? Is this what 
you insinuated in your reply?

THanking you,
yours sincerely,
AKSHAY M KULKARNI

From: Rui Barradas 
Sent: Tuesday, November 8, 2022 2:20 AM
To: akshay kulkarni ; R help Mailing list 

Subject: Re: [R] print and lapply

Às 19:22 de 07/11/2022, akshay kulkarni escreveu:

Dear Rui,
   THanks for your reply...The point is the loop is a scraping 
code, and in your examples you have assumed that the body acts on i, the loop 
variable. Can you adapt your code to JUST PRINT the loop variable i ?

By the by, I think I have stumbled upon the answer: The lapply() caches the 
result, and prints the output of the function in question  immediately after 
printing the final i. The i's get printed serially, as the function 
progresses


lapply(1:4,function(x){print(x);Sys.sleep(x^2);x^2})

[1] 1
[1] 2
[1] 3
[1] 4
[[1]]
[1] 1

[[2]]
[1] 4

[[3]]
[1] 9

[[4]]
[1] 16

Here x^2 's print only after 4 is printed on the console

tHanks anyways for your reply

THanking you,
Yours sincerely,
AKSHAY M KULKARNI

From: Rui Barradas 
Sent: Tuesday, November 8, 2022 12:24 AM
To: akshay kulkarni ; R help Mailing list 

Subject: Re: [R] print and lapply

À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 
Sent: Monday, November 7, 2022 11:59 PM
To: akshay kulkarni ; R help Mailing list 

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



Hello,

No, the x^2 are not printed after the i's. The x^2 are the function's
return values. The function prints the i's, then returns x^2.

As for your problem, it is now more clerar.
I would write a function accepting a url to take care of scraping and
call it in the lapply loop. The progress report can be in the loop, like
below.

This is a complete working example, scraping the Wikipedia list of
countries by GDP. The urls are in a list (it's always the same, I'm not
complicating things) and in a real scraping function I would wrap
tryCatch around it, just in case.

First the function, then the urls list, then the lapply loop.



library(rvest)

scrape <- function(url) {
page <- read_html(url)
gdp <- page |>
  html_element(".wikitable") |>
  html_table() |>
  as.data.frame()
names(gdp) <- unlist(gdp[1,, drop = TRUE

Re: [R] print and lapply....

2022-11-08 Thread akshay kulkarni
Dear Tim,
   I think the recent replies from you, Bert and Rui cleared 
everything ...
THanks a lot..

Thanking you,
Yours sincerely,
AKSHAY M KULKARNI

From: Ebert,Timothy Aaron 
Sent: Tuesday, November 8, 2022 2:15 AM
To: akshay kulkarni ; Bert Gunter 
; Andrew Simmons 
Cc: R help Mailing list 
Subject: RE: [R] print and lapply

Dear Akshay,
I think we have provided several solutions to the question asked. Can you 
please adjust your question to more closely align with what you need. It would 
be nice if you can provide sufficient detail so that we can see how you have 
adapted our solutions and how these have not quite addressed this problem.

Tim

-Original Message-
From: R-help  On Behalf Of akshay kulkarni
Sent: Monday, November 7, 2022 1:10 PM
To: Bert Gunter ; Andrew Simmons 
Cc: R help Mailing list 
Subject: Re: [R] print and lapply

[External Email]

Dear Bert,
   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: Bert Gunter 
Sent: Monday, November 7, 2022 11:16 PM
To: Andrew Simmons 
Cc: akshay kulkarni ; R help Mailing list 

Subject: Re: [R] print and lapply

Well... yes, of course. But assuming the sole purpose is to print the results 
and not to save them for further processing, the OP's approach seems rather 
painful. My preference would be to vectorize:

> print(cbind(TP, TPsq =TP^2), print.gap = 3)
   TP   TPsq
[1,]1  1
[2,]2  4
[3,]3  9
[4,]4 16

See ?print.default for details

-- Bert


On Mon, Nov 7, 2022 at 9:20 AM Andrew Simmons  wrote:
>
> put print() around x^2
>
> On Mon, Nov 7, 2022, 12:18 akshay kulkarni  wrote:
>
> > 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://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fst
> > at.ethz.ch%2Fmailman%2Flistinfo%2Fr-helpdata=05%7C01%7Ctebert%4
> > 0ufl.edu%7Cb726a89349ed4860265708dac0eb5d0a%7C0d4da0f84a314d76ace60a
> > 62331e1b84%7C0%7C0%7C638034414365456384%7CUnknown%7CTWFpbGZsb3d8eyJW
> > IjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C300
> > 0%7C%7C%7Csdata=dQk1JPxWI4D0D6%2FSoC8LWw56749ulaFrGQc6affgBeM%3
> > Dreserved=0
> > PLEASE do read the posting guide
> > https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww
> > .r-project.org%2Fposting-guide.htmldata=05%7C01%7Ctebert%40ufl.
> > edu%7Cb726a89349ed4860265708dac0eb5d0a%7C0d4da0f84a314d76ace60a62331
> > e1b84%7C0%7C0%7C638034414365456384%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiM
> > C4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%
> > 7C%7Csdata=KDskHBxmxe0DV9XO7GvkESemUNTq2w%2BT%2Fro%2Bgb4DS2w%3D
> > reserved=0 and provide commented, minimal, self-contained,
> > reproducible code.
> >
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat
> .ethz.ch%2Fmailman%2Flistinfo%2Fr-helpdata=05%7C01%7Ctebert%40ufl
> .edu%7Cb726a89349ed4860265708dac0eb5d0a%7C0d4da0f84a314d76ace60a62331e
> 1b84%7C0%7C0%7C638034414365456384%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4w
> LjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C
> sdata=dQk1JPxWI4D0D6%2FSoC8LWw56749ulaFrGQc6affgBeM%3Dreserv
> ed=0 PLEASE do read the posting guide
> https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r
> -project.org%2Fposting-guide.htmldata=05%7C01%7Ctebert%40ufl.edu%
> 7Cb726a89349ed4860265708dac0eb5d0a%7C0d4da0f84a314d76ace60a62331e1b84%
> 7C0%7C0%7C638034414365612626%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwM
> DAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C
> sdata=OxZmpA4ORbfUq0gq

Re: [R] print and lapply....

2022-11-08 Thread akshay kulkarni
Dear Rui,
  The replies from you, Bert, Tim and solved my problem. My 
last question: what if I put print inside the body of the function passed on to 
lapply, instead of separately in the function argument of apply? Is this what 
you insinuated in your reply?

THanking you,
yours sincerely,
AKSHAY M KULKARNI

From: Rui Barradas 
Sent: Tuesday, November 8, 2022 2:20 AM
To: akshay kulkarni ; R help Mailing list 

Subject: Re: [R] print and lapply

�s 19:22 de 07/11/2022, akshay kulkarni escreveu:
> Dear Rui,
>   THanks for your reply...The point is the loop is a scraping 
> code, and in your examples you have assumed that the body acts on i, the loop 
> variable. Can you adapt your code to JUST PRINT the loop variable i ?
>
> By the by, I think I have stumbled upon the answer: The lapply() caches the 
> result, and prints the output of the function in question  immediately after 
> printing the final i. The i's get printed serially, as the function 
> progresses
>
>> lapply(1:4,function(x){print(x);Sys.sleep(x^2);x^2})
> [1] 1
> [1] 2
> [1] 3
> [1] 4
> [[1]]
> [1] 1
>
> [[2]]
> [1] 4
>
> [[3]]
> [1] 9
>
> [[4]]
> [1] 16
>
> Here x^2 's print only after 4 is printed on the console
>
> tHanks anyways for your reply
>
> THanking you,
> Yours sincerely,
> AKSHAY M KULKARNI
> 
> From: Rui Barradas 
> Sent: Tuesday, November 8, 2022 12:24 AM
> To: akshay kulkarni ; R help Mailing list 
> 
> Subject: Re: [R] print and lapply
>
> �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 
>> Sent: Monday, November 7, 2022 11:59 PM
>> To: akshay kulkarni ; R help Mailing list 
>> 
>> 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, 

Re: [R] print and lapply....

2022-11-08 Thread akshay kulkarni
Dear Bert,
 Amazing! Very informative...THanks a lot.

From: Bert Gunter 
Sent: Tuesday, November 8, 2022 2:15 AM
To: akshay kulkarni 
Cc: Rui Barradas ; R help Mailing list 

Subject: Re: [R] print and lapply

" The lapply() caches the result, and prints the output of the
function in question  immediately after printing the final i. "

I don't believe you understand how lists or lapply works. You seem to
be trying to intuit from empirical behavior. Bad idea, if so. You need
to read the docs or spend time with suitable tutorials.

Consider:
lapply produces a **list,** each component of which is the result of
applying a function, FUN, to the components of its first argument
(here 1:4), which is itself a list. Results are **not** cached in the
sense you seem to think they are. The list object that is produced by
the function is **only** printed if you ask it to be (via cat, print,
or whatever) or, by default, if the result is unassigned. The default
printing can be turned off via the invisible() function.

So:

> res <- lapply(TP, function(x){
+print(x)
+x^2}
+)
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
## Nothing printed, but the results are the components of **list**
res, which can be printed on the console by print(res) or simply:

> res ## note the list syntax below
[[1]]
[1] 1

[[2]]
[1] 4

[[3]]
[1] 9

[[4]]
[1] 16

[[5]]
[1] 25

Now can you explain what happens here:

> invisible(lapply(TP, function(x){
+print(x)
+x^2}
+))
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
> ## results not printed; nothing assigned. Where can you find them? (See below)

or here:

> lapply(TP, function(x){
+print(x)
+x^2}
+)
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[[1]]
[1] 1

[[2]]
[1] 4

[[3]]
[1] 9

[[4]]
[1] 16

[[5]]
[1] 25

## How could you recover the results of the lapply and not just look
at the console printout if the results have not been assigned?
## Hint:  See ?.Last.value

-- Bert

On Mon, Nov 7, 2022 at 11:22 AM akshay kulkarni  wrote:
>
> Dear Rui,
>  THanks for your reply...The point is the loop is a scraping 
> code, and in your examples you have assumed that the body acts on i, the loop 
> variable. Can you adapt your code to JUST PRINT the loop variable i ?
>
> By the by, I think I have stumbled upon the answer: The lapply() caches the 
> result, and prints the output of the function in question  immediately after 
> printing the final i. The i's get printed serially, as the function 
> progresses
>
> > lapply(1:4,function(x){print(x);Sys.sleep(x^2);x^2})
> [1] 1
> [1] 2
> [1] 3
> [1] 4
> [[1]]
> [1] 1
>
> [[2]]
> [1] 4
>
> [[3]]
> [1] 9
>
> [[4]]
> [1] 16
>
> Here x^2 's print only after 4 is printed on the console
>
> tHanks anyways for your reply
>
> THanking you,
> Yours sincerely,
> AKSHAY M KULKARNI
> ____________
> From: Rui Barradas 
> Sent: Tuesday, November 8, 2022 12:24 AM
> To: akshay kulkarni ; R help Mailing list 
> 
> Subject: Re: [R] print and lapply
>
> �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 
> > Sent: Monday, November 7, 2022 11:59 PM
> > To: akshay kulkarni ; R help Mailing list 
> > 
> > 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/mailm

Re: [R] print and lapply....

2022-11-07 Thread Rui Barradas

Às 19:22 de 07/11/2022, akshay kulkarni escreveu:

Dear Rui,
  THanks for your reply...The point is the loop is a scraping 
code, and in your examples you have assumed that the body acts on i, the loop 
variable. Can you adapt your code to JUST PRINT the loop variable i ?

By the by, I think I have stumbled upon the answer: The lapply() caches the 
result, and prints the output of the function in question  immediately after 
printing the final i. The i's get printed serially, as the function 
progresses


lapply(1:4,function(x){print(x);Sys.sleep(x^2);x^2})

[1] 1
[1] 2
[1] 3
[1] 4
[[1]]
[1] 1

[[2]]
[1] 4

[[3]]
[1] 9

[[4]]
[1] 16

Here x^2 's print only after 4 is printed on the console

tHanks anyways for your reply

THanking you,
Yours sincerely,
AKSHAY M KULKARNI

From: Rui Barradas 
Sent: Tuesday, November 8, 2022 12:24 AM
To: akshay kulkarni ; R help Mailing list 

Subject: Re: [R] print and lapply

À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 
Sent: Monday, November 7, 2022 11:59 PM
To: akshay kulkarni ; R help Mailing list 

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



Hello,

No, the x^2 are not printed after the i's. The x^2 are the function's 
return values. The function prints the i's, then returns x^2.


As for your problem, it is now more clerar.
I would write a function accepting a url to take care of scraping and 
call it in the lapply loop. The progress report can be in the loop, like 
below.


This is a complete working example, scraping the Wikipedia list of 
countries by GDP. The urls are in a list (it's always the same, I'm not 
complicating things) and in a real scraping function I would wrap 
tryCatch around it, just in case.


First the function, then the urls list, then the lapply loop.



library(rvest)

scrape <- function(url) {
  page <- read_html(url)
  gdp <- page |>
html_element(".wikitable") |>
html_table() |>
as.data.frame()
  names(gdp) <- unlist(gdp[1,, drop = TRUE])
  gdp <- gdp[-1,]
  row.names(gdp) <- NULL

  #return value
  gdp
}

wiki_gdp_url <- 
"https://en.wikipedia.org/wiki/List_of_countries_by_GDP_(nominal)"

urls_list <- list(wiki_gdp_url, wiki_gdp_url)
TP <- seq_along(urls_list)

TP
# [1] 1 2

df_list <- lapply(TP, \(i) {
  URL <- urls_list[[i]]
  data <- scrape(URL)
  # show progress
  message("iteration: ", i)
  #return value
  data
})

str(df_list)


Hope this helps,

Rui Barradas

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE 

Re: [R] print and lapply....

2022-11-07 Thread Ebert,Timothy Aaron
Dear Akshay,
I think we have provided several solutions to the question asked. Can you 
please adjust your question to more closely align with what you need. It would 
be nice if you can provide sufficient detail so that we can see how you have 
adapted our solutions and how these have not quite addressed this problem. 

Tim

-Original Message-
From: R-help  On Behalf Of akshay kulkarni
Sent: Monday, November 7, 2022 1:10 PM
To: Bert Gunter ; Andrew Simmons 
Cc: R help Mailing list 
Subject: Re: [R] print and lapply

[External Email]

Dear Bert,
   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: Bert Gunter 
Sent: Monday, November 7, 2022 11:16 PM
To: Andrew Simmons 
Cc: akshay kulkarni ; R help Mailing list 

Subject: Re: [R] print and lapply

Well... yes, of course. But assuming the sole purpose is to print the results 
and not to save them for further processing, the OP's approach seems rather 
painful. My preference would be to vectorize:

> print(cbind(TP, TPsq =TP^2), print.gap = 3)
   TP   TPsq
[1,]1  1
[2,]2  4
[3,]3  9
[4,]4 16

See ?print.default for details

-- Bert


On Mon, Nov 7, 2022 at 9:20 AM Andrew Simmons  wrote:
>
> put print() around x^2
>
> On Mon, Nov 7, 2022, 12:18 akshay kulkarni  wrote:
>
> > 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://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fst
> > at.ethz.ch%2Fmailman%2Flistinfo%2Fr-helpdata=05%7C01%7Ctebert%4
> > 0ufl.edu%7Cb726a89349ed4860265708dac0eb5d0a%7C0d4da0f84a314d76ace60a
> > 62331e1b84%7C0%7C0%7C638034414365456384%7CUnknown%7CTWFpbGZsb3d8eyJW
> > IjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C300
> > 0%7C%7C%7Csdata=dQk1JPxWI4D0D6%2FSoC8LWw56749ulaFrGQc6affgBeM%3
> > Dreserved=0
> > PLEASE do read the posting guide
> > https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww
> > .r-project.org%2Fposting-guide.htmldata=05%7C01%7Ctebert%40ufl.
> > edu%7Cb726a89349ed4860265708dac0eb5d0a%7C0d4da0f84a314d76ace60a62331
> > e1b84%7C0%7C0%7C638034414365456384%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiM
> > C4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%
> > 7C%7Csdata=KDskHBxmxe0DV9XO7GvkESemUNTq2w%2BT%2Fro%2Bgb4DS2w%3D
> > reserved=0 and provide commented, minimal, self-contained, 
> > reproducible code.
> >
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat
> .ethz.ch%2Fmailman%2Flistinfo%2Fr-helpdata=05%7C01%7Ctebert%40ufl
> .edu%7Cb726a89349ed4860265708dac0eb5d0a%7C0d4da0f84a314d76ace60a62331e
> 1b84%7C0%7C0%7C638034414365456384%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4w
> LjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C
> sdata=dQk1JPxWI4D0D6%2FSoC8LWw56749ulaFrGQc6affgBeM%3Dreserv
> ed=0 PLEASE do read the posting guide 
> https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r
> -project.org%2Fposting-guide.htmldata=05%7C01%7Ctebert%40ufl.edu%
> 7Cb726a89349ed4860265708dac0eb5d0a%7C0d4da0f84a314d76ace60a62331e1b84%
> 7C0%7C0%7C638034414365612626%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwM
> DAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C
> sdata=OxZmpA4ORbfUq0gqM8ebZvK5bQTaiCwo0Y1ekn%2F3zBE%3Dreserved=0
> and provide commented, minimal, self-contained, reproducible code.

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help

Re: [R] print and lapply....

2022-11-07 Thread Bert Gunter
" The lapply() caches the result, and prints the output of the
function in question  immediately after printing the final i. "

I don't believe you understand how lists or lapply works. You seem to
be trying to intuit from empirical behavior. Bad idea, if so. You need
to read the docs or spend time with suitable tutorials.

Consider:
lapply produces a **list,** each component of which is the result of
applying a function, FUN, to the components of its first argument
(here 1:4), which is itself a list. Results are **not** cached in the
sense you seem to think they are. The list object that is produced by
the function is **only** printed if you ask it to be (via cat, print,
or whatever) or, by default, if the result is unassigned. The default
printing can be turned off via the invisible() function.

So:

> res <- lapply(TP, function(x){
+print(x)
+x^2}
+)
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
## Nothing printed, but the results are the components of **list**
res, which can be printed on the console by print(res) or simply:

> res ## note the list syntax below
[[1]]
[1] 1

[[2]]
[1] 4

[[3]]
[1] 9

[[4]]
[1] 16

[[5]]
[1] 25

Now can you explain what happens here:

> invisible(lapply(TP, function(x){
+print(x)
+x^2}
+))
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
> ## results not printed; nothing assigned. Where can you find them? (See below)

or here:

> lapply(TP, function(x){
+print(x)
+x^2}
+)
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[[1]]
[1] 1

[[2]]
[1] 4

[[3]]
[1] 9

[[4]]
[1] 16

[[5]]
[1] 25

## How could you recover the results of the lapply and not just look
at the console printout if the results have not been assigned?
## Hint:  See ?.Last.value

-- Bert

On Mon, Nov 7, 2022 at 11:22 AM akshay kulkarni  wrote:
>
> Dear Rui,
>  THanks for your reply...The point is the loop is a scraping 
> code, and in your examples you have assumed that the body acts on i, the loop 
> variable. Can you adapt your code to JUST PRINT the loop variable i ?
>
> By the by, I think I have stumbled upon the answer: The lapply() caches the 
> result, and prints the output of the function in question  immediately after 
> printing the final i. The i's get printed serially, as the function 
> progresses
>
> > lapply(1:4,function(x){print(x);Sys.sleep(x^2);x^2})
> [1] 1
> [1] 2
> [1] 3
> [1] 4
> [[1]]
> [1] 1
>
> [[2]]
> [1] 4
>
> [[3]]
> [1] 9
>
> [[4]]
> [1] 16
>
> Here x^2 's print only after 4 is printed on the console
>
> tHanks anyways for your reply
>
> THanking you,
> Yours sincerely,
> AKSHAY M KULKARNI
> ____________
> From: Rui Barradas 
> Sent: Tuesday, November 8, 2022 12:24 AM
> To: akshay kulkarni ; R help Mailing list 
> 
> Subject: Re: [R] print and lapply
>
> À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 
> > Sent: Monday, November 7, 2022 11:59 PM
> > To: akshay kulkarni ; R help Mailing list 
> > 
> > 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

Re: [R] print and lapply....

2022-11-07 Thread akshay kulkarni
Dear Rui,
 THanks for your reply...The point is the loop is a scraping 
code, and in your examples you have assumed that the body acts on i, the loop 
variable. Can you adapt your code to JUST PRINT the loop variable i ?

By the by, I think I have stumbled upon the answer: The lapply() caches the 
result, and prints the output of the function in question  immediately after 
printing the final i. The i's get printed serially, as the function 
progresses

> lapply(1:4,function(x){print(x);Sys.sleep(x^2);x^2})
[1] 1
[1] 2
[1] 3
[1] 4
[[1]]
[1] 1

[[2]]
[1] 4

[[3]]
[1] 9

[[4]]
[1] 16

Here x^2 's print only after 4 is printed on the console

tHanks anyways for your reply

THanking you,
Yours sincerely,
AKSHAY M KULKARNI

From: Rui Barradas 
Sent: Tuesday, November 8, 2022 12:24 AM
To: akshay kulkarni ; R help Mailing list 

Subject: Re: [R] print and lapply

�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 
> Sent: Monday, November 7, 2022 11:59 PM
> To: akshay kulkarni ; R help Mailing list 
> 
> 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


[[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.


Re: [R] print and lapply....

2022-11-07 Thread Rui Barradas

À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 
Sent: Monday, November 7, 2022 11:59 PM
To: akshay kulkarni ; R help Mailing list 

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.


Re: [R] print and lapply....

2022-11-07 Thread akshay kulkarni
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 
Sent: Monday, November 7, 2022 11:59 PM
To: akshay kulkarni ; R help Mailing list 

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



[[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.


Re: [R] print and lapply....

2022-11-07 Thread Rui Barradas

À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

__
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.


Re: [R] print and lapply....

2022-11-07 Thread akshay kulkarni
Dear Bert,
   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: Bert Gunter 
Sent: Monday, November 7, 2022 11:16 PM
To: Andrew Simmons 
Cc: akshay kulkarni ; R help Mailing list 

Subject: Re: [R] print and lapply

Well... yes, of course. But assuming the sole purpose is to print the
results and not to save them for further processing, the OP's approach
seems rather painful. My preference would be to vectorize:

> print(cbind(TP, TPsq =TP^2), print.gap = 3)
   TP   TPsq
[1,]1  1
[2,]2  4
[3,]3  9
[4,]4 16

See ?print.default for details

-- Bert


On Mon, Nov 7, 2022 at 9:20 AM Andrew Simmons  wrote:
>
> put print() around x^2
>
> On Mon, Nov 7, 2022, 12:18 akshay kulkarni  wrote:
>
> > 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.
> >
>
> [[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.

[[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.


Re: [R] print and lapply....

2022-11-07 Thread Andrew Simmons
I meant something like:

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

You may wish to add cat("\n") after print(x^2) so that your results
from each iteration are separated.
You may also wish to add invisible() around lapply() if you're not
saving / / using the return list in any way.

On Mon, Nov 7, 2022 at 12:38 PM akshay kulkarni  wrote:
>
> Dear Andrew
>  It doesn't work:
>
> > lapply(TP,function(x){print(x^2)})
> [1] 1
> [1] 4
> [1] 9
> [1] 16
> [[1]]
> [1] 1
>
> [[2]]
> [1] 4
>
> [[3]]
> [1] 9
>
> [[4]]
> [1] 16
>
> Basically, lapply() is implemented by a for loop. So there must be some way 
> right?
>
> tHanking you,
> Yours sincerely,
> AKSHAY M KULKARNI
> 
> From: Andrew Simmons 
> Sent: Monday, November 7, 2022 10:50 PM
> To: akshay kulkarni 
> Cc: R help Mailing list 
> Subject: Re: [R] print and lapply
>
> put print() around x^2
>
> On Mon, Nov 7, 2022, 12:18 akshay kulkarni  wrote:
>
> 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.

__
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.


Re: [R] print and lapply....

2022-11-07 Thread akshay kulkarni
Dear Tim,
   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: Ebert,Timothy Aaron 
Sent: Monday, November 7, 2022 11:07 PM
To: Andrew Simmons ; akshay kulkarni 
Cc: R help Mailing list 
Subject: RE: [R] print and lapply

Another option is use paste() within print()

lapply(TP,function(x){print(paste("x= ",x, " x^2 = ", x^2))})


Tim

-Original Message-
From: R-help  On Behalf Of Andrew Simmons
Sent: Monday, November 7, 2022 12:21 PM
To: akshay kulkarni 
Cc: R help Mailing list 
Subject: Re: [R] print and lapply

[External Email]

put print() around x^2

On Mon, Nov 7, 2022, 12:18 akshay kulkarni  wrote:

> 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://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat
> .ethz.ch%2Fmailman%2Flistinfo%2Fr-helpdata=05%7C01%7Ctebert%40ufl
> .edu%7C28ae59febb2d43ce03b108dac0e46e79%7C0d4da0f84a314d76ace60a62331e
> 1b84%7C0%7C0%7C638034384601744085%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4w
> LjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C
> sdata=1D8FQ07q2NcYL8PaJW84PqdUAte3pZJy8XJuJXENbJ4%3Dreserved
> =0
> PLEASE do read the posting guide
> https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r
> -project.org%2Fposting-guide.htmldata=05%7C01%7Ctebert%40ufl.edu%
> 7C28ae59febb2d43ce03b108dac0e46e79%7C0d4da0f84a314d76ace60a62331e1b84%
> 7C0%7C0%7C638034384601744085%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwM
> DAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C
> sdata=aEeZJ8FHVKGX%2BqOJaKskC1onjqKcON2Ux5cj3MimTGw%3Dreserved=0
> and provide commented, minimal, self-contained, reproducible code.
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-helpdata=05%7C01%7Ctebert%40ufl.edu%7C28ae59febb2d43ce03b108dac0e46e79%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638034384601744085%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7Csdata=1D8FQ07q2NcYL8PaJW84PqdUAte3pZJy8XJuJXENbJ4%3Dreserved=0
PLEASE do read the posting guide 
https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.htmldata=05%7C01%7Ctebert%40ufl.edu%7C28ae59febb2d43ce03b108dac0e46e79%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638034384601744085%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7Csdata=aEeZJ8FHVKGX%2BqOJaKskC1onjqKcON2Ux5cj3MimTGw%3Dreserved=0
and provide commented, minimal, self-contained, reproducible code.

[[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.


Re: [R] print and lapply....

2022-11-07 Thread Bert Gunter
Well... yes, of course. But assuming the sole purpose is to print the
results and not to save them for further processing, the OP's approach
seems rather painful. My preference would be to vectorize:

> print(cbind(TP, TPsq =TP^2), print.gap = 3)
   TP   TPsq
[1,]1  1
[2,]2  4
[3,]3  9
[4,]4 16

See ?print.default for details

-- Bert


On Mon, Nov 7, 2022 at 9:20 AM Andrew Simmons  wrote:
>
> put print() around x^2
>
> On Mon, Nov 7, 2022, 12:18 akshay kulkarni  wrote:
>
> > 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.
> >
>
> [[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.

__
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.


Re: [R] print and lapply....

2022-11-07 Thread akshay kulkarni
Dear Andrew
 It doesn't work:

> lapply(TP,function(x){print(x^2)})
[1] 1
[1] 4
[1] 9
[1] 16
[[1]]
[1] 1

[[2]]
[1] 4

[[3]]
[1] 9

[[4]]
[1] 16

Basically, lapply() is implemented by a for loop. So there must be some way 
right?

tHanking you,
Yours sincerely,
AKSHAY M KULKARNI

From: Andrew Simmons 
Sent: Monday, November 7, 2022 10:50 PM
To: akshay kulkarni 
Cc: R help Mailing list 
Subject: Re: [R] print and lapply

put print() around x^2

On Mon, Nov 7, 2022, 12:18 akshay kulkarni 
mailto:akshay...@hotmail.com>> wrote:
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<mailto: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.

[[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.


Re: [R] print and lapply....

2022-11-07 Thread Ebert,Timothy Aaron
Another option is use paste() within print()

lapply(TP,function(x){print(paste("x= ",x, " x^2 = ", x^2))})


Tim

-Original Message-
From: R-help  On Behalf Of Andrew Simmons
Sent: Monday, November 7, 2022 12:21 PM
To: akshay kulkarni 
Cc: R help Mailing list 
Subject: Re: [R] print and lapply

[External Email]

put print() around x^2

On Mon, Nov 7, 2022, 12:18 akshay kulkarni  wrote:

> 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://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat
> .ethz.ch%2Fmailman%2Flistinfo%2Fr-helpdata=05%7C01%7Ctebert%40ufl
> .edu%7C28ae59febb2d43ce03b108dac0e46e79%7C0d4da0f84a314d76ace60a62331e
> 1b84%7C0%7C0%7C638034384601744085%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4w
> LjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C
> sdata=1D8FQ07q2NcYL8PaJW84PqdUAte3pZJy8XJuJXENbJ4%3Dreserved
> =0
> PLEASE do read the posting guide
> https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r
> -project.org%2Fposting-guide.htmldata=05%7C01%7Ctebert%40ufl.edu%
> 7C28ae59febb2d43ce03b108dac0e46e79%7C0d4da0f84a314d76ace60a62331e1b84%
> 7C0%7C0%7C638034384601744085%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwM
> DAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C
> sdata=aEeZJ8FHVKGX%2BqOJaKskC1onjqKcON2Ux5cj3MimTGw%3Dreserved=0
> and provide commented, minimal, self-contained, reproducible code.
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-helpdata=05%7C01%7Ctebert%40ufl.edu%7C28ae59febb2d43ce03b108dac0e46e79%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638034384601744085%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7Csdata=1D8FQ07q2NcYL8PaJW84PqdUAte3pZJy8XJuJXENbJ4%3Dreserved=0
PLEASE do read the posting guide 
https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.htmldata=05%7C01%7Ctebert%40ufl.edu%7C28ae59febb2d43ce03b108dac0e46e79%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638034384601744085%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7Csdata=aEeZJ8FHVKGX%2BqOJaKskC1onjqKcON2Ux5cj3MimTGw%3Dreserved=0
and provide commented, minimal, self-contained, reproducible code.

__
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.


Re: [R] print and lapply....

2022-11-07 Thread Andrew Simmons
put print() around x^2

On Mon, Nov 7, 2022, 12:18 akshay kulkarni  wrote:

> 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.
>

[[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.