[R] String concatenation, File Path Handling to pass to download.file( ) [backslash in DOS paths]

Vishal Belsare shoot.spam at gmail.com
Mon Oct 15 18:29:22 CEST 2007


Thanks much. Your solution is elegant. My overall scheme is to take
present date, and check whether it is a weekend, if not, then create a
string based on the date, to concatenate into a url link for
download.file( ). The files I need to download have a part which is in
the format: mmddyy. I am working to make myself a system to connect to
exchanges, and download end of day files from their servers. Knowing
that there won't be any files for weekends, I want to omit making
those requests (later I'd add something to skip a list of given
exchange holidays).

So I am making this string from the date, if it isn't a weekend,
concatenating to make the URL, downloading the file, unzipping it and
decrementing the date by a day to repeat the test for weekday and
following action. If to begin with the date is of a weekend, then
'else' should take over and decrement the date. I also have problem
with backslashes in creating the file path.

My crude solution is this (I am just 4-day old into learning/using R):

#begin script

date <- as.POSIXlt(Sys.time())                  #present date

for (i in 1:difftime(as.POSIXlt(Sys.Date()),"2007-10-01"))

if (date$wday != 0 & date$wday != 6)                        #check
whether weekday
        datestr <- as.character(date, "%d%m%y")       #make date
character string
        url <- paste("http://www.bseindia.com/bhavcopy/eq",datestr,"_csv.zip",sep="")
        file <- paste("C:\\Program
        download.file(url, destfile = file, mode = "wb")
        zip.unpack(file, cat("C:\\Program Files\\R\\R-2.5.0\\data\\"))
        date <- date - 86400
 else (date <- date-86400)

#end script

The above URLs would point to Bombay Stock Exchange's data files (if
the script works). Thanks again. The code you already suggested would
help me make the date sequence, but I am not sure how I could extract
the mmddyy string from it. Also, I might end up making a list for
dates extending to about 10 years in the past (~2600 working days), so
I was more keen on not creating one long list.

Vishal Belsare

On 10/15/07, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
> See ?seq.Date, e.g.
>    now <- Sys.Date()
>    dd <- seq(now - 20, now, by = "day")
>    dd[as.POSIXlt(dd)$wday %% 6 != 0]
> and have a look at R News 4/1.
> On 10/15/07, Vishal Belsare <shoot.spam at gmail.com> wrote:
> > date <- as.POSIXlt(Sys.time())                  #present date
> > for (i in 1:difftime(as.POSIXlt(Sys.Date()),"2007-10-01"))
> >        if (date$wday != 0 & date$wday != 6) {print(date);assign("date",
> > (date-86400))} else (assign("date", (date-86400)))
> >
> >
> > I am trying to print dates from present day to a day in the past, but
> > omitting weekends. I am not doing something right, but can't figure
> > out what. Help appreciated!
> >
> >
> > Vishal Belsare
> >

"A human being should be able to change a diaper, plan an invasion,
butcher a hog, conn a ship, design a building, write a sonnet, balance
accounts, build a wall, set a bone, comfort the dying, take orders,
give orders, cooperate, act alone, solve equations, analyze a new
problem, pitch manure, program a computer, cook a tasty meal, fight
efficiently, die gallantly. Specialization is for insects."
~ Robert Heinlein

More information about the R-help mailing list