title: Accessing (attributes of) a list of variables from a data frame based on a vector
body: <p>I have a (big) data frame with variables which each have a <code>comment</code> attribute.</p>
  
  <pre><code># Basic sample data
  df &lt;- data.frame(a = 1:5, b = 5:1, c = 5:9, d = 9:5, e = 1:5)
  comment(df$a) &lt;- "Some explanation"
  comment(df$b) &lt;- "Some description"
  comment(df$c) &lt;- "etc."
  </code></pre>
  
  <p>I would like to extract the <code>comment</code> attributes for <em>some</em> of those variables, as well as a lit of possible values.</p>
  
  <p>So I start by defining the list of variables I want to extract:</p>
  
  <pre><code>variables_to_extract = c("a", "b", "e")
  </code></pre>
  
  <p>I would normally work on a subset of the data frame, but then I cannot access the attributes (e.g., <code>comment</code>) nor the list of possible values of <em>each variable</em>.</p>
  
  <pre><code>library(tidyverse)
  df %&gt;% select(one_of(variables_to_export)) %&gt;% comment()
  # accesses only the 'comment' attribute of the whole data frame (df), hence NULL
  </code></pre>
  
  <p>I also tried to access through <code>df[[variables_to_export]]</code>, but it generates an error...</p>
  
  <pre><code>df[[variables_to_export]]
  # Error: Recursive Indexing failed at level 2
  </code></pre>
  
  <p>I wanted to extract everything into a data frame, but because of the recursive indexing error, it doesn't work.</p>
  
  <pre><code>meta &lt;- data.frame(variable = variables_to_export,
                     description = comment(papers[[variables_to_export]]),
                     values = papers[[vairables_to_export]] %&gt;% 
                       unique() %&gt;% na.omit() %&gt;% sort() %&gt;% paste(collapse = ", "))
  # Error: Recursive Indexing failed at level 2
  </code></pre>