The R function browseURL does nothing when the url is file download

1.3k Views Asked by At

I am trying to open a url in a browser in MAC attempting to save a file. For example, the following link when opened in a browser will download a cvs file from google trends. However if I use the function browseURL, it does not open anything or saves anything.

 URL="http://www.google.com/trends/trendsReport?hl=en-US&q=FTSE 100&date=1%2F2015 1m&cmpt=q&content=1&export=1"
  browseURL(URL)  

it works for webpages like (it open the page on the browser) URL="http://www.google.com" browseURL(URL)

but it doesn't work when it is the csv file of google trends. I tried to replicate the example shown here: http://www.quora.com/What-is-the-way-to-bulk-download-a-CSV-file-from-Google-Trends

Any ideas?

1

There are 1 best solutions below

0
On

Ok found a way! For a reason I could not replicate the code exactly as the Quora post. The reason was in the encoding of the url in my Mac. Maybe it worked for the author who seemed to use Windows but it did not work in my Mac.

It was solved by altering the code in the following way:

# Dates
if(!is.na(year)){
  date <- paste0("&date=", month, "%2F", year, "%20", length, "m")
}

and then

trendsDir <- vector()
for (i in year){
  for (j in 1:12){
    URL <- URL_GT(keyword = seach_word, year = i, month = j, length = 1)
    URL <- gsub(" ", "%20", URL, fixed = TRUE)
  }
}

The URL <- gsub(" ", "%20", URL, fixed = TRUE) made the difference for me.