打印工作区中的所有对象 [英] Print all objects in a workspace

查看:62
本文介绍了打印工作区中的所有对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何列出和打印工作区中的所有对象.我想看看他们,了解发生了什么.例如, ls() 给你 30 个对象.除了单独键入它们之外,如何将它们全部显示出来.看起来如此微不足道,解决方案可能会很尴尬.我最接近的是 ls.str() 和循环对象的想法.

I cannot find out how to list and print all objects in a workspace. I'd like to see them all and understand what's going on. For example, ls() gives you 30 objects. How, besides typing them individually, is it possible to display them all. Seems so trivial, the solution will probably quite embarrasing. The closest I've come was ls.str() and the idea of looping over the objects.

编辑:这不适用于数据框.我有一个功能齐全的工作区,没有数据,想了解哪些引用了哪些等等.

推荐答案

你的意思是显示"的意思是对于 ls() 中的每个对象,我想看看我会做什么看看我是否在提示中输入了它"?如果您有一些 1000x10000 的矩阵 - 您仍然想打印它怎么办?我个人喜欢 ls.str() - 我认为它提供了对所有内容的简洁概述,并且很好地处理了我刚刚提到的情况.

Do you mean 'display' in the sense of "for every object in ls(), I want to see what I would see if I typed it into the prompt"? What if you have some matrix that's 1000x10000 - you still want to print it? I personally like ls.str() - I think it gives a nice concise overview of everything, and handles the case I just mentioned nicely.

但是,如果您想基本上显示"每个对象,即在提示上键入每个对象,我建议使用循环:

However if you want to basically "display" every object in the sense of typing each on the prompt, I'd suggest a loop:

for ( obj in ls() ) { print(get(obj)) }

由于 ls() 返回变量名称的字符向量,我需要使用 get(obj) 获取名称在 obj<中的变量/代码>.

Since ls() returns a character vector of variable names, I need to use get(obj) which gets the variable whose name is in obj.

您可能希望对此进行变体以打印变量名称,例如

You may wish to do a variation of this in order to print the variable name too, e.g.

for ( obj in ls() ) { cat('---',obj,'---\n'); print(get(obj)) }

举个例子:

> a <- 1
> b <- LETTERS[1:10]
> c <- data.frame(a=LETTERS[1:10],b=runif(10))
> for ( obj in ls() ) { cat('---',obj,'---\n'); print(get(obj)) }
--- a ---
[1] 1
--- b ---
 [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"
--- c ---
   a         b
1  A 0.1087306
2  B 0.9577797
3  C 0.8995034
4  D 0.1434574
5  E 0.3548047
6  F 0.1950219
7  G 0.1453959
8  H 0.4071727
9  I 0.3324218
10 J 0.4342141

这确实有一个缺点 - 下次您调用 ls() 时,那里现在有一个 obj.不过我确定有一些解决方法.

This does have a drawback though - next time you call ls() there's now an obj in there. I'm sure there's some workaround though.

无论如何,我想我仍然更喜欢 ls.str() 处理大对象的方式(但我使用了很多巨大的(数百万个元素)矩阵,所以这是我的偏好).

Anyhow, I think I still prefer ls.str() for the way it handles big objects (but I work with a lot of huge (millions of elements) matrices, so that's my preference).

这篇关于打印工作区中的所有对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆