try.xts | R Documentation |
Functions to convert objects of arbitrary classes to xts and then back to the original class, without losing any attributes of the original class.
try.xts(x, ..., error = TRUE)
reclass(x, match.to, error = FALSE, ...)
Reclass(x)
x |
Data object to convert. See details for supported types. |
... |
Additional parameters or attributes. |
error |
Error handling option. See Details. |
match.to |
An xts object whose attributes will be copied to the result. |
A simple and reliable way to convert many different objects into a uniform format for use within R.
try.xts()
and reclass()
are functions that enable external developers
access to the reclassing tools within xts to help speed development of
time-aware functions, as well as provide a more robust and seemless end-user
experience, regardless of the end-user's choice of data-classes.
try.xts()
calls as.xts()
internally. See as.xts()
for available xts
methods and arguments for each coercible class. Since it calls as.xts()
,
you can add custom attributes as name = value
pairs in the same way. But
these custom attributes will not be copied back to the original object when
reclass()
is called.
The error
argument can be a logical value indicating whether an error
should be thrown (or fail silently), a character string allowing for custom
error error messages, or a function of the form f(x, ...)
that will be
called if the conversion fails.
reclass()
converts an object created by try.xts()
back to its original
class with all the original attributes intact (unless they were changed
after the object was converted to xts). The match.to
argument allows you
copy the index attributes (tclass
, tformat
, and tzone
) and
xtsAttributes()
from another xts object to the result. match.to
must
be an xts object with an index value for every observation in x
.
Reclass()
is designed for top-level use, where it is desirable to have
the object returned from an arbitrary function in the same class as the
object passed in. Most functions in R are not designed to return objects
matching the original object's class. It attempts to handle conversion and
reconversion transparently but it requires the original object must be
coercible to xts, the result of the function must have the same number of
rows as the input, and the object to be converted/reclassed must be the
first argument to the function being wrapped. Note that this function
hasn't been tested for robustness.
See the accompanying vignette for more details on the above usage.
try.xts()
returns an xts object when conversion is successful.
The error
argument controls the function's behavior when conversion fails.
Reclass()
and reclass()
return the object as its original class, as
specified by the 'CLASS' attribute.
Jeffrey A. Ryan
as.xts()
a <- 1:10
# fails silently, the result is still an integer vector
try.xts(a, error = FALSE)
# control the result with a function
try.xts(a, error = function(x, ...) { "I'm afraid I can't do that." })
z <- zoo(1:10, timeBasedSeq("2020-01-01/2020-01-10"))
x <- try.xts(z) # zoo to xts
str(x)
str(reclass(x)) # reclass back to zoo
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.