[R] Downloading file with lapply

Santosh Srinivas santosh.srinivas at gmail.com
Fri Oct 15 10:12:03 CEST 2010


Thx.

-----Original Message-----
From: Uwe Ligges [mailto:ligges at statistik.tu-dortmund.de] 
Sent: 15 October 2010 13:11
To: Santosh Srinivas
Cc: r-help at r-project.org
Subject: Re: [R] Downloading file with lapply



On 15.10.2010 09:19, Santosh Srinivas wrote:
> I'm still getting familiar with lapply
>
> I have this date sequence
> x<- seq(as.Date("01-Jan-2010",format="%d-%b-%Y"), Sys.Date(), by=1) #to
> generate series of dates
>
> I want to apply the function for all values of x . so I use lapply (Still
a
> newbie!)
> I wrote this test function
> pFun<- function (x) {
>    print(paste("This is: ",x,sep=""))
>    }
>
>
> When I run it:
>> lapply(x,pFun(x))
 >
> It gives the result correctly.. but end with an error
>
>   Error in match.fun(FUN) :
>    'pFun(x)' is not a function, character or symbol
>
> Question 1: What is the issue here??

You have to pass the function, not to call it already:

lapply(x, pFun)


>
> Now, to the real problem. I wrote a function to cmDownFun(sDate)which is
> working correctly
> If I do cmDownFun (x[6]) .. it works correctly .. (i.e. my function was ok
> this time around!)
>
> Hoewever if I do, lapply (x,cmDownFun(x))
> It fails at 01-Jan-2010 .... this is because of: HTTP status was '404 Not
> Found'
>
> This is OK, because the file does not exist ... but I want to continue for
> the remaining values of x
> lapply(x,try(cmDownFun(x),silent = TRUE)) .. does not work
> I also put the try inside the function itself but does not work either.


Then either use try() in cmDownFun directly or use an anonymous function 
rather than a call, again:

lapply(x, function(x) try(cmDownFun(x),silent = TRUE))

Uwe Ligges


>
>
>
----------------------------------------------------------------------------
> --------------------------
> Thanks R-Helpers. Yes, this is a silly question and it will not be
repeated!
> :-)
>
> ______________________________________________
> 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.



More information about the R-help mailing list