Skip to content

Instantly share code, notes, and snippets.

Created July 23, 2015 20:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/6a77160913c513e0e973 to your computer and use it in GitHub Desktop.
Save anonymous/6a77160913c513e0e973 to your computer and use it in GitHub Desktop.
# code starts here
 
# install "extrafont". I chose the repo here, too, but you can do it session-wide.
install.packages('extrafont', repos='http://cran.us.r-project.org')
 
library("extrafont")
 
# import font. Yesterday we had to install the font to the system first. Here I specify the paths so R can directly use it without installing it to my linux system. If you don't specify the paths, extrafont will search in the system font directories.
font_import(paths="/home/tklee/Download/fonts", prompt=FALSE,pattern="BRAILLE1.ttf")
# It seems on each system you only need to run this once.
# As long as the TTF file stays where it is, the extrafont package stores the font reference in a local database.
 
# Prepare the data:
Ozone = airquality$Ozone
 
# First, let's plot the histogram on the screen
# Remember yesterday we had to use the following for Windows:
# windowsFonts(A=windowsFont("Braille Normal"))
# hist(Ozone, family="A")
# on my linux machine, I only had to run:
hist(Ozone, family="Braille Normal")
 
# Now, to enable PDF files to use the installed fonts, do the following:
loadfonts(device = "pdf", quiet = TRUE)
# note that "pdf" is the default device. quiet=TRUE just mutes some useless messages. So you can actually just run "loadfonts()".
 
# Specify the output device (PDF), and the file name, of our plot.
pdf("/tmp/Ozone_Histogram.pdf")
# plot the histogram. Because of the command above, the output will be the PDF file instead of the screen
hist(Ozone, family="A")
# now we need to close the device (in this case, finish writing the PDF file)
dev.off()
# the PDF file is there. You can view and print it on the same system, if Braille font is installed.
 
# However, we really want the PDF to have the font embedded, so other systems without Braille font installed can still view/print the same thing.
# This following command embeds the font into the PDF file
embed_fonts("/tmp/Ozone_Histogram.pdf")
 
# Now you have a perfect PDF file that shows the Braille text on any system
 
# code ends here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment