问题描述
比方说,我的某些用户无法更改其R环境,但是我需要他们能够打开RData文件.这些环境文件需要加载程序包(确切地说是httpuv).我们不在乎包,我们不需要它的功能,我们只需要获取数据即可.有没有一种方法可以强制R在加载RData文件时绕过加载命名空间,或者强制其在源端不依赖命名空间的情况下保存它?谢谢.
Let's say some of my users cannot alter their R environments, but I need them to be able to open up RData files. These environment files require a package to be loaded (httpuv to be exact). We don't care about the package, we don't need its capabilities, we just need to get at the data. Is there a way to either force R to bypass loading namespaces when loading the RData file, or force it to save it without namespace dependencies at the originating end? Thanks.
要复制,请安装Shiny.从Shiny applet中创建一些R对象并将其保存为服务器的文件系统,作为RData文件.将文件复制到未安装Shiny或httpuv软件包的计算机上.即使您保存的实际对象完全是与Shiny或httpuv无关的普通data.frame,也请尝试加载RData文件.
To reproduce, install Shiny. Create and save a some R objects to the server's file system from within a Shiny applet as an RData file. Copy the file over to a computer that doesn't have Shiny or the httpuv package installed. Try loading the RData file, even if the actual objects you saved are completely ordinary data.frames that have nothing to do with Shiny or httpuv.
我在RData上做了strings
,该死的东西充满了对httpuv的引用.软件正在加载文件,然后主动决定不再继续使用内部loadFromConn2()
功能.因此,必须有一种方法使其停止这样做.
I did strings
on the RData, and the damn thing is full of references to httpuv. The software is loading the file and then actively deciding to not continue in the internal loadFromConn2()
function. Therefore there must be a way to make it stop doing so.
推荐答案
@baptiste确实应该赞扬他在评论中与一些常规解决方案的链接,尤其是R CMD INSTALL --fake
技巧,如果他重新发布,我将接受作为答案.这就是为什么我不接受针对我自己造成的具体问题的以下答案,而是在可能对他人有所帮助的情况下发布我的答案.
Really @baptiste should get credit for the link in his comment to some general solutions, especially the R CMD INSTALL --fake
trick, and I will accept that if he reposts it as an answer. That is why I am not accepting the following answer of my own to the specific problem that caused it in my case, but I am posting my answer in case it helps someone else.
我要保存的一些对象是lm
个已装配的对象.那些包含公式/术语对象(由于某种原因至少每个,至少两个),可能是因为它们已经通过stepAIC
了,而这些公式又各自具有环境属性.环境属性为.GlobalEnv
,它可能确实包含某个地方的软件包功能的副本.当我浏览拟合模型内的对象,然后依次浏览那些对象的所有属性内的对象,然后依次浏览那些对象的属性内的对象...并将所有我可以找到的环境属性设置为NULL时,最终,我能够将拟合的模型保存到可以从其他R安装中打开的文件中,而不会出现无法加载名称空间的错误.
Some of the objects I was saving were lm
fitted objects. Those contain formula/terms objects (at least two each, for some reason... maybe because they've been through stepAIC
), and those formulas in turn each have an environment attribute. The environment attribute is .GlobalEnv
which probably does contain copies of package functions someplace. When I dug through the objects inside the fitted models, and then the objects inside all the attributes of those objects, and then the objects inside the attributes of the attributes of those objects... and set every environment attribute I could find to NULL, eventually I was able to save that fitted model to a file that could be opened from a different R installation without getting the error about not being able to load a namespace.
我想我也可以编写一个函数来遍历拟合模型中的对象及其属性,并删除环境,但这听起来很丑陋而且很危险.也许有一种方法可以强制公式和拟合的模型不保留环境,这会更好.目前,我将不保存适合的模型,而是在清理可能在其中找到的所有环境属性之后保存其调用属性.如果那不起作用,我将它们分解为字符串.
I suppose I could also write a function to iterate through the objects within a fitted model, and their attributes, and remove environments but that sounds ugly and dangerous. Maybe there is a way to force formulas and fitted models not to retain environments, and that will be better. For the time being, instead of saving fitted models, I will save their call attributes after scrubbing any environment attributes I might find there. If that doesn't work, I'll deparse them into character strings.
PS:我使用了RDS格式,但尚未使用RData对其进行测试,但是我怀疑问题是在某些属性中保存了评估环境,而与RDS格式无关.对象被保存.如果事实证明这不适用于RData,我将发布更新.
PS: I used the RDS format and haven't yet tested it with RData, but I suspect that the problem was the saving of the evalution environment in some of the attributes, and had nothing to do with the format in which the objects get saved. I'll post an update if it turns out that this doesn't also work with RData.
PPS:我怀疑我不是唯一一个第一次听说R CMD INSTALL --fake
技巧的人,也许这个词应该广为流传...因为在某种程度上,其他R用户没有知道这一点,这仍然是针对R的拒绝服务攻击的明显载体!
PPS: I suspect I'm not the only one here who's hearing about the R CMD INSTALL --fake
trick for the first time, and perhaps the word should be spread about this... because to the extent other R users don't know about it, this remains an obvious vector for denial-of-service attacks against R!
我将接受我自己的答案以摆脱SO自动夸张,但如果他们将我的答案发布为答案,那么我将不接受并接受@baptiste.谢谢.
I will accept my own answer to get rid of the SO auto-nagger, but will unaccept it and accept @baptiste if they make it possible for me to do so by posting it as an answer. Thanks.
这篇关于我可以在绕过加载名称空间的同时加载RData文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!