\HeaderA{isS4}{Test for an S4 object}{isS4}
\aliasA{asS4}{isS4}{asS4}
\aliasA{S4}{isS4}{S4}
\keyword{programming}{isS4}
\begin{Description}\relax
Tests whether the object is an instance of an S4 class.
\end{Description}
\begin{Usage}
\begin{verbatim}
isS4(object)

asS4(object, flag = TRUE, complete = TRUE)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{object}] Any R object. 
\item[\code{flag}] A single logical value; not \code{NA}, whether to turn
the S4 object flag on or off.
\item[\code{complete}] How to complete the transformation to/from S4.  Only
currently used when \code{flag} is \code{FALSE}.  If \code{1}, the object
must convert to a valid S3 object, or an error results.  If \code{2}, a
non-valid S3 object is left alone, silently.  If \code{0}, no conversion
is made other than setting the internal bit (used internally but not recommended, since it can result
in an invalid object).

\end{ldescription}
\end{Arguments}
\begin{Details}\relax
Note that \code{isS4} does not rely on the \pkg{methods}
package, so in particular it can be used to detect the need to
\code{\LinkA{require}{require}} that package.  (But \code{asS4} does depend on
\pkg{methods}.)

Note that the S4 methods for primitive functions do not apply to S3
classes and that the methods for basic classes, such as the vector
types, are sealed.
To apply S4 methods reliably to an S3 object, define an S4 class
that extends the S3 class
(\code{\LinkA{setOldClass}{setOldClass}}).

When \code{asS4} is called with \code{flag == FALSE}, the value of
\code{complete} controls whether an attempt is made to transform
\code{object} into a valid object of the implied S3 class.  If so,
then an object from an S4 class extending an S3 class will be
transformed into an S3 object with the corresponding S3 class (see
\code{\LinkA{S3Part}{S3Part}}).  This includes classes
extending the pseudo-classes \code{array} and \code{matrix}: such
objects will have their class attribute set to \code{NULL}.
\end{Details}
\begin{Value}
\code{isS4} always returns \code{TRUE} or \code{FALSE} according to
whether the internal flag marking an S4 object has been turned on for
this object.

\code{asS4} will turn this flag on or off, according to argument
\code{flag}, and in the latter case complete the conversion as
described under argument \code{complete}. Note that with \code{flag
    FALSE}, an S4 object will \emph{not} but turned into an S3 object
unless there is a valid conversion; that is, an object of type other
than \code{"S4"} for which the S4 object is an extension, unless
argument \code{complete} is \code{0}.
\end{Value}
\begin{SeeAlso}\relax
\code{\LinkA{is.object}{is.object}} for a more general test; \LinkA{Methods}{Methods}
for general information on S4.
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}

isS4(pi) # FALSE
isS4(getClass("MethodDefinition")) # TRUE

\end{ExampleCode}
\end{Examples}

