Why is there no lubridate:::update function? - r

As said in the title: Why is there no such function? Or in a different way: What is the type of the function? When I type ?update I get something from stats package, but there is a lubridate function as described here on page 7. There also seems to be a lubridate:::update.Date function, but I can't find any explanations for that function.
Backround: I use the function in a package and I only got it to work after I used the Depends: in the decription file. Initially I wanted to use lubridate::update()...

The lubridate package provides the methods lubridate:::update.Date() and lubridate:::update.POSIXt(). Those functions are not exported into the namespace, but I assume that, by means of function overloading, they are invoked when update() is applied to a POSIXor Date object when the lubridate library is loaded.
The help page ?lubridate:::update.POSIXt provides some information concerning the update function within the lubridate package:
Description
update.Date and update.POSIXt return a date with the specified
elements updated. Elements not specified will be left unaltered.
update.Date and update.POSIXt do not add the specified values to the
existing date, they substitute them for the appropriate parts of the
existing date.
Usage
## S3 method for class 'POSIXt'
update(object, ..., simple = FALSE)
The usage section and the examples in the help page indicate that these functions don't need to be addressed individually, as they are called by simply using update() when the lubridate library is loaded.
To inspect these functions one can type, e.g., lubridate:::update.POSIXt in the console (without passing arguments, and without the parentheses).

You need to load the lubridate package:
library(lubridate)
date <- now()
print(date)
new_date <- update(date, year = 2010, month = 1, day = 1)
print(new_date)
Outputs:
"2016-08-04 08:58:08 CEST"
"2010-01-01 08:58:08 CET"

Related

What is '!!' and '::' functions in R? [duplicate]

I am following a tutorial in Rbloggers and found the use of double colons, I looked online, but I couldn't find an explanation for their use.
Here is an example of their use.
df <- dplyr::data_frame(
year = c(2015, NA, NA, NA),
trt = c("A", NA, "B", NA)
)
I understand it creates a data frame but I don't understand their purpose.
As you probably have looked up the help page by now usage of :: helps to access the exact function from that specific package. When you load dplyr you probably got a message as follows..
The following objects are masked from ‘package:base’:
intersect, setdiff, setequal, union
So, for instance, if you would like to use intersect function from dplyr or base package, you need to specify using the :: double colons. Usage will be as follows
mtcars$model <- rownames(mtcars)
first <- mtcars[1:20, ]
second <- mtcars[10:20, ]
dplyr::intersect(first, second)
base::intersect(first, second)
Update: Added additional explanation
Note: The sequence you load libraries determine the preferential access of the specific functions. Developers of different package tend to use same function names. However, when R encounters a function, it runs through the different libraries that particular session has loaded in a sequential manner. You can check the packages in a session by running (.packages())
[1] "tidyr" "data.table" "dplyr" "stats"
[5] "graphics" "grDevices" "utils" "datasets"
[9] "methods" "base"
As you can see in my example session above, tidyr is the last library I loaded, which is r session 1st entry. So, when you use any function in your code , first it is searched in tidyr -> then data.table -> then dplyr and so on, finally the base package is looked up. So, in this process when there is function name overlaps between packages the one which loaded the last masks the previous ones. To avoid this masking, you specify in R code where to look for the function. Hence, here base::intersect, will use the function from base library instead of the dplyr. Alternatively, you can use to avoid loading of complete library. There are positives and negatives with this. Read the links and learn more.
run and check the differences.
Here are some resources for you to get an understanding.
Compare library(), require(), ::
Namespace
There may be multiple functions with the same name in multiple packages. The double colon operator allows you to specify the specific function you want:
package::functionname

R use %within% operator without calling library(lubridate)

I'm writing a function that uses some lubridate functions.
However, I'm not sure how to import the function %within% from lubridate. Usually this is easy, as I just use lubridate::function. When I tried this with the %within% operator, this did not work. Is there any way to use operators from packages without loading the entire package itself?
Yes. The issue here is that %within% is a special symbol that is also a function. In R, pretty much everything is a function. For example, when you do 2 + 2, R actually interprets that as
`+`(2, 2)
Check this section from "Advanced R" by Hadley Wickham to understand this details.
So the most direct answer to your question would be: use the same logic for the %within% function, but escaping the % symbol and directly specifying arguments a and b for the function.
lubridate::`%within%`(a, b)
If you're developing a package yourself, you can document your function that uses %within% using roxygen2. All you need to do is:
#' #title Your Nice Function
#' #description This is a description...
#' #param a Your first param
#' #param b Your second param
#' #importFrom lubridate \%within\%
your_nice_function <- function(a, b) {
...
}
That does it, because you're saying that this function imports the specific function %within% from lubridate, without loading the entire lubridate package.
However, if you just want to use the function inside a script the most "familiar" way possible, maybe the best way is to:
`%within%` <- lubridate::`%within%`
By doing so, you're essentially copying the function to a local variable of the same name.

R apply() format_date

I am a noob trying to troubleshooting an R script written by someone else. The script used to work, but now does not. It is related to apply(), which is apply(X, MARGIN, FUN, ...). That says to me that format_date is supposed to be a function. But the person who wrote this script did not define a function called format_date, and I can't find this function in the libraries that are called in the script. Where do I find format_date?
The reason for this line is that the index of this table is date. But we need a date field to export (and not just date as the index), so we are appending it on.
Here is the line throwing the error:
result$date = apply(rownames(result), 1, format_date) # add in date to dataframe
Here is the error message:
Warning: Ignoring unknown parameters: fill
Saving 7 x 7 in image
Error in apply(rownames(result), 1, format_date) :
object 'format_date' not found
You can comment out or delete line result$date = apply(rownames(result), 1, format_date) from your code.
In older version of rtweet package the function format_date converted datetime Twitter API format to standard datatime objects. Current version of rtweet functions return datetime (like POSIX) objects so there is no need for function like format_date.

What are the double colons (::) in R?

I am following a tutorial in Rbloggers and found the use of double colons, I looked online, but I couldn't find an explanation for their use.
Here is an example of their use.
df <- dplyr::data_frame(
year = c(2015, NA, NA, NA),
trt = c("A", NA, "B", NA)
)
I understand it creates a data frame but I don't understand their purpose.
As you probably have looked up the help page by now usage of :: helps to access the exact function from that specific package. When you load dplyr you probably got a message as follows..
The following objects are masked from ‘package:base’:
intersect, setdiff, setequal, union
So, for instance, if you would like to use intersect function from dplyr or base package, you need to specify using the :: double colons. Usage will be as follows
mtcars$model <- rownames(mtcars)
first <- mtcars[1:20, ]
second <- mtcars[10:20, ]
dplyr::intersect(first, second)
base::intersect(first, second)
Update: Added additional explanation
Note: The sequence you load libraries determine the preferential access of the specific functions. Developers of different package tend to use same function names. However, when R encounters a function, it runs through the different libraries that particular session has loaded in a sequential manner. You can check the packages in a session by running (.packages())
[1] "tidyr" "data.table" "dplyr" "stats"
[5] "graphics" "grDevices" "utils" "datasets"
[9] "methods" "base"
As you can see in my example session above, tidyr is the last library I loaded, which is r session 1st entry. So, when you use any function in your code , first it is searched in tidyr -> then data.table -> then dplyr and so on, finally the base package is looked up. So, in this process when there is function name overlaps between packages the one which loaded the last masks the previous ones. To avoid this masking, you specify in R code where to look for the function. Hence, here base::intersect, will use the function from base library instead of the dplyr. Alternatively, you can use to avoid loading of complete library. There are positives and negatives with this. Read the links and learn more.
run and check the differences.
Here are some resources for you to get an understanding.
Compare library(), require(), ::
Namespace
There may be multiple functions with the same name in multiple packages. The double colon operator allows you to specify the specific function you want:
package::functionname

Lubridate Objects Masked After Loading Data.Table

When I load the data.table package after having already loaded the lubridate package, I get the following error message:
Loading required package: data.table
data.table 1.9.4 For help type: ?data.table
*** NB: by=.EACHI is now explicit. See README to restore previous behaviour.
Attaching package: ‘data.table’
The following objects are masked from ‘package:lubridate’:
hour, mday, month, quarter, wday, week, yday, year
Does anyone know a) what's causing this issue and b) how to prevent these objects within lubridate from being masked?
UPDATE:
The issue associated with the above is that I'm using the quarter function from the lubridate package and, after loading the data.table package, I can no longer do so in the same way.
Specifically, when I run quarter(Date, with_year=TRUE) (where Date is a vector of class = Dates), I now get the following error: Error in quarter(Date, with_year = TRUE) : unused argument (with_year = TRUE).
If I simply, quarter(Date), then I can get the desired output without the attached year. For example, if Date is set as simply May 15, 2015 (today), then quarter(Date) will yield 2 (since we're in the 2nd quarter of 2015), but I'd like it to yield 2015.2, hence the importance of the with_year = TRUE option.
Obviously, I can overcome this by using paste to bind together the year and the output of quarter(Date), but I'd prefer to avoid that work-around.
An object name in a package namespace is masked when a new object is defined with the same name. This can be done by the user assigning the name, or by attaching another package that has an object of the same name.
data.table and lubridate have overlapping function names. If you want the lubridate version to be the default, then the easiest solution is to load data.table first, then load lubridate---thus it will be the data.table versions of these functions that is masked by the "newer" lubridate versions.
library(data.table)
library(lubridate)
Otherwise, the solution is to use :: (as in package::function) to fully specify which version of the function you want to use, for example:
lubridate::quarter(Date, with_year = T)
Another option, which involves a little less typing but is perhaps a little less clear as well, would be to alias the lubridate functions you want in the global environment at the start of your script.
quarter = lubridate::quarter
Any use of quarter() later in the script will use the lubridate version of the function.
Yet another option is the conflicted package, which provides a system for preferring a function from one package. It is a bit more intense and intentional, you should definitely read the documentation before using it, but your script might include something like this:
library(conflicted)
conflict_prefer("quarter", "lubridate")
The package conflicted provides various alternatives and is a good practice to use it while loading libraries to be clear on the masking.
https://github.com/r-lib/conflicted

Resources