Google文档中的R代码

Mar*_*o M 5 r google-docs google-docs-api google-drive-api

我知道您可以在Google文档上发布电子表格,然后在R中导入它们.但是,假设我有一个函数或一些我想在R中读取的代码(就像在源函数中一样).例如,您如何阅读存储在Google文档中的所有代码?

https://docs.google.com/document/edit?id=1f11rcVs9AtVcgOyiP0lV04yq9hshVDIPx93lA0ItFvQ

我不认为这已经发表,但它只是举个例子.

基本上我想:

  1. 在R中创建一个函数(为了比较,请参见下面的示例)

  2. 将其上传到Google文档并与拥有该链接的任何人共享,以便我无需通过R或Google文档等登录...

  3. 通过像source()这样的命令读取它,无论我想要什么

我的兴趣不是阅读数据而是阅读功能.当我不在同一台计算机/服务器上时,它会更快地导入我自己的功能.

有任何想法吗?

非常感谢

PS statsmethods.net的函数示例

mysummary <- function(x,npar=TRUE,print=TRUE) {
  if (!npar) {
    center <- mean(x); spread <- sd(x) 
  } else {
    center <- median(x); spread <- mad(x) 
  }
  if (print & !npar) {
    cat("Mean=", center, "\n", "SD=", spread, "\n")
  } else if (print & npar) {
    cat("Median=", center, "\n", "MAD=", spread, "\n")
  }
  result <- list(center=center,spread=spread)
  return(result)
}
Run Code Online (Sandbox Code Playgroud)

A5C*_*2T1 8

对于这种情况,我会建议Github或类似的东西.

推荐Github的几个原因:

  • 版本控制
  • 代码突出显示
  • 合作
  • 能够在Github上开发和托管R包,其他人可以使用该devtools包在R中安装
  • 简单的URL方案,使其易于source()用于加载功能

将功能发布到Github后,可以按如下方式轻松加载:

require(RCurl)
baseURL = c("https://raw.github.com/--username--/--repo-name--/master/")
source(textConnection(getURL(paste0(baseURL, "path-to-scripts/script-name.R"))))
Run Code Online (Sandbox Code Playgroud)

当然,您可以轻松地将其包装到函数中,而不必在每个R会话中键入它,或者尝试devtools包中的某些选项.

如果你这么做了,你可能想学习制作一个R包并在Github上托管它.如果您只需要偶尔执行此操作,则可以考虑使用"github:gist".每个"要点"都分配了一个数字ID,然后可以source_gist()devtools包中轻松获取,如下所示:

source_gist("gist-id-number")
Run Code Online (Sandbox Code Playgroud)

很难击败它,但你必须记下你的功能的ID号,或者把你所有的功能放在一个文件中并记住一个ID号......


建议像Google Docs这样编写代码的原因是,为了这样的目的使用"文字处理器"类型的软件通常是一个可怕的想法,因为它们可能会引入隐藏的字符,使您的文件难以获取.此外,纯文本版本的URL是一个非常模糊的,你将无法记住....

顺便说一下,可以让你连接到您的文章的文件名为"txt"的版本,但我不能给source()它-我有一个文本编辑器打开它,复制并粘贴成R这是因为文件编码似乎是"UTF-8(带BOM)".那里的任何人都有关于如何处理的提示?


Kay*_*Kay 8

使用Dropbox最容易完成 - 请参阅http://thebiobucket.blogspot.co.at/2012/05/source-r-script-from-dropbox.html

像这样:

source("http://dl.dropbox.com/s/c18lcwnnrodsevt/test_dropbox_source.R")
Run Code Online (Sandbox Code Playgroud)

但是你可以用Googledocs来做,比如http://thebiobucket.blogspot.co.at/2011/11/how-to-run-r-scripts-directly-out-of.html#more

library(RCurl)
setwd(tempdir())
destfile = "test_google_docs.txt"
x = getBinaryURL("https://docs.google.com/uc?export=download&id=0B2wAunwURQNsMDViYzllMTMtNjllZS00ZTc4LTgzMzEtNDFjMWQ3MTUzYTRk", followlocation = TRUE, ssl.verifypeer = FALSE)
writeBin(x, destfile, useBytes = TRUE)
source(paste(tempdir(), "/test_google_docs.txt", sep = ""))
Run Code Online (Sandbox Code Playgroud)