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.
ASCII art is a graphic design technique that uses computers for presentation and consists of pictures pieced together from the 95 printable (from a total of 128) characters defined by the ASCII Standard from 1963 and ASCII compliant character sets with proprietary extended characters (beyond the 128 characters of ...
ASCII art is one of the earliest forms of digital art. 'ASCII' is the short form for the 'American Standard Code for Information Interchange'. First published in 1963, the Standard encodes 128 characters, 95 of which are printable.
ASCII art is the process of creating graphics or art with the help of using the ASCII text characters; it is also known as ANSI art, text art, or word art. The graphics are made by beginning from scratch and using the characters to approximate anything or by manually converting an existing image to ASCII characters.
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 //_// ___/
\_)
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:
ascii_request <- httr::GET("http://artii.herokuapp.com/make?text=this_is_your_text&font=ascii___")
ascii_response <- httr::content(ascii_request,as = "text", encoding = "UTF-8")
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With