Little useless-useful R functions – Same function names from different packages or namespaces

This little useless-useful R function is not that useless after-all. Many of the R programmers have had some issues with namespaces in R environment and lost some time debugging, that turned out to be due to variable name or function name.

Let’s make a simple example:

c <- 100
a <- c(a,c)

a

So it looks like two variables names “a” and “c”. YeeaNo. “c” is reserved word (means combine) and can easily be used also as a named variable. Which is a programmers fault. But the interesting part is when you want to see what is stored in variable a. To your surprise, the value of variable and the function itself. So, caution when using reserved words for variables names. Another problems are namespaces and same function names among different packages. And you want to know about these incidents, right?

So the following function does just that; if goes through the list of functions for all the installed packages in particular environment and finds duplicates. Giving the R programmer the insights where to be cautious and refer to scope and namespace.

funkyFun <- function(){
     libnames <- installed.packages()[,1]
     #exclude packages: 
     libnames <- libnames[ !(libnames %in% c("rJava","RPostgreSQL","XLConnect","xlsx","xlsxjars")) ]
     df <- data.frame(packname = NULL, funkyName = c(NULL,NULL))
     #for (i in 1:50){
     for (i in 1:length(libnames)){
        com <- paste0("require(", libnames[i],")")
        eval(parse(text= com))
        str <- paste0("package:", libnames[i])
        funk <- (ls(str))
        if (length(funk)==0){
          funk <- ifelse((length(funk)==0)==TRUE, "funkyFun", funk)
        }
        da <- cbind(libnames[i], funk)
        df <- rbind(df, da)
    
     }  
     no_freq <- data.frame(table(df$funk))
     all_duplicated_functions  <- no_freq[no_freq$Freq > 1,]
     all_duplicated_functions_per_package <- df[df$funk %in% no_freq$Var1[no_freq$Freq > 1],]
     return(all_duplicated_functions_per_package)
}

Results of the function is somewhat interesting. In my case, with installed little over 300 packages (yes, it’s not that much), I have managed to find at least 20 functions that share same name and each of them is present in at least two (or more) packages.

And this is just an excerpt from much larger set of duplicate function names among the packages. And also gives you the idea of potentially conflicting packages, as on the example above, caret and base for function Recall. When loading caret (to already existing base function), be cautious when calling such functions.

As always, the code is available at Github.

Happy R-coding and stay healthy!

Tagged with: , , , , , , ,
Posted in Uncategorized
6 comments on “Little useless-useful R functions – Same function names from different packages or namespaces
  1. […] by data_admin [This article was first published on R – TomazTsql, and kindly contributed to R-bloggers]. (You can report issue about the content on this page […]

    Like

  2. […] Tomaz Kastrun has a script for us: […]

    Like

  3. […] article was first published on R – TomazTsql, and kindly contributed to R-bloggers]. (You can report problem in regards to the content material […]

    Like

  4. […] article was first published on R – TomazTsql, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here) […]

    Like

  5. […] article was first published on R – TomazTsql, and kindly contributed to R-bloggers]. (You can report issue about the content on this page […]

    Like

  6. […] article was first published on R – TomazTsql, and kindly contributed to R-bloggers]. (You can report issue about the content on this page […]

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Categories
Follow TomazTsql on WordPress.com
Programs I Use: SQL Search
Programs I Use: R Studio
Programs I Use: Plan Explorer
Programs I use: Scraper API
Rdeči Noski – Charity

Rdeči noski

100% of donations made here go to charity, no deductions, no fees. For CLOWNDOCTORS - encouraging more joy and happiness to children staying in hospitals (http://www.rednoses.eu/red-noses-organisations/slovenia/)

€2.00

Top SQL Server Bloggers 2018
TomazTsql

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Discover

A daily selection of the best content published on WordPress, collected for you by humans who love to read.

Revolutions

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

tenbulls.co.uk

tenbulls.co.uk - attaining enlightenment with the Microsoft Data and Cloud Platforms with a sprinkling of Open Source and supporting technologies!

SQL DBA with A Beard

He's a SQL DBA and he has a beard

Reeves Smith's SQL & BI Blog

A blog about SQL Server and the Microsoft Business Intelligence stack with some random Non-Microsoft tools thrown in for good measure.

SQL Server

for Application Developers

Business Analytics 3.0

Data Driven Business Models

SQL Database Engine Blog

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Search Msdn

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

R-bloggers

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Ms SQL Girl

Julie Koesmarno's Journey In Data, BI and SQL World

R-bloggers

R news and tutorials contributed by hundreds of R bloggers

Data Until I Die!

Data for Life :)

Paul Turley's SQL Server BI Blog

sharing my experiences with the Microsoft data platform, SQL Server BI, Data Modeling, SSAS Design, Power Pivot, Power BI, SSRS Advanced Design, Power BI, Dashboards & Visualization since 2009

Grant Fritchey

Intimidating Databases and Code

Madhivanan's SQL blog

A modern business theme

Alessandro Alpi's Blog

DevOps could be the disease you die with, but don’t die of.

Paul te Braak

Business Intelligence Blog

Sql Server Insane Asylum (A Blog by Pat Wright)

Information about SQL Server from the Asylum.

Gareth's Blog

A blog about Life, SQL & Everything ...

SQLPam's Blog

Life changes fast and this is where I occasionally take time to ponder what I have learned and experienced. A lot of focus will be on SQL and the SQL community – but life varies.

%d bloggers like this: