0
x <- "y"
assign(x,4)

comment(get(x))<-"this is a comment!"

how can i do something like this? i tried also

comment(x) <-"this is a comment!"

and my others, but it doesn't seem to work. Similar to this question: Access variable value where the name of variable is stored in a string

vrige
  • 297
  • 1
  • 9

2 Answers2

2

Use a temporary variable and then re-assign it:

x <- "y"
assign(x,4)
temp_x <- get(x)
comment(temp_x) <- "this is a comment!"
assign(x, temp_x)
thc
  • 9,527
  • 1
  • 24
  • 39
0
eval(
  substitute(
    expr = comment(temp) <- "this is a comment!",
    env = list(temp = as.name(x))
  )
)

str(y)

Output:

 num 4
 - attr(*, "comment")= chr "this is a comment!"
Nik
  • 355
  • 2
  • 6