如何在R会话中查看R包的内部变量? [英] How to view internal variables of an R package within an R session?

查看:322
本文介绍了如何在R会话中查看R包的内部变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在R会话中查看R包的所有隐藏/内部变量?

How can I view all the hidden/internal variables of an R package within an R session?

通过隐藏/内部变量,我的意思是 R包-外部中描述的变量数据,该数据将存储在软件包源代码的"/pkg/R/sysdata.Rda"文件中.

By hidden/internal variables I mean the sort of variables described in R Packages - External Data which would be stored in the "/pkg/R/sysdata.Rda" file of a package's source code.

我在下面的尝试.这些都不是完全正确的.我觉得我想念一些东西.必须有一种简单的方法来做到这一点,对吧?

My attempts below. None of these are quite right. I feel like I'm missing something. There has to be a straightforward way to do this, right?

当然,我可以下载软件包的源代码和

Of course, I can download the package source and

load(file.path("pkg","R","sysdata.Rda"))

但是那会涉及我的会话之外的工作,这对我来说不起作用.

But that would involve work outside my session, which doesn't work for me.

在RStudio R会话中,我可以做

Within an RStudio R session, I could do

> library(pkg)
> pkg:::

,其中RStudio自动建议在:::之后包含的内容.在建议中,它显示了所有内部变量.这是一个不错的技巧.但是我想要一种更清洁的东西,它不依赖于RStudio的自动建议.

where RStudio auto-suggests what to include after the :::. Among the suggestions, it shows all internal variables. This is a decent hack. But I'd like something a little cleaner, which doesn't depend on RStudio's auto-suggestions.

我注意到

pkg/R/sysdata.rdx
pkg/R/sysdata.rdb

软件包二进制文件中的

.我以为我也许可以阅读这些内容以显示所有内部变量. 如何使用R打开.rdb文件的答案取决于lazyLoad.但是我无法使它正常工作.看来其他人也不是.

in the package binaries. I thought I might be able to read these to display all the internal variables. How to open .rdb file using R has an answer which relies on lazyLoad. But I wasn't able to get this working. And it seems others weren't either.

推荐答案

您可以使用lsasNamespace获取所有功能(导出和未导出):

You can get all of the functions (exported and unexported) with ls and asNamespace:

head(ls(envir = asNamespace('data.table')))
# [1] "-.IDate"        ":="             "[.data.table"   "[.ITime"       
# [5] "[<-.data.table" "[<-.IDate" 

我对您的后一种观点不满意,但我想system.file拥有您想要的. IIRC与软件包一起安装的所有其他物品都应位于此位置.

I'm not positive about your latter point, but I thing system.file has what you want. IIRC anything else that gets installed with the package should be in this location.

head(list.files(system.file(package = 'data.table'), recursive = TRUE))
# [1] "DESCRIPTION" "help"        "html"        "INDEX"       "libs"       
# [6] "LICENSE"   

这篇关于如何在R会话中查看R包的内部变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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