Skip to content

Commit

Permalink
various fixes to plotting
Browse files Browse the repository at this point in the history
  • Loading branch information
leeper committed Jun 16, 2014
1 parent 1273128 commit 3a90c28
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 24 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Date: 2014-06-14
Title: Meme Generation in R
Author: Thomas J. Leeper
Maintainer: Thomas J. Leeper <thosjleeper@gmail.com>
Imports: RCurl, RJSONIO, jpeg
Imports: RCurl, RJSONIO, jpeg, png, tools
Description: Generate memes on a variety of meme generators from R
License: GPL-2
URL: https://github.com/leeper/meme
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ S3method(plot, meme)
S3method(plot, meme_template)

import("jpeg")
import("png")
import("RJSONIO")
importFrom(tools, file_ext)
importFrom(RCurl, curlPerform, basicTextGatherer, basicHeaderGatherer, dynCurlReader, getBinaryURL, curlEscape)
25 changes: 9 additions & 16 deletions R/create_meme.r
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
create_meme <-
function(template, upper, lower, username, password, site = NULL,
function(template, upper = NULL, lower = NULL, username = NULL, password = NULL, site = NULL,
font = NULL, language = NULL, ...){
if(class(template) != 'meme_template' & class(template) == 'character') {
warning("'template' is not of class 'meme_template'. Template assumted to be an image URL and meme will be generated with site 'memecaptain'.")
template <- list(url = template)
site <- 'memecaptain'
} else {
site <- attr(template, 'site')
}
if(site == "imgflip") {
base <- "https://api.imgflip.com/caption_image?"
u <- paste(base, 'template_id=', template$id,
'&username=', username, '&password=', password,
'&text0=',upper, '&text1=', lower,
'&text0=',curlEscape(upper), '&text1=', curlEscape(lower),
if(!is.null(font)) paste('&font=', font))
} else if(site == "memegenerator") {
base <- 'https://version1.api.memegenerator.net/Instance_Create?'
u <- paste(base, 'generatorID=', template$generatorID,
'&username=', username, '&password=', password,
'&text0=',upper, '&text1=', lower,
'&text0=',curlEscape(upper), '&text1=', curlEscape(lower),
if(!is.null(language)) paste('&languageCode=', language))
} else if(site == "memecaptain") {
u <- paste0("http://v1.memecaptain.com/i?",'u=',template,'&t1=',upper,'&t2=',lower)
u <- paste0("http://v1.memecaptain.com/i?",'u=',curlEscape(template$url),'&t1=',curlEscape(upper),'&t2=',curlEscape(lower))
} else {
stop("Only 'imgflip', 'memegenerator', and 'memecaptain' are currently supported!")
}
Expand Down Expand Up @@ -56,18 +59,8 @@ function(template, upper, lower, username, password, site = NULL,
}

plot.meme <- function(x, ...){
if('meme' %in% names(x)) {
j <- readJPEG(x$meme)
} else {
if(attr(x,'site') == 'imgflip')
j <- readJPEG(getBinaryURL(x$url))
else if(attr(x,'site') == 'memegenerator')
j <- readJPEG(x$imageUrl)
else if(attr(x,'site') == 'memecaptain')
stop("If site=='memecaptain', x must supply 'meme' field")
}
par(mar=rep(0,4), mgp=rep(0,3))
plot(NULL, xlim=c(0,1), ylim=c(0,1), xaxs='i', yaxs='i')
j <- .readimage(x)
plot(NULL, xlim=c(0,1), ylim=c(0,1), xaxs='i', yaxs='i', mar=rep(0,4), mgp=rep(0,3), ...)
rasterImage(j, 0,0,1,1)
invisible(x)
}
8 changes: 2 additions & 6 deletions R/get_templates.r
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,8 @@ get_templates <- function(site = "memecaptain", type = NULL, query = NULL, ...){
}

plot.meme_template <- function(x, ...){
if(attr(x,'site') %in% c('imgflip','memecaptain'))
j <- readJPEG(getBinaryURL(x$url))
else if(attr(x,'site') == 'memegenerator')
j <- readJPEG(x$imageUrl)
par(mar=rep(0,4), mgp=rep(0,3))
plot(NULL, xlim=c(0,1), ylim=c(0,1), xaxs='i', yaxs='i')
j <- .readimage(x)
plot(NULL, xlim=c(0,1), ylim=c(0,1), xaxt='n', yaxt='n', xaxs='i', yaxs='i', mar=rep(0,4), mgp=rep(0,3), ...)
rasterImage(j, 0,0,1,1)
invisible(x)
}
17 changes: 17 additions & 0 deletions R/readimage.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.readimage <- function(x){
if('meme' %in% names(x)) {
out <- readJPEG(x$meme)
} else {
if(attr(x,'site') == 'imgflip')
u <- x$url
else if(attr(x,'site') == 'memegenerator')
u <- x$imageUrl
else if(attr(x,'site') == 'memecaptain')
u <- x$url
contents <- getBinaryURL(u)
ext <- tolower(file_ext(u))
readfun <- switch(ext, 'jpg'=readJPEG, 'jpeg'=readJPEG, 'png'=readPNG)
out <- do.call(readfun, list(contents))
}
structure(out, extension=ext)
}
3 changes: 2 additions & 1 deletion man/create_meme.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
\title{Create meme}
\description{Create a meme from a template image}
\usage{
create_meme(template, upper, lower, username, password, site = NULL,
create_meme(template, upper = NULL, lower = NULL,
username = NULL, password = NULL, site = NULL,
font = NULL, language = NULL, ...)
}
\arguments{
Expand Down

0 comments on commit 3a90c28

Please sign in to comment.