What is a detach function in R

This recipe explains what is a detach function in R

Recipe Objective

If we want to unload or detach a package from the Rsearch path, we use detach function in R. It is an in-built function in R which also removes the R-objects (such as data.frame) available in the search path after using the attach function. ​

This recipe demonstartes how to use detach function ​

Step 1: loading required library and a dataset

# Data manipulation package library(tidyverse) # reading a dataset customer_seg = read.csv('R_198_Mall_Customers.csv') #summary of the dataset glimpse(customer_seg)
Observations: 200
Variables: 5
$ CustomerID              1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1...
$ Gender                  Male, Male, Female, Female, Female, Female, ...
$ Age                     19, 21, 20, 23, 31, 22, 35, 23, 64, 30, 67, ...
$ Annual.Income..k..      15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 19, ...
$ Spending.Score..1.100.  39, 81, 6, 77, 40, 76, 6, 94, 3, 72, 14, 99,...

Step 2: Detaching a dataframe

We first attach a dataframe and then use detach function to show it's application ​

Syntax: detach(x) ​

where: x = Dataframe or matrix ​

# we first use attach function to bring the dataframe in search Path attach(customer_seg) # Now accessing the variable by its name (Gender) Gender
Male Male Female Female Female Female Female Female Male Female Male Female Female Female Male Male Female Male Male Female Male Male Female Male Female Male Female Male Female Female Male Female Male Male Female Female Female Female Female Female Female Male Male Female Female Female Female Female Female Female Female Male Female Male Female Male Female Male Female Male Male Male Female Female Male Male Female Female Male Female Male Female Female Female Male Male Female Male Female Female Male Male Male Female Female Male Female Female Female Female Female Male Male Female Female Male Female Female Male Male Female Female Male Male Male Female Female Male Male Male Male Female Female Male Female Female Female Female Female Female Male Female Female Male Female Female Male Male Male Male Male Male Female Female Male Female Female Male Male Female Female Male Female Female Male Male Male Female Female Male Male Male Female Female Female Female Male Female Male Female Female Female Male Female Male Female Male Female Female Male Male Male Male Male Female Female Male Male Male Male Female Female Male Female Female Male Female Male Female Female Female Female Male Female Female Female Female Male Male Male
 Levels:
'Female' 'Male'
# detaching a dataframe which was initially attached detach(customer_seg) # Now accessing the variable by its name Gender
Error in eval(expr, envir, enclos): object 'Gender' not found
Traceback:

Note: we cannot work with the variable name as before after using detach function ​

Step 3: Unloading a Package

# we unload the package using the same detach function detach("package:tidyverse") tidyverse::glimpse(customer_seg)
Error: 'glimpse' is not an exported object from 'namespace:tidyverse'
Traceback:

1. tidyverse::glimpse
2. getExportedValue(pkg, name)
3. stop(gettextf("'%s' is not an exported object from 'namespace:%s'", 
 .     name, getNamespaceName(ns)), call. = FALSE, domain = NA)

Note: we cannot access any functions from the tidyverse package after detaching it ​

What Users are saying..

profile image

Gautam Vermani

Data Consultant at Confidential
linkedin profile url

Having worked in the field of Data Science, I wanted to explore how I can implement projects in other domains, So I thought of connecting with ProjectPro. A project that helped me absorb this topic... Read More

Relevant Projects

Build a Autoregressive and Moving Average Time Series Model
In this time series project, you will learn to build Autoregressive and Moving Average Time Series Models to forecast future readings, optimize performance, and harness the power of predictive analytics for sensor data.

Demand prediction of driver availability using multistep time series analysis
In this supervised learning machine learning project, you will predict the availability of a driver in a specific area by using multi step time series analysis.

Deep Learning Project for Text Detection in Images using Python
CV2 Text Detection Code for Images using Python -Build a CRNN deep learning model to predict the single-line text in a given image.

Predictive Analytics Project for Working Capital Optimization
In this Predictive Analytics Project, you will build a model to accurately forecast the timing of customer and supplier payments for optimizing working capital.

GCP MLOps Project to Deploy ARIMA Model using uWSGI Flask
Build an end-to-end MLOps Pipeline to deploy a Time Series ARIMA Model on GCP using uWSGI and Flask

MLOps Project for a Mask R-CNN on GCP using uWSGI Flask
MLOps on GCP - Solved end-to-end MLOps Project to deploy a Mask RCNN Model for Image Segmentation as a Web Application using uWSGI Flask, Docker, and TensorFlow.

Expedia Hotel Recommendations Data Science Project
In this data science project, you will contextualize customer data and predict the likelihood a customer will stay at 100 different hotel groups.

ML Model Deployment on AWS for Customer Churn Prediction
MLOps Project-Deploy Machine Learning Model to Production Python on AWS for Customer Churn Prediction

Classification Projects on Machine Learning for Beginners - 1
Classification ML Project for Beginners - A Hands-On Approach to Implementing Different Types of Classification Algorithms in Machine Learning for Predictive Modelling

Build an End-to-End AWS SageMaker Classification Model
MLOps on AWS SageMaker -Learn to Build an End-to-End Classification Model on SageMaker to predict a patient’s cause of death.