7

我想通过在 R 中运行命令在 Windows 7 机器上下载并安装 pandoc。这可能吗?

(我知道我可以手动执行此操作,但是当我向学生展示时 - 我可以在 R 代码块中组织的步骤越多 - 越好)

4

1 回答 1

12

简单地下载最新版本的安装程序并从 R 启动它怎么样:

  1. a) 识别 Pandoc 的最新版本并在XML包的帮助下获取 URL:

    library(XML)
    page     <- readLines('http://code.google.com/p/pandoc/downloads/list', warn = FALSE)
    pagetree <- htmlTreeParse(page, error=function(...){}, useInternalNodes = TRUE, encoding='UTF-8')
    url      <- xpathSApply(pagetree, '//tr[2]//td[1]//a ', xmlAttrs)[1]
    url      <- paste('http', url, sep = ':')
    

    b)或者感谢@G.Grothendieck应用一些正则表达式魔法(不需要XML这种方式的包):

    page <- readLines('http://code.google.com/p/pandoc/downloads/list', warn = FALSE)
    pat  <- "//pandoc.googlecode.com/files/pandoc-[0-9.]+-setup.exe"
    line <- grep(pat, page, value = TRUE); m <- regexpr(pat, line)
    url  <- paste('http', regmatches(line, m), sep = ':')
    

    c) 或者,如果您愿意,也可以手动检查最新版本:

    url <- 'http://pandoc.googlecode.com/files/pandoc-1.10.1-setup.exe'
    
  2. 下载文件为binary

    t <- tempfile(fileext = '.exe')
    download.file(url, t, mode = 'wb')
    
  3. 只需从 R 运行它:

    system(t)
    
  4. 安装后删除不需要的文件:

    unlink(t)
    

PS:抱歉,仅在 Windows XP 上测试过

于 2013-02-25T17:12:59.600 回答