2

I am trying to formulate a select statement in my API query by pulling a vector from an excel file.

The vector I am pulling from an excel file is:

X <-c("name", "type", "target")

I am then passing this vector into my API query path as such:

path <- paste0(`url`,`table`,"?$select=",paste(`X`, collapse = ","))

and I get the following:

"https://url/api/data/v8.2/table1?$select=name,type,target"

The desired output I want though is to have my select variables enclosed with quotations like this:

"https://url/api/data/v8.2/table1?$select="name","type","target".

However when I try to add quotations in my paste function, like this:

path <- paste0(`url`,`table`,"?$select=",paste('"',`X`,'"', collapse = ","))

I get the following output:

"https://url/api/data/v8.2/table1?$select=\" name \",\" type \",\" target \""

Does anyone know how I can get my desired output with quotations around each selected variable?

SteveM
  • 213
  • 3
  • 13
  • I'd recommend thinking about what the specific task is and what tools might be designed for it. So in the case of putting a URL together based on some parameters, there are packages for that (and maybe some base or `utils` functions). Or if it's that you need to put together an API call with parameters, you might skip the URL assembly and use something like `httr`. Some discussion [here](https://stackoverflow.com/q/53350738/5325862) – camille Feb 20 '20 at 20:32

1 Answers1

1

We can either use sprintf

sprintf("https://url/api/data/v8.2/table1?$select='%s','%s','%s'", X[1], X[2], X[3])
#[1] "https://url/api/data/v8.2/table1?$select='name','type','target'"

If we have 'n' number of elements in 'X'

s1 <- paste(rep("'%s'", length(X)), collapse=",")
do.call(sprintf, c(fmt = paste0("https://url/api/data/v8.2/table1?$select=", s1), as.list(X) ))
#[1] "https://url/api/data/v8.2/table1?$select='name','type','target'"

or glue

library(glue)
glue("https://url/api/data/v8.2/table1?$select='{X[1]}','{X[2]}', '{X[3]}'")

In the OP's post, if it is a double quote, it is the escape character. We can check with cat

akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thanks for this. In my actual vector there is about 50 observations (I only included 3 in my example). Do you know of a way to apply the quotations without having to explicitly name each string like you have in your example: '{X[1]}','{X[2]}', '{X[3]}' ? – SteveM Feb 20 '20 at 19:49
  • @SteveM you can do `do.call(sprintf, c(fmt = "https://url/api/data/v8.2/table1?$select='%s','%s','%s'", as.list(X)))` – akrun Feb 20 '20 at 20:21
  • Thanks for your prompt responses and help on this. I still have to enter '%s' as many times are there are records though. If I have 50 records in my vector, then I have to have '%s" 50 times in my code line. Anyway to avoid this? – SteveM Feb 20 '20 at 20:46
  • @SteveM that is easier. You can create the format with `paste(rep("'%s'", 50), collapse=",")` – akrun Feb 20 '20 at 20:49
  • Sorry - can you put the whole line of code together. I keep getting errors when I put it all together. Much appreciated!! – SteveM Feb 20 '20 at 21:14
  • Thanks @akrun, that code works. One last thing though. Is there a way to have double quotes instead of single quotes? When I use "`%s'", it gives me 'name', 'type', and 'target' instead of "name", "type", "target". – SteveM Feb 20 '20 at 22:40
  • @SteveM In that case just replace the `'` with `"` and wrap the outer quote with single quote. If you print, it would have escape `\`, but with `cat` it would print correctly – akrun Feb 20 '20 at 22:43
  • @SteveM `s1 <- paste(rep('"%s"', length(X)), collapse=",");cat(do.call(sprintf, c(fmt = paste0("https://url/api/data/v8.2/table1?$select=", s1), as.list(X) )) )# https://url/api/data/v8.2/table1?$select="name","type","target"` – akrun Feb 20 '20 at 22:44
  • Is there a way to write that do.call function with cat to an object? It seems to only print to the console. – SteveM Feb 20 '20 at 22:56
  • @SteveM `cat` can be wrriten to a `file`, i.e. `cat(do.call(sprintf, c(fmt = paste0("https://url/api/data/v8.2/table1?$select=", s1), as.list(X) )), file = "file1.txt" )` – akrun Feb 20 '20 at 22:57
  • Sorry that is me not being clear. I want have that function to be stored in an object variable and not a file. Like.... path <- cat(do.call(sprintf, c(fmt = paste0("https://url/api/data/v8.2/table1?$select=", s1), as.list(X))). When I do that however, it just prints to console and doesn't store the value in "path" – SteveM Feb 20 '20 at 23:02
  • @SteveM If you use double quote, the print method will have that escape, otherwise, you may have to use `cat`. If you check the nchar, it is not changing `nchar("%s")# [1] 2` and `nchar(dQuote("%s"))# [1] 4` and `nchar("'%s'")# [1] 4` - single quote – akrun Feb 20 '20 at 23:05
  • or `nchar(dQuote("%s", FALSE))# [1] 4` > – akrun Feb 20 '20 at 23:06
  • OK ya I see if I only use s1 <- paste(rep(dQuote("%s"), length(variables)), collapse=",") then I get the escape character in my query --- $select=\"clrs_name\",\"clrs_type\",\"clrs_targetlicence\"". If I use s1 <- paste(rep('"%s"', length(variables)), collapse=",") with the double quote wrapped in a single quote, then I need to use cat but it does let me store it in a object to then pass to my query – SteveM Feb 20 '20 at 23:24
  • Here is my full code snippet s1 <- paste(rep(dQuote("%s"), length(variables)), collapse=",") path <- do.call(sprintf, c(fmt = paste0(`url`,`table`,"$select=", paste(rep('"%s"', length(variables)), collapse=",")), as.list(variables))) path Response <- GET(path, authenticate(username, password, "ntlm"), add_headers(.headers = c('Prefer' = 'odata.include-annotations="OData.Community.Display.V1.FormattedValue"'))) – SteveM Feb 20 '20 at 23:25
  • @SteveM Not sure why you are worried about the escape symbol, which is not a real one, it is printing in that format because with `nchar` you don't find that character – akrun Feb 20 '20 at 23:26
  • @SteveM in the `dQuote("%s", FALSE)` would make the option fancyQuotes to FALSE – akrun Feb 20 '20 at 23:34
  • 1
    thanks akrun. I think I was overthinking things. I have it all working now. Thanks for your help! – SteveM Feb 21 '20 at 01:38