Foreign(base)
Foreign()所属R语言包:base
Foreign Function Interface
外部函数接口
译者:生物统计家园网 机器人LoveR
描述----------Description----------
Functions to make calls to compiled code that has been loaded into R.
函数来调用编译的代码已经装入河
用法----------Usage----------
.C(.NAME, ..., NAOK = FALSE, DUP = TRUE, PACKAGE, ENCODING)
.Fortran(.NAME, ..., NAOK = FALSE, DUP = TRUE, PACKAGE, ENCODING)
.External(.NAME, ..., PACKAGE)
.Call(.NAME, ..., PACKAGE)
参数----------Arguments----------
参数:.NAME
a character string giving the name of a C function or Fortran subroutine, or an object of class "NativeSymbolInfo", "RegisteredNativeSymbol" or "NativeSymbol" referring to such a name.
一个字符串给一个C函数或Fortran子程序的名称,或一个类的对象"NativeSymbolInfo","RegisteredNativeSymbol"或"NativeSymbol"指的是这样一个名字。
参数:...
arguments to be passed to the foreign function.
要传递给外部函数的参数。
参数:NAOK
if TRUE then any NA or NaN or Inf values in the arguments are passed on to the foreign function. If FALSE, the presence of NA or NaN or Inf values is regarded as an error.
如果TRUE然后任何NA或NaN或Inf在参数值传递到国外的功能。如果FALSE,NA或NaN或Inf值的存在被视为错误。
参数:DUP
if TRUE then arguments are duplicated before their address is passed to C or Fortran.
如果TRUE然后参数复制之前,他们的地址被传递到C或Fortran。
参数:PACKAGE
if supplied, confine the search for the .NAME to the DLL given by this argument (plus the conventional extension, ".so", ".sl", ".dll", ...). This is intended to add safety for packages, which can ensure by using this argument that no other package can override their external symbols, and also speeds up the search (see "Note"). Use PACKAGE="base" for symbols linked in to R.
如果提供的,限制搜索.NAME这种说法的DLL(加上传统的延伸,.so,.sl,.dll,...)。这是为了补充包的安全,可以确保使用这种说法,没有其他的包可以覆盖其外部符号,也加快了搜索(见“注”)。使用PACKAGE="base"符号链接到R
参数:ENCODING
optional name for an encoding to be assumed for character vectors. See "Details".
编码可选的名字被假定为特征向量。见“详细资料”。
Details
详情----------Details----------
The functions .C and .Fortran can be used to make calls to compiled C and Fortran code.
职能.C和.Fortran可以用来使编译的C和Fortran代码调用。
.Call can be used to call compiled code which makes use of internal R objects, passing the arguments to the C code as a sequence of R objects.
.Call可用于调用编译的代码,这使得利用内部研发对象,传递参数的C代码作为一个R对象序列。
.External can be used to call compiled code that uses R objects in the same way as internal R functions: this allows for a variable number of arguments.
,.External可以用来调用编译的代码,使用同样的方式在内部研发功能的R对象:这允许一个可变数目的参数。
Specifying ENCODING overrides any declared encodings (see Encoding) which are otherwise used to translate to the current locale before passing the strings to the compiled code.
指定ENCODING覆盖任何声明的编码(见Encoding)以其他方式使用之前的字符串传递到编译后的代码翻译到当前语言环境。
These functions are all primitive, and .NAME is always matched to the first argument supplied (which if named must partially match .NAME). The other named arguments follow ... and so cannot be abbreviated. You should avoid using names in the arguments passed to ... that match or partially match .NAME in case the argument handling in .C or .Fortran should change to follow standard argument matching conventions.
这些功能都是原始的,.NAME总是相匹配的第一个参数(如果命名必须部分匹配.NAME)。其他命名参数的...所以不能缩写。你应该避免使用中的参数的名称传递给...匹配或部分匹配.NAME.C或.Fortran应该改变按照标准参数匹配公约的参数处理。
For details about how to write code to use with .Call and .External, see the chapter on “System and foreign language interfaces” in the “Writing R Extensions” manual.
关于如何编写代码来使用.Call和.External“上看到”系统和外国语言接口“,”写R扩展“一章手册的详细信息。
For Windows-specific details on producing the external code, see the “R Installation and Administration” manual.
对于Windows的具体细节上产生的外部代码,看到的“R安装和管理手册”。
值----------Value----------
The functions .C and .Fortran return a list similar to the ... list of arguments passed in, but reflecting any changes made by the C or Fortran code.
职能.C和.Fortran返回一个列表类似...传入的参数列表,但反映的C或Fortran代码所做的任何更改。
.External and .Call return an (arbitrary) R object.
.External和.Call返回一个(任意)R对象。
These calls are typically made in conjunction with dyn.load which links DLLs to R.
这些电话通常是dyn.load哪个环节的DLL到R一起
参数类型----------Argument types----------
The mapping of the types of R arguments to C or Fortran arguments in .C or .Fortran is
C或Fortran.C或.Fortran参数的R参数的类型的映射
C
Ç
int *
INT *
double *
双*
float *
浮动*
Rcomplex *
rcomplex *
int *
INT *
char **
字符**
unsigned char *
无符号字符*
SEXP *
SEXP *
SEXP
SEXP
Numeric vectors in R will be passed as type double * to C (and as double precision to Fortran) unless (i) .C or .Fortran is used, (ii) DUP is true and (iii) the argument has attribute Csingle set to TRUE (use as.single or single). This mechanism is only intended to be used to facilitate the interfacing of existing C and Fortran code.
在R的数值向量将通过类型double *到C(double precision到Fortran),除非:(一).C或.Fortran使用,(II)DUP是真实的,(三)参数属性Csingle设置为TRUE(使用as.single或single)。这一机制的目的只被用来促进现有的C和Fortran代码的接口。
The C type Rcomplex is defined in "Complex.h" as a typedef struct {double r; double i;}. Fortran type double complex is an extension to the Fortran standard, and the availability of a mapping of complex to Fortran may be compiler dependent.
C型Rcomplex在定义Complex.htypedef struct {double r; double i;}。 Fortran的类型double complex是Fortran标准的扩展,一个complex映射到Fortran的可用性可能是编译器的依赖。
Logical values are sent as 0 (FALSE), 1 (TRUE) or INT_MIN = -2147483648 (NA, but only if NAOK = TRUE), and the compiled code should return one of these three values: however non-zero value other than INT_MIN are mapped to TRUE.
逻辑值被发送0(FALSE)1(TRUE)INT_MIN = -2147483648(NA,但只有当NAOK = TRUE ),编译后的代码应该返回这三个值之一:然而INT_MIN映射TRUE的其他非零值。
Note: The C types corresponding to integer and logical are int, not long as in S. This difference matters on most 64-bit platforms, where int is 32-bit and long is 64-bit (but not on 64-bit Windows).
注:C类型对应integer和logical是int不longS中,这种差异在大多数64位平台上的事项,其中int 是32位和long是64位(但不包括在64位Windows)。
Note: The Fortran type corresponding to logical is integer, not logical: the difference matters on some older Fortran compilers.
注:相应的Fortran类型logical是integer不logical:对一些旧的Fortran编译器的重要区别。
The first character string of a character vector is passed as a C character array to Fortran: that string may be usable as character*255 if its true length is passed separately. Only up to 255 characters of the string are passed back. (How well this works, or even if it works at all, depends on the C and Fortran compilers and the platform.)
为C字符数组的一个特征向量的第一个字符串传递到Fortran:该字符串可能是有用的作为character*255如果它的真实长度分别通过。最多只能为255个字符的字符串传回。 (本作品,或即使它的工作原理,所有C和Fortran编译器和平台。)
Missing (NA) string values are passed to .C as the string "NA". As the C char type can represent all possible bit patterns there appears to be no way to distinguish missing strings from the string "NA". If this distinction is important use .Call.
失踪(NA)的字符串值传递到.C字符串“NA”。由于在Cchar类型可以代表所有可能的位模式,似乎是没有办法区分失踪字符串字符串"NA"。如果这种区别是重要的用途.Call。
Functions, expressions, environments and other language elements are passed as the internal R pointer type SEXP. This type is defined in "Rinternals.h" or the arguments can be declared as generic pointers, void *. Lists are passed as C arrays of SEXP and can be declared as void * or SEXP *. Note that you cannot assign values to the elements of the list within the C routine. Assigning values to elements of the array corresponding to the list bypasses R's memory management/garbage collection and will cause problems. Essentially, the array corresponding to the list is read-only. If you need to return S objects created within the C routine, use the .Call interface.
通过内部研发指针类型SEXP函数,表达式,环境和其他语言元素。此类型中定义的Rinternals.h或参数可以声明为通用指针,void *。列表是通过作为SEXPC数组和可以void *或SEXP *声明。请注意,你不能在C例程分配值列表中的元素。列表对应的数组元素赋值绕过R的内存管理/垃圾收集,会引起问题。从本质上讲,是只读的数组对应列表。如果你需要返回在C程序中创建的S对象,使用.Call接口。
R functions can be invoked using call_S or call_R and can be passed lists or the simple types as arguments.
可以使用call_S或call_R“可以通过列表或简单类型作为参数调用R函数。
警告----------Warning----------
DUP=FALSE is dangerous.
DUP=FALSE是危险的。
There are two dangers with using DUP=FALSE.
有两个使用DUP=FALSE的危险。
The first is that if you pass a local variable to .C/.Fortran with DUP=FALSE, your compiled code can alter the local variable and not just the copy in the return list. Worse, if you pass a local variable that is a formal parameter of the calling function, you may be able to change not only the local variable but the variable one level up. This will be very hard to trace.
首先是,如果你通过一个局部变量.C/.FortranDUP=FALSE,你编译的代码可以改变局部变量,而不是只是在返回列表中的副本。更糟糕的是,如果你通过一个局部变量,这是一个正式的参数调用函数,你可能能够改变不仅是局部变量,但变量的一个级别。这将是非常难以追查。
The second is that lists are passed as a single R SEXP with DUP=FALSE, not as an array of SEXP. This means the accessor macros in "Rinternals.h" are needed to get at the list elements and the lists cannot be passed to call_S/call_R. New code using R objects should be written using .Call or .External, so this is now only a minor issue.
二是名单通过一个单一的RSEXPDUP=FALSE,而不是作为SEXP数组。这意味着在存取宏Rinternals.h需要在列表中的元素和名单,不能通过call_S/call_R。新的代码,使用R对象应使用书面.Call或.External,所以这是现在唯一一个小问题。
In addition, character vectors and lists cannot be used with DUP=FALSE.
此外,特征向量,并列出不能使用DUP=FALSE。
It is safe and useful to set DUP=FALSE if you do not change any of the variables that might be affected, e.g.,
它是安全的和有用的设置DUP=FALSE如果你不改变任何可能受到影响的变量,例如,
.C("Cfunction", input=x, output=numeric(10)).
.C("Cfunction", input=x, output=numeric(10))。
In this case the output variable did not exist before the call so it cannot cause trouble. If the input variable is not changed in the C code of Cfunction you are safe.
在这种情况下,输出变量之前并不存在的呼叫,因此它不能带来麻烦。如果输入变量没有被C代码在Cfunction的改变,“你是安全的。
Neither .Call nor .External copy their arguments. You should treat arguments you receive through these interfaces as read-only.
既不.Call也.External复制他们的论据。你应该把你收到通过这些接口为只读参数。
Fortran语言符号名称----------Fortran symbol names----------
All Fortran compilers that can be used to compile R map symbol names to lower case, and so does .Fortran.
所有的Fortran编译器,可以用来编译R映射的符号名称为小写,所以没有.Fortran。
Symbol names containing underscores are not valid Fortran 77 (although they are valid in Fortran 9x). Many Fortran 77 compilers will allow them but may translate them in a different way to names not containing underscores. Such names will often work with .Fortran (since how they are translated is detected when R is built and the information used by .Fortran), but portable code should not use Fortran names containing underscores.
包含下划线符号名称是无效的Fortran 77(虽然他们是在Fortran 9X有效)。许多Fortran 77的编译器将允许他们,但他们以不同的方式的名称不包含下划线可能转化。这样的名字往往会使用.Fortran(因为他们是如何翻译的检测,当R是建造和使用的信息.Fortran),但可移植的代码不应该使用的Fortran名称中含有下划线。
Use .Fortran with care for compiled Fortran 9x code: it may not work if the Fortran 9x compiler used differs from the Fortran compiler used when configuring R, especially if the subroutine name is not lower-case or includes an underscore. It is also possible to use .C and do any necessary symbol-name translation yourself.
使用.Fortran代码编译的Fortran 9X护理:它可能无法正常工作,如果从Fortran编译器的Fortran 9x的编译器使用不同用于配置ŕ,特别是如果子程序名是小写或包含下划线。它也有可能使用.C做任何必要的符号名翻译自己。
外部代码的头文件----------Header files for external code----------
Writing code for use with .External and .Call will need to use internal R structures. If possible use just those defined in "Rinternals.h" and/or the macros in "Rdefines.h", as other header files are not installed and are even more likely to be changed.
写作.External和.Call需要使用内部研发结构的代码。如果可能的话,只是那些定义在Rinternals.h和/或宏在“Rdefines.h,其他头文件没有安装,更可能改变。
注意----------Note----------
If one of these functions is to be used frequently, do specify PACKAGE (to confine the search to a single DLL) or pass name as one of the native symbol objects.
如果这些功能之一是要经常使用,指定PACKAGE(只搜索到一个单一的DLL)或通过name本地符号对象之一。
参考文献----------References----------
The New S Language. Wadsworth & Brooks/Cole. (<code>.C</code> and <code>.Fortran</code>.)
Programming with Data. A Guide to the S Language. Springer. (<code>.Call</code>.)
参见----------See Also----------
dyn.load.
dyn.load。
转载请注明:出自 生物统计家园网(http://www.biostatistic.net)。
注:
注1:为了方便大家学习,本文档为生物统计家园网机器人LoveR翻译而成,仅供个人R语言学习参考使用,生物统计家园保留版权。
注2:由于是机器人自动翻译,难免有不准确之处,使用时仔细对照中、英文内容进行反复理解,可以帮助R语言的学习。
注3:如遇到不准确之处,请在本贴的后面进行回帖,我们会逐渐进行修订。
|