R - 将当前ggplot2上传到imgur

时间:2014-11-04 17:45:36

标签: r plot imgur

imguR软件包似乎正是我想要的,但扫描文档使它看起来只是上传文件。我可以上传当前的绘图设备而无需输出到文件吗?具体来说,我有兴趣上传用ggplot2创建的图。这是我试过的:

> token = imgur_login()
> ggplot(dstat, aes(x=obs,y=tcp)) + geom_line() + theme_bw()
> upload_image(token=token)
Error in file.exists(file) : argument "file" is missing, with no default
编辑:我应该补充一下,我很满意一种在后台无形创建文件然后上传它的方法,这只是为了快速展示情节而不是完美呈现

编辑:这是我到目前为止创建的代码。它不能很好地工作,因为似乎在account_albums内部使用的缓存不容易被禁用。任何帮助最终确定这一点将不胜感激

get_or_create_album = function(token) {
  # I can find no way to disable the cache inside
  # account_albums - it always returns the first
  # result unless you login again.
  # So if this function creates the album, then 
  # the user must manually log out and log back in 
  # or this function will happily continue creating the album
  albums = account_albums(ids=F,token=token)
  albums = data.frame(t(sapply(albums,c)))
  album = albums$id[albums$title == "R ggplot2"]
  if (length(album) >= 1)
    return(album[[1]])
  album = create_album(title='R ggplot2',description='Automatic uploads from ggplot2',privacy='public', token=token)
  return(album$id)
}

ggupload = function(token) {
  albumID = get_or_create_album(token)
  x = tempfile(fileext = '.pdf')
  ggsave(filename=x)

  p = last_plot()
  title = ""
  desc = ""
  labels = names(p$labels)
  if ("x" %in% labels && "y" %in% labels)
    title = paste(p$labels$x,"vs",p$labels$y)
    desc = title
  if ("title" %in% labels)
    title = p$labels$title
  imgur_upload(file = x, album = albumID, title=title, description = desc, token = token)
}

这可以如下工作

> token = imgur_login()
> ggplot(dstat, aes(x=obs,y=tcp)) + geom_line() + theme_bw()
> ggupload(token)

0 个答案:

没有答案