Search code examples
rrveststringrmagrittr

Generate an URL? How to build an URL from vector variables?


I pull account data from a database which returns a user_ID and a tag_ID.

  user_ID
> [1] "userId=111111"
  tag_ID
> [1] "tagId=222222"

With these two parameters, it allows me to check how many times the account has been viewed. I get there with a .json link like this:

check_url <- ("URL/account_views.json?userID=111111&tag_ID=222222")

 read_html(check_url)
> [1] {"numVisitsStr":"00203"}

My problem is that I want to build each link url with the proper User_ID and Tag_ID. When using:

url2 <- paste("URL/account_views.json?", "user_ID", "tag_ID", sep = "")

this only provides:

> url2
[1] "URL/account_views.json?user_ID&tag_ID"

The real values do not get parsed. But I need the numbers; not the vector variables

How can I do this?

Thanks for all input!


Solution

  • Try :

    user_ID <- "userId=111111"
    tag_ID <- "tagId=222222"
    paste0("URL/account_views.json?", user_ID, "&", tag_ID)
    #[1] "URL/account_views.json?userId=111111tagId=222222"
    

    Or you can use also use glue which is very convenient for such operations

    glue::glue("URL/account_views.json?{user_ID}&{tag_ID}")
    #URL/account_views.json?userId=111111&tagId=222222
    

    where strings inside {} are treated as R code.