2

I can run this command in my bash shell. The command makes a request to an API to compress an image, and gets a response from the API without issue:

curl https://my.api.com --user api:mypassword --data-binary @/Users/myUsername/MyDirectoryName\ \(abc\)/test/my/file.png --dump-header /dev/stdout

Within an R script, when I try to run the same command within R's system (docs), like this:

system("curl https://my.api.com --user api:mypassword --data-binary @/Users/myUsername/MyDirectoryName\ \(abc\)/test/my/file.png --dump-header /dev/stdout", intern = T)

I get an error message:

Error: '\(' is an unrecognized escape in character string starting "curl https://my.api.com --user api:mypassword --data-binary @/Users/myUsername/MyDirectoryName\ \("

I do not have control over the directory name, with its whitespace and special characters, e.g. ( and ).

How do I need to change the command string passed to system? If this is a character escaping issue as I think it is, how would I perform the escape?

Thank you

zx8754
  • 52,746
  • 12
  • 114
  • 209
Benjamin
  • 683
  • 1
  • 8
  • 22
  • Related? https://stackoverflow.com/questions/4685737/ignore-escape-characters-backslashes-in-r-strings – zx8754 Jan 31 '19 at 10:18
  • Maybe @zx8754, but I'm new to R so would appreciate some more detail about what you mean. tx – Benjamin Jan 31 '19 at 10:30
  • I think you need to escape "escape characters", or escape "characters that are not escape but used as escape in R", hope that makes sense. I will add "regex" tag, hope that will attract better answers. – zx8754 Jan 31 '19 at 10:45
  • Confused because I can do something like this: system("open '/Users/myUsername/MyDirectoryName\ \(abc\)/test/my/file.png'"). – Benjamin Jan 31 '19 at 13:48

1 Answers1

0

As @zx8754 suspected, escaping escape characters should help. In particular, \ is an escape character, while things like ( don't need to be escaped. So, escaping \ should help:

system("curl https://my.api.com --user api:mypassword --data-binary @/Users/myUsername/MyDirectoryName\\ \\(abc\\)/test/my/file.png --dump-header /dev/stdout", intern = T)

The following shows that R is no longer unhappy about anything and prints the string correctly:

cat("curl https://my.api.com --user api:mypassword --data-binary @/Users/myUsername/MyDirectoryName\\ \\(abc\\)/test/my/file.png --dump-header /dev/stdout")
# curl https://my.api.com --user api:mypassword --data-binary @/Users/myUsername/MyDirectoryName\ \(abc\)/test/my/file.png --dump-header /dev/stdout
Julius Vainora
  • 47,421
  • 9
  • 90
  • 102