Loss of column names in dataframe
<p>I'm working on a Shiny app. Part of the user interface involves selecting which of many columns of data to plot using drop down menus, checkboxes, etc. I am using grepl() with the user inputs to whittle down the large dataframe to the single column that winds up being plotted. Specifying this context so you know that I can't do a one-off workaround; I need a robust solution that I can apply to a thousand or more columns of data (before you scold me for having wide data, I'm doing mapping, so I'm working within the confines of a shp file) through a user interface.</p>

<p>It's important that I retain the name of the column being plotted for things like debugging my code, generating the graph title, etc, but in the final stage of trimming, when I get down to just one column left, the column name disappears. </p>

<p>If I do this: </p>

<pre><code>    test &lt;- data.frame(c1 = c(1,2,3), c2 = c(4,5,6))
    trimmed &lt;- test[, grepl("c1", names(test))]
</code></pre>

<p>This is the return. </p>

<blockquote>
  <p>trimmed
  [1] 1 2 3</p>
</blockquote>

<p>Okay, so fine. I'll force the dataframe. Trying this: </p>

<pre><code>    test &lt;- data.frame(c1 = c(1,2,3), c2 = c(4,5,6))
    trimmed &lt;- as.data.frame(test[, grepl("c1", names(test))])
</code></pre>

<p>This is the return:</p>

<blockquote>
  <p>trimmed
    test[, grepl("c1", names(test))]
  1                                1
  2                                2
  3                                3</p>
</blockquote>

<p>Well that column doesn't appear to be named c1 anymore. </p>

<p>Because of the way I'm whittling down the dataframe to a smaller subset, I don't have that column name elsewhere. In other words, I can't just rename the column in my final dataframe because I don't know in advance what that name will be. </p>

<p>How can I write this so that I retain the original "c1" column name?</p>