2

I would like to know how to make R treat an argument as a string even if it does not have quotes. Perhaps with unknown function bar:

foo <- function(x) bar(a))
foo(a abadsf 23)
[1] "a abadsf 23"
Francis Smart
  • 3,875
  • 6
  • 32
  • 58
  • you want to print "a" while there is a variable a defined as "foo" or did I not understand at all ? – T_O Apr 11 '14 at 14:37
  • No, I just wrote `a="foo"` so that it was clear that I did not want to print "foo". Perhaps I should have just given `(function(x) print(bar(a)))(a) [1] "a"` – Francis Smart Apr 11 '14 at 14:39
  • 2
    that's what I said. You want to print "a" HOWEVER there is a variable **a** that is defined as "foo". Is it correct ? – T_O Apr 11 '14 at 14:40
  • 1
    I'm afraid the part inside foo() will produce a syntax error everytime – T_O Apr 11 '14 at 15:17
  • Yes to @T_O. I'm sure there's a duplicate question here that has a full answer why, but I can't seem to find it; anyone else remember it and is having better luck searching? – Aaron left Stack Overflow Apr 11 '14 at 15:21
  • Here are two similar questions, though neither specifically address the spaces issue: http://stackoverflow.com/q/13040120 http://stackoverflow.com/q/5011348 – Aaron left Stack Overflow Apr 11 '14 at 15:23
  • 3
    why exactly do you want to do this? – Dason Apr 11 '14 at 15:38
  • I would like to program a batch of functions that are much less syntactically demanding. For instance `paste()` could be greatly simplified with a syntax `paste(I am a string because it is obvious and the following is my age:, age)`. Is this so crazy? I have a lot of other examples – Francis Smart Apr 11 '14 at 20:29
  • But is having quotes really "syntactically demanding"? And is a string without them really "greatly simplified"? :s If I understand the example you've given, you seem to want the comma to separate your "string" from a variable called age. How do you want the function to know that "age" at the end shouldn't also be printed as "age"? And the function you're proposing presumably also won't accept strings containing commas unless you're escaping them in some way. You'd be introducing a lot of trouble and ambiguity just to evade the use of quotes... – ping Apr 12 '14 at 12:46

2 Answers2

7

Is this doing what you want?

printname <- function(x) deparse(substitute(x))
printname(foo)
[1] "foo"
printname(bar)
[1] "bar"

edit: This probably won't be considered acceptable, depending on where these values are coming from, but to include spaces you could try something like:

print.input = function(y=readline()) return(y)

usage in script:

print.input()
input with spaces
[1] "input with spaces"
ping
  • 1,316
  • 11
  • 14
  • My mistake, it is working for that example but not working generally. `printname(foo foo) Error: unexpected symbol in "printname(foo foo"` – Francis Smart Apr 11 '14 at 15:12
  • @fsmart Sorry, I didn't realise spaces were required at the time. I've added something, but I expect that you have some reason for looking for this capability and that the solution above won't be sufficient for it. – ping Apr 11 '14 at 15:36
  • interesting. thank you for the idea but it is not really what I was going after – Francis Smart Apr 11 '14 at 17:27
1

You can use quote, but this doesn't work with spaces.

deparse(quote(abc))
## [1] "abc"
deparse(quote(abc 1))
## Error: unexpected numeric constant in "deparse(quote(abc 1"
bartektartanus
  • 15,284
  • 6
  • 74
  • 102