How to read password protected excel file in R ?
Last Updated :
18 Jul, 2021
In this article, we are going to see how to read password-protected Excel files in R programming language.
File in use: file
Method 1: Using excel.link Package.
Here we will use excel.link package to read the file with a password.
Installation:
install.packages(“excel.link”)
xl.read.file() function is used to read Excel files in R programming.
Syntax:
xl.read.file( “file_name”, password = “pass”)
Example 1:
R
library ( "excel.link" )
df <- xl.read.file ( "data.xlsx" , password = "gfg@123" )
head (df)
|
Output:
The same module can be used to first unlock the file and then copy its contents onto another so that it can be accessed again without any password. Here we save the file with a password NULL value and save it to another file.
Syntax:
xl.save.file( file.object, “New_file”, password = NULL , write.res.password = NULL)
Program:
R
library ( "excel.link" )
df <- xl.read.file ( "data.xlsx" , password = "gfg@123" )
xl.save.file (df, "Output.xlsx" , password = NULL ,
write.res.password = NULL )
df1 <- xl.read.file ( "Output.xlsx" )
head (df)
|
Output:
Method 2: Using XLConnect package.
Here we will use XLConnect package to read the password-protected file. This package provides comprehensive functionality to read, write and format Excel data.
loadWorkbook() function loads Microsoft Excel workbooks.
Syntax:
loadWorkbook(filename , password )
readWorksheet() function reads data from worksheets.
Syntax: readWorksheet( object, sheet).
Parameters:
- Object: The ‘>workbook to use
- Sheet: The name or index of the worksheet to read from
Program:
R
library (XLConnect)
workbook <- loadWorkbook ( "data.xlsx" , password = "gfg@123" )
df <- readWorksheet (workbook, "sheet1" )
head (df)
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...