Search code examples
rascii-art

Including ASCII art in R


I'm writing a small program and wanted to know if there is a way to include ASCII art in R. I was looking for an equivalent of three quotes (""" or ''') in python.

I tried using cat or print with no success.


Solution

  • Unfortunately R can only represent literal strings by using single quotes or double quotes and that makes representing ascii art awkward; however, you can do the following to get a text representation of your art which can be output using R's cat function.

    1) First put your art in a text file:

    # ascii_art.txt is our text file with the ascii art
    # For test purposes we use the output of say("Hello") from cowsay package 
    # and put that in ascii_art.txt
    library(cowsay)
    writeLines(capture.output(say("Hello"), type = "message"), con = "ascii_art.txt")
    

    2) Then read the file in and use dput:

    art <- readLines("ascii_art.txt")
    dput(art)
    

    which gives this output:

    c("", " -------------- ", "Hello ", " --------------", "    \\", 
    "      \\", "        \\", "            |\\___/|", "          ==) ^Y^ (==", 
    "            \\  ^  /", "             )=*=(", "            /     \\", 
    "            |     |", "           /| | | |\\", "           \\| | |_|/\\", 
    "      jgs  //_// ___/", "               \\_)", "  ")
    

    3) Finally in your code write:

    art <- # copy the output of `dput` here
    

    so your code would contain this:

    art <-
    c("", " -------------- ", "Hello ", " --------------", "    \\", 
    "      \\", "        \\", "            |\\___/|", "          ==) ^Y^ (==", 
    "            \\  ^  /", "             )=*=(", "            /     \\", 
    "            |     |", "           /| | | |\\", "           \\| | |_|/\\", 
    "      jgs  //_// ___/", "               \\_)", "  ")
    

    4) Now if we simply cat the art variable it shows up:

    > cat(art, sep = "\n")
    
     -------------- 
    Hello 
     --------------
        \
          \
            \
                |\___/|
              ==) ^Y^ (==
                \  ^  /
                 )=*=(
                /     \
                |     |
               /| | | |\
               \| | |_|/\
          jgs  //_// ___/
                   \_)
      
    

    Added

    This is an addition several years later. In R 4.0 there is a new syntax that makes this even easier. See ?Quotes

     Raw character constants are also available using a syntax similar
     to the one used in C++: ‘r"(...)"’ with ‘...’ any character
     sequence, except that it must not contain the closing sequence
     ‘)"’. The delimiter pairs ‘[]’ and ‘{}’ can also be used, and ‘R’
     can be used in place of ‘r’. For additional flexibility, a number
     of dashes can be placed between the opening quote and the opening
     delimiter, as long as the same number of dashes appear between the
     closing delimiter and the closing quote.
    

    For example:

    hello <- r"{
    
     -------------- 
     Hello 
     --------------
        \
          \
            \
                |\___/|
              ==) ^Y^ (==
                \  ^  /
                 )=*=(
                /     \
                |     |
               /| | | |\
               \| | |_|/\
          jgs  //_// ___/
                   \_)
      
    }"
    cat(hello)
    

    giving:

     -------------- 
     Hello 
     --------------
        \
          \
            \
                |\___/|
              ==) ^Y^ (==
                \  ^  /
                 )=*=(
                /     \
                |     |
               /| | | |\
               \| | |_|/\
          jgs  //_// ___/
                   \_)