1

I tried to use this resource: How to print R variables in middle of String

and I still couldn't get to what I was looking for.

I'm looking to create a web scraper to run on a weekly basis, that scrapes seven web pages, each with a date. An example website (not real site for this posting) would be: "http://www.stevel.com/log/?xid=2275644&dd=2014-12-20". Therefore, I have seven dates and I want to create seven strings, inserting the dates at the end of the string.

#create dates needed
dates <- seq.Date(as.Date(Sys.Date() - 6) , Sys.Date(), by='days')

dates
[1] "2015-01-15" "2015-01-16" "2015-01-17" "2015-01-18" "2015-01-19" "2015-01-20"
[7] "2015-01-21"

my feeble attempt before creating a loop was trying to generate just one observation:

cat(sprintf("http://www.stevel.com/log/?xid=2275644&dd="\"%s"\, dates[1]))

desired final output would be:

[1] "http://www.stevel.com/log/?xid=2275644&dd=2015-01-15"
"http://www.stevel.com/log/?xid=2275644&dd=2015-01-16"
"http://www.stevel.com/log/?xid=2275644&dd=2015-01-17"
"http://www.stevel.com/log/?xid=2275644&dd=2015-01-18"
"http://www.stevel.com/log/?xid=2275644&dd=2015-01-19"
"http://www.stevel.com/log/?xid=2275644&dd=2015-01-20"
[7] "http://www.stevel.com/log/?xid=2275644&dd=2015-01-21"

the problem i clearly have in R in inserting a date into a string. any help to point me in the right direction would be greatly appreciated

Community
  • 1
  • 1
itjcms18
  • 3,993
  • 7
  • 26
  • 45
  • `cat` will display text to the console or send text to a file but you cannot assign the result to an R name since it returns `NULL`. If you want to do assignment or construction of a function argument, then just use `paste`,`paste0`, or `sprintf` without the `cat`. – IRTFM Jan 22 '15 at 01:36

2 Answers2

5

Try this:

paste0("http://www.stevel.com/log/?xid=2275644&dd=", Sys.Date() - 6:0)

giving this character vector:

[1] "http://www.stevel.com/log/?xid=2275644&dd=2015-01-15"
[2] "http://www.stevel.com/log/?xid=2275644&dd=2015-01-16"
[3] "http://www.stevel.com/log/?xid=2275644&dd=2015-01-17"
[4] "http://www.stevel.com/log/?xid=2275644&dd=2015-01-18"
[5] "http://www.stevel.com/log/?xid=2275644&dd=2015-01-19"
[6] "http://www.stevel.com/log/?xid=2275644&dd=2015-01-20"
[7] "http://www.stevel.com/log/?xid=2275644&dd=2015-01-21"

If you wanted to cat them out on 7 successive lines of the R console rather than output a character vector then if v is the character vector above then any of the following will do:

for(e in v) cat(e, "\n", sep = "")

cat(paste(paste0(v, "\n"), collapse = ""))

cat(paste(v, collapse = "\n"), "\n", sep = "")

or if its not a problem to have an extra space at the end of some lines then sep="" in the first and third above could be omitted. Alternately the gsubfn package has a cat0 function which is the same same as cat except the default sep is "".

In this case the loop seems substantially clearer than the non-loop solutions below it.

G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
2

I think you are tripping yourself up by worrying too much about the quotes. You don't really want quotes so don't try to escape them in the sprintf call:

sprintf("http://www.stevel.com/log/?xid=2275644&dd=%s", dates)
#-----------------
[1] "http://www.stevel.com/log/?xid=2275644&dd=2015-01-15"
[2] "http://www.stevel.com/log/?xid=2275644&dd=2015-01-16"
[3] "http://www.stevel.com/log/?xid=2275644&dd=2015-01-17"
[4] "http://www.stevel.com/log/?xid=2275644&dd=2015-01-18"
[5] "http://www.stevel.com/log/?xid=2275644&dd=2015-01-19"
[6] "http://www.stevel.com/log/?xid=2275644&dd=2015-01-20"
[7] "http://www.stevel.com/log/?xid=2275644&dd=2015-01-21"
IRTFM
  • 258,963
  • 21
  • 364
  • 487