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

Ed Godalle

Director Data Analytics at EY / EY Tech
linkedin profile url

I am the Director of Data Analytics with over 10+ years of IT experience. I have a background in SQL, Python, and Big Data working with Accenture, IBM, and Infosys. I am looking to enhance my skills... Read More

Relevant Projects

BigMart Sales Prediction ML Project in Python
The goal of the BigMart Sales Prediction ML project is to build and evaluate different predictive models and determine the sales of each product at a store.

MLOps using Azure Devops to Deploy a Classification Model
In this MLOps Azure project, you will learn how to deploy a classification machine learning model to predict the customer's license status on Azure through scalable CI/CD ML pipelines.

Build an Image Segmentation Model using Amazon SageMaker
In this Machine Learning Project, you will learn to implement the UNet Architecture and build an Image Segmentation Model using Amazon SageMaker

AWS Project to Build and Deploy LSTM Model with Sagemaker
In this AWS Sagemaker Project, you will learn to build a LSTM model on Sagemaker for sales forecasting while analyzing the impact of weather conditions on Sales.

Create Your First Chatbot with RASA NLU Model and Python
Learn the basic aspects of chatbot development and open source conversational AI RASA to create a simple AI powered chatbot on your own.

NLP Project to Build a Resume Parser in Python using Spacy
Use the popular Spacy NLP python library for OCR and text classification to build a Resume Parser in Python.

Time Series Project to Build a Multiple Linear Regression Model
Learn to build a Multiple linear regression model in Python on Time Series Data

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

Deploy Transformer BART Model for Text summarization on GCP
Learn to Deploy a Machine Learning Model for the Abstractive Text Summarization on Google Cloud Platform (GCP)

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.