Interrupt Namespace Issue in R

There is a namespace issue in R if there is a same name in the existing column name and function argument name. The dplyr::filter function will regard the column name as the target in whatever scene.

Here is an example. The following graph shows the sample data.

screenshot

Suppose we want to filter this table by entry_id and we have a function take entry_id as an argument. The script of this function as shown below:

1
2
3
4
5
6
func <- function(entry_id, df){
entry_select <- df %>%
filter(entry_id == entry_id)

return(entry_select)
}

As shown in the image below, the output will be the whole original df table rather than the filtered table only contains 20987 we sent as the argument. Because the filter(entry_id == entry_id) actually means filter(df$entry_id == df$entry_id) here.

Imgur

To avoid the namespace collision. The argument name of function shouldn’t have the same name as column names in the table to be filter. It can be simply resolved by change entry_id in the function argument to another name which is not in the column names of the table.

The following functions fixed this issue:

1
2
3
4
5
6
func <- function(id, df){
entry_select <- df %>%
filter(entry_id == id)

return(entry_select)
}

The output of updated function looks OK now.

Imgur