Search code examples
rquotes

Is there a "quote words" operator in R?


Is there a "quote words" operator in R, analogous to qw in Perl? qw is a quoting operator that allows you to create a list of quoted items without having to quote each one individually.

Here is how you would do it without qw (i.e. using dozens of quotation marks and commas):

#!/bin/env perl
use strict;
use warnings;

my @NAM_founders = ("B97",    "CML52",  "CML69", "CML103", "CML228", "CML247",
                    "CML322", "CML333", "Hp301", "Il14H",  "Ki3",    "Ki11",
                    "M37W",   "M162W",  "Mo18W", "MS71",   "NC350",  "NC358"
                    "Oh7B",   "P39",    "Tx303", "Tzi8",
                   );

print(join(" ", @NAM_founders)); # Prints array, with elements separated by spaces

Here's doing the same thing, but with qw it is much cleaner:

#!/bin/env perl
use strict;
use warnings;

my @NAM_founders = qw(B97    CML52  CML69  CML103 CML228 CML247 CML277
                      CML322 CML333 Hp301  Il14H  Ki3    Ki11   Ky21
                      M37W   M162W  Mo18W  MS71   NC350  NC358  Oh43
                      Oh7B   P39    Tx303  Tzi8
                   );

print(join(" ", @NAM_founders)); # Prints array, with elements separated by spaces

I have searched but not found anything.


Solution

  • Try using scan and a text connection:

    qw=function(s){scan(textConnection(s),what="")}
    NAM=qw("B97      CML52    CML69    CML103    CML228   CML247  CML277
                      CML322   CML333   Hp301    Il14H     Ki3      Ki11    Ky21
                      M37W     M162W    Mo18W    MS71      NC350    NC358   Oh43
                      Oh7B     P39      Tx303    Tzi8")
    

    This will always return a vector of strings even if the data in quotes is numeric:

    > qw("1 2 3 4")
    Read 4 items
    [1] "1" "2" "3" "4"
    

    I don't think you'll get much simpler, since space-separated bare words aren't valid syntax in R, even wrapped in curly brackets or parens. You've got to quote them.