Including ASCII art in R

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  //_// ___/
               \_)
  

Alternative approach: Use an API

URL - artii

Steps:

  1. Fetch the data ascii_request <- httr::GET("http://artii.herokuapp.com/make?text=this_is_your_text&font=ascii___")
  2. Retrieve the response - ascii_response <- httr::content(ascii_request,as = "text", encoding = "UTF-8")
  3. cat it out - cat(ascii_response)

If not connected to the web, you can set up your own server. Read more here

Thanks to @johnnyaboh for setting up this amazing service

Tags:

R

Ascii Art