I am trying to build a package that contains own S4 class definitions. R check works but issues the following warning: undefined slot classes in definition mySecond.
I set to classes while one slot in the second class is of class myFirstClass. E.g.:
setClass("myFirst",representation(field_one = "character",
field_two = "list")
)
setClass("mySecond",representation(a = "character",
b = "myFirst")
)
Note:Though S4 might be easier to handle I'd like to stick to S4 here, because I am mapping information from a database and prefer S4's stricter definitions in this case. The compilation does work but I try to fulfill CRAN's standards here and want to get rid of the warnings. Btw:
getClassDef gives reasonable result for both classes.
Maybe your class definitions are in separate files, and the collation is such that mySecond is defined before myFirst? See the Collate: field in DESCRIPTION (Section 1.1.1 of RShowDoc("R-exts"), or follow one model of structuring packages where a file AllClasses.R contains class definitions.
Related
i'm trying to do an "node" Class in R, like in java:
but the code pop up an error, "The class node doesn't exist when i'm creating the value "Next="node"
is it not possible to do recursion in a class in R? or how could i do this?
node <- setRefClass("node", fields = list(value="numeric", next="node"))
Error: inesperado '=' in "node <- node <- setRefClass("node", fields = list(value="numeric", next=""
The problem here is that you are using the flow Control word next. Try help(next) and this description comes up
These are the basic control-flow constructs of the R language. They function in much the same way as control statements in any Algol-like language. They are all reserved words.
As such they cannot be used for variable names, and if they are used for list names they should be quoted. Eg. this will work:
setRefClass('node', fields = list(value = 'numeric', 'next' = 'node'))
note that I wrote 'next' and not next
As a side note I would suggest checking out the R6 package, which provides a simpler interface to OOP than reference classes while also being much faster than reference classes.
I'm looking for idea on how to overwrite a function without modify the source. Like if I have foo() in the original source, I want to overwrite it with my own version with the same function name by adding it in a C file, which may also contains other overwrite functions. Sort of like strong/weak compilation. Currently I have to go in the source files and ifdef with __FRAMAC__. I don't want to touch the source files. Is there some kernel option to not use the second instance of foo() function?
Your question does not specify whether you want to replace a function declaration or a function definition. Since they are handled differently by Frama-C, I'm going to detail both.
Duplicate definitions at the kernel level
Currently, at the parsing level, there is no option in Frama-C to completely ignore the definition of a function that is present in one of the files given for parsing. The Frama-C AST will incorporate the definition of all functions it finds.
There is no exact equivalent for strong/weak symbols.
If a second definition for the same function is found, one of the following will happen:
If both definitions occur in the same compilation unit, there is an error.
If each definition happens in a different compilation unit, Frama-C will try to find a plausible solution:
If both occurrences have the same signature, Frama-C will emit a warning such as:
[kernel] b.c:2: Warning:
dropping duplicate def'n of func f at b.c:2 in favor of that at a.c:1
In this case, you just need to ensure the definition you want appears later in the list of sources to be parsed.
If the occurrences have different signatures, but one of the functions is never actually used, you may have a warning such as:
[kernel:linker:drop-conflicting-unused] Warning:
Incompatible declaration for f:
different number of arguments
First declaration was at a.c:1
Current declaration is at b.c:2
Current declaration is unused, silently removing it
However, if both occurrences are used, then you have an error:
[kernel] User Error: Incompatible declaration for f:
different type constructors: int vs. int *
First declaration was at a.c:1
Current declaration is at b.c:2
Duplicate declarations at the kernel level
Considering function declarations, Frama-C will, in accordance with the C standard, accept as many of them as are given, provided they are compatible. If they have ACSL specifications, those specifications will be merged.
Multiple incompatible declarations are handled as before, with warnings or errors depending on whether both versions are used (in which case Frama-C is unable to choose).
Plugin-specific options
Plug-ins may have specific options to override the default behavior of functions in the AST. For instance, Eva has option -eva-use-spec <fns>, which tells the analysis to ignore the definitions of functions <fns>, using only their specifications instead.
I am looking for the real object type of some functions in R, for example, I can not find out the object type of mean function.
> library(pryr)
> otype(mean)
[1] "base"
> ftype(mean)
[1] "s3" "generic"
Sometimes the mean function is S3 and sometimes it is base!
What does ftype tell us?
This function figures out whether the input function is a regular/primitive/internal function, a internal/S3/S4 generic, or a S3/S4/RC method. This is function is slightly simplified as it’s possible for a method from one class to be a generic for another class, but that seems like such a bad idea that hopefully no one has done it.
What does otype give us?
Figure out which object system an object belongs to:
• base: no class attribute
• S3: class attribute, but not S4
• S4: isS4, but not RC
• RC: inherits from "refClass"
For reference:
pryr package documentation
R language objects
I have used the setClass function to define several new classes. But these classes don't appear in my Rstudio environment. How do I see all the classes that exist?
Here is an example:
setClass("geckoNss", representation(absolute = "character", item = "list"))
The class now exists somewhere, as we can do
> getClass("geckoNss")
Class "geckoNss" [in ".GlobalEnv"]
Slots:
Name: absolute item
Class: character list
and make objects of that class:
> new("geckoNss")
An object of class "geckoNss"
Slot "absolute":
character(0)
Slot "item":
list()
Yet, I still do not see the class anywhere. BondedDust's answer suggests that you can only see these classes if you assign them to an object.
So is there no way to even see the default classes R comes with?
http://stat.ethz.ch/R-manual/R-devel/library/methods/html/Classes.html
"When a class is defined, an object is stored that contains the information about that class. The object, known as the metadata defining the class, is not stored under the name of the class (to allow programmers to write generating functions of that name), but under a specially constructed name. To examine the class definition, call getClass. The information in the metadata object includes: "
From the setClass help page, it's stored in the environment where it is created (by default) or in the specified with the "where" argument:
"Create a class definition, specifying the representation (the slots) and/or the classes contained in this one (the superclasses), plus other optional details. As a side effect, the class definition is stored in the specified environment. A generator function is returned as the value of setClass(), suitable for creating objects from the class if the class is not virtual."
After running a setClass call at the console you get an object in the global environment by that name:
> track <- setClass("track",
+ slots = c(x="numeric", y="numeric"))
> ls()
[1] "A" "AE_by_factors" "B"
[4] "dat" "dd" "df"
[7] "final" "hl" "len"
[10] "lm0" "ml" "ml0"
[13] "peas2" "realdata" "temp"
[16] "tolerance" "track" "TravelMode"
[19] "vbin" "vint" "vnum"
> track
class generator function for class “track” from package ‘.GlobalEnv’
function (...)
new("track", ...)
> class(track)
#----------
[1] "classGeneratorFunction"
attr(,"package")
[1] "methods"
Your question originally asked about S4 classes, i.e. the ones created with setClass.. It wasn't at all clear that you wanted to find S3 and what might be called default or implicit classes. They are managed in a different manner. If you want to see all the classes that exist for the print function, just type:
methods(print) # I get 397 different methods at the moment. Each one implies an S3 class.
# a variable number of values will appear depending on which packages ar loaded
Also read the help page for ?methods. Those are each dispatched on the basis of the class attribute. For classes, such as 'numeric', integer, character or 'list' that are implicit but not stored in object class-attributes youyou simply need to know that they were built into the original S language. The S3 dispatch mechanism was actually bolted on to that core S mechanism back in the dawn of time. S3 was part of the language when it was described by "New S Language". I currently see that you can still get used copies at Amazon:
New S Language Paperback – June 30, 1988
by R. A. Becker (Author), J. M. Chambers (Author), Allan R Wilks (Author)
There are other functions that allow you to look at the functions accessible along the search path:
> ?objects
> length(objects())
[1] 85
> length(apropos(what="", mode="function"))
[1] 3431
So on my machine a bit more than 10% of the available functions are print methods.
In R, how do I get the list of subclass of a S4 superclass?
I found showClass("mySuperClass",complete=FALSE) but it only prints the result. I would like to store it in a vector to use it.
Discover the class of a class definition (the class definition is itself an S4 class!)
cls = getClass("MySuperClass")
class(cls)
class?classRepresentation
and, with a little exploring, arrive at
names(cls#subclasses)