Filtering using a comma separated list
<p>I've been developing a dynamic data frame in R Shiny, which is filtered depending on a number of user inputs (checkboxes, radio buttons, that kind of thing).  For one of the options, I am trying to filter the data frame based on a vector of distinct strings, though in the data frame they are stored as a single comma separated string.</p>

<pre><code>         [,1]    [,2]                      
[1,] "Sam"   "C, Python, VB"           
[2,] "Jess"  "VB6, R"                  
[3,] "Mike"  "Matlab, Java, Javascript"
[4,] "Sarah" "Python"                  
[5,] "Ahmad" "HTML, Ruby" 
</code></pre>

<p>Using this simplified example, I have a set of names and the programming languages they know.  I want the dataframe filtered to only include those who know Python or R.  My checkboxes pass the selections as a vector in the form</p>

<pre><code>selections &lt;-    c("Python", "R")
</code></pre>

<p>The result of filtering the dataframe with the selection vector should be</p>

<pre><code>           [,1]    [,2]                      
[1,] "Sam"   "C, Python, VB"           
[2,] "Jess"  "VB6, R"                  
[3,] "Sarah" "Python"  
</code></pre>

<p>Getting the filter to work is my main issue.</p>

<p>I've been converting the languages strings into vectors using </p>

<pre><code>strsplit(as.character(df[,2]), split=", ")
</code></pre>

<p>And I can filter if the selection vector matches the dataframe's exactly, but not if it's only partially similar.</p>

<p>I was trying something along the lines of</p>

<pre><code>df %&gt;% 
      filter(Languages %in% selections)
</code></pre>

<p>But to no avail.  I also looked at using for loops, but those make me feel uneasy in R, like I'm angering the Gods of Vectorisation.</p>

<p>Is there something I'm missing?</p>

<p><strong>Edit 1</strong>
Following lmo's advice using regular expressions, I managed to get it working.</p>

<p>Having "C++" as one of the selections caused some problems, so I had to replace it with an escaped version.</p>

<pre><code>selections &lt;- replace(selections, selections=="C++", "C\\+\\+")
</code></pre>

<p>I then collapsed selections into a | separated string for use in the regular expression </p>

<pre><code>selections &lt;- paste(selections, collapse = '|')
</code></pre>

<p>Finally, I put it into the lmo's suggestion</p>

<pre><code>df &lt;- df[grep(paste0("(^|, )(",
                               selections,
                               ")(,|$)"),
                  df$Programming.Languages),]
</code></pre>

<p>I'll put the actual code I used for the reactive Shiny expression, if I can figure out how to make it more compact (<a href="https://meta.stackoverflow.com/questions/302078/collapsible-code-markup">like in</a>)</p>

<p><strong>Edit 2</strong><br>
Works with an HTML snippit (doen't run though)
<div class="snippet" data-lang="js" data-hide="true" data-console="true" data-babel="false">
<div class="snippet-code snippet-currently-hidden">
<pre class="snippet-code-html lang-html prettyprint-override"><code>  # Filter the courses based on the language checkboxes
  ## A regular expression is used to filter

  courseData &lt;- reactive({
df &lt;- courses
# The +'s in C++ cause a problem in the regex, so it is replaced with an escaped version
selections &lt;- replace(input$languages, input$languages=="C++", "C\\+\\+")
selections = paste(selections, collapse = '|')

# This selects courses in the data frame who offer the selected languages
## grep returns the row numbers
df &lt;- df[grep(paste0("(^|, )(",
                           selections,
                           ")(,|$)"),
              df$Programming.Languages.Taught),]
  })
  
## The selections are made by selecting checkboxes, and the reactive expression is used later in the program</code></pre>
</div>
</div>
</p>