0

In the following code

f <- function(nm1, nm2) {
    df1 <- data.frame()
    df2 <- data.frame()
    list(nm1=df1, nm2=df2)
}

res <- f("yo", "there")

the nms are treated as literals.

How can I express during list building that I am using a symbol not a literal?

This confusion between when a literal is intended seems to permeate the language. In the absence of BNF diagrams, if you have figured out a general rule, please share it (but I'm at present only concerned about the narrow question above).

Vrokipal
  • 784
  • 5
  • 18
  • @MrFlick "Dynamic name"? Is that really the right way to ask this question? – Vrokipal Jun 26 '18 at 15:52
  • Are you saying that's not a good term? Maybe "programmatically setting names of arguments" ? But i've seem "dynamically" used in other situations such as [this question](https://stackoverflow.com/q/26003574/2372064). Seems appropriate to me. – MrFlick Jun 26 '18 at 15:55
  • 1
    With the updated example, it's just `setNames(list(df1, df2), c(nm1, nm2))` – MrFlick Jun 26 '18 at 15:56
  • Don't use `setNames()` on the data.frames (that will change the column names). Use them on the list. It's the list that you want to have the names. – MrFlick Jun 26 '18 at 16:07
  • @MrFlick OK, understood. What about the question under "Update"? Is writing list(data.frame()) guaranteed to produce a list that wraps around a data frame, rather than convert to a list, even for a single-column data frame? – Vrokipal Jun 26 '18 at 16:09
  • That seems like a separate question from your original question. Edits should not introduce new questions. You should make a separate post. I'm not even sure I understand what you are asking. Calling `list()` will create a new list, it doesn't do any conversion. (But data frames are really lists anyway.) So i'm not sure what problem you are having or are trying to avoid. A clear separate post on just that topic might make it more clear. – MrFlick Jun 26 '18 at 16:13
  • @MrFlick Never mind debating whether it is a new or an old question. What is the answer!? In my second attempt I used your most recent suggestion. – Vrokipal Jun 26 '18 at 16:18
  • What does "doesn't work" mean to you exactly? You are trying to return an object like `list(yo=data.frame(), there=data.frame())` right? Both solution 2 and 3 do that when you run `f("yo", "there")`. What am I missing? – MrFlick Jun 26 '18 at 19:40
  • 1
    I think this is a duplicate of [Defer expression evaluation without using \`quote\`](https://stackoverflow.com/questions/15039599/defer-expression-evaluation-without-using-quote) – smci Jun 29 '18 at 06:18

0 Answers0