:首次安装或使用软件包时运行代码 [英] : Run code first time a package is installed or used

查看:90
本文介绍了:首次安装或使用软件包时运行代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正忙于为对R知识一无所知的客户编写一个程序包.鉴于其复杂的数据结构,我需要在R内建立一个数据库",其中包含从他们从另一个电子表格中获取的大量信息.公司.由于他们无法在其计算机上安装SQL或类似设备(ICT存在一些电源控制问题...),因此我基于特定的目录结构在R中编写了一个仿真程序.现在,我想自动运行此程序,但仅在第一次加载程序包时运行.类似于.First.lib,但随后是.VeryFirst.

I am busy writing a package for a customer with little knowledge about R. Given their complex data structure, I need to set up a "data base" within R containing tons of information obtained from a set of spreadsheets they get from another company. As they can't install SQL or so on their computers (ICT has some power control issues...), I've written an emulation in R, based on a specific directory structure. Now I want to run this automatically, but only the first time the package is loaded. Something like .First.lib, but then .VeryFirst.

关于第一次加载程序包时如何加载一段代码的任何想法?我在手册的任何地方都找不到它,因此欢迎使用所有指针.

Any idea on how to load a piece of code the first time a package is loaded? I couldn't really find it anywhere in the manuals, so all pointers are welcome.

推荐答案

在手册中.

基本上,您有两个代码路径:

Basically you have two code paths:

    没有名称空间的
  1. 软件包可以使用函数.First.lib(),通常来自R/zzz.R

  1. packages without a NAMESPACE can use a function .First.lib(), typically from R/zzz.R

软件包可以使用函数.onLoad(),通常也可以使用R/zzz.R.

packages with a NAMESPACE can use a function .onLoad(), also often from R/zzz.R.

我用它来完成一些技巧,例如在加载时更新包本身(!!).这就要求在实际加载二进制代码之前,不使用NAMESPACE并运行utils::update.packages().

I have used this for tricks like having a package update itself (!!) when loaded. That required not using a NAMESPACE and running utils::update.packages() before actually loading binary code.

这篇关于:首次安装或使用软件包时运行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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