如何将R对象的定义导出为纯文本,以便其他人可以重新创建它?

时间:2021-05-02 23:16:58

Let's say you have this data in R, and you want to post a question on *. For others to best help you, it would be nice if they could have a copy of your object (dataframe, vector, etc) to work with.

假设你在R中有这个数据,你想在*上发布一个问题。对于其他人来说,最好的帮助是,如果他们可以使用您的对象的副本(dataframe, vector等)。

Let's say your data is in a data frame called site.data

假设数据在一个名为site.data的数据框架中

> site.data
    site year     peak
1  ALBEN    5 101529.6
2  ALBEN   10 117483.4
3  ALBEN   20 132960.9
8  ALDER    5   6561.3
9  ALDER   10   7897.1
10 ALDER   20   9208.1
15 AMERI    5  43656.5
16 AMERI   10  51475.3
17 AMERI   20  58854.4

How do you package it up so that the users can recreate the data exactly as you have it?

如何将其打包,以便用户能够完全按照您的方式重新创建数据?

You want to do this without having people download a text file and import it.

您希望在不让人们下载文本文件并导入它的情况下进行此操作。

(Note: These data subsetted from an example of the REvolutions blog)

(注:这些数据来自革命博客的一个例子)

3 个解决方案

#1


19  

The dput command writes an ASCII representation. If instead of a filename you put "" it will write it to the console

dput命令写入ASCII表示。如果您输入的不是文件名,而是“”,那么它会将其写入控制台

> dput(site.data,"")
structure(list(site = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 
3L, 3L), .Label = c("ALBEN", "ALDER", "AMERI"), class = "factor"), 
    year = c(5L, 10L, 20L, 5L, 10L, 20L, 5L, 10L, 20L), peak = c(101529.6, 
    117483.4, 132960.9, 6561.3, 7897.1, 9208.1, 43656.5, 51475.3, 
    58854.4)), .Names = c("site", "year", "peak"), row.names = c(1L, 
2L, 3L, 8L, 9L, 10L, 15L, 16L, 17L), class = "data.frame")

Just copy the structure and put it after "site.data=" in your example code and people will be able to recreate the data frame exactly as you have it.

只需复制结构,把它放在“站点”后面。data="在您的示例代码中,人们将能够完全重新创建数据框架。

#2


6  

Actually, in your original example, the way you've pasted your data in column format works just fine. I just copied your text from the web page, and did this (using OS X so I have the nice "paste" command):

实际上,在您的原始示例中,以列格式粘贴数据的方式非常好。我只是从网页上复制了你的文字,然后这样做了(使用OS X,所以我有很好的“粘贴”命令):

> site.data <- read.table(pipe("pbpaste"))

For toy data like something posted as a test case, this is often the best approach. To be extra-precise, dput() is good, as dggoldst says.

对于像作为测试用例发布的玩具数据,这通常是最好的方法。正如dggoldst所说,要做到“超精确”,dput()是好的。

#3


3  

Another way, similar to Ken's is using the clipboard (on windows, and possibly linux). I would copy your code and run

另一种类似于Ken的方法是使用剪贴板(在windows上,可能在linux上)。我将复制您的代码并运行

> site.data <- read.table("clipboard", header=T)
> site.data
    site year     peak
1  ALBEN    5 101529.6
2  ALBEN   10 117483.4
3  ALBEN   20 132960.9
8  ALDER    5   6561.3
9  ALDER   10   7897.1
10 ALDER   20   9208.1
15 AMERI    5  43656.5
16 AMERI   10  51475.3
17 AMERI   20  58854.4

#1


19  

The dput command writes an ASCII representation. If instead of a filename you put "" it will write it to the console

dput命令写入ASCII表示。如果您输入的不是文件名,而是“”,那么它会将其写入控制台

> dput(site.data,"")
structure(list(site = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 
3L, 3L), .Label = c("ALBEN", "ALDER", "AMERI"), class = "factor"), 
    year = c(5L, 10L, 20L, 5L, 10L, 20L, 5L, 10L, 20L), peak = c(101529.6, 
    117483.4, 132960.9, 6561.3, 7897.1, 9208.1, 43656.5, 51475.3, 
    58854.4)), .Names = c("site", "year", "peak"), row.names = c(1L, 
2L, 3L, 8L, 9L, 10L, 15L, 16L, 17L), class = "data.frame")

Just copy the structure and put it after "site.data=" in your example code and people will be able to recreate the data frame exactly as you have it.

只需复制结构,把它放在“站点”后面。data="在您的示例代码中,人们将能够完全重新创建数据框架。

#2


6  

Actually, in your original example, the way you've pasted your data in column format works just fine. I just copied your text from the web page, and did this (using OS X so I have the nice "paste" command):

实际上,在您的原始示例中,以列格式粘贴数据的方式非常好。我只是从网页上复制了你的文字,然后这样做了(使用OS X,所以我有很好的“粘贴”命令):

> site.data <- read.table(pipe("pbpaste"))

For toy data like something posted as a test case, this is often the best approach. To be extra-precise, dput() is good, as dggoldst says.

对于像作为测试用例发布的玩具数据,这通常是最好的方法。正如dggoldst所说,要做到“超精确”,dput()是好的。

#3


3  

Another way, similar to Ken's is using the clipboard (on windows, and possibly linux). I would copy your code and run

另一种类似于Ken的方法是使用剪贴板(在windows上,可能在linux上)。我将复制您的代码并运行

> site.data <- read.table("clipboard", header=T)
> site.data
    site year     peak
1  ALBEN    5 101529.6
2  ALBEN   10 117483.4
3  ALBEN   20 132960.9
8  ALDER    5   6561.3
9  ALDER   10   7897.1
10 ALDER   20   9208.1
15 AMERI    5  43656.5
16 AMERI   10  51475.3
17 AMERI   20  58854.4