Use the readxl package to import contents of an Excel file from your file into a DataFrame
library ("readxl")
xlsx_path = path_to_excel
sheet_name = sheet_to_import
your_df <-read_excel (xlsx_path, sheet_name)
Make sure to install the readxl library (or the tidyverse library) into RStudio or other R development environment before calling it from your script.
Import Excel files into RStudio – Practical Example
Step 1: Import the readxl library
First off, ensure that the readxl library, which is part of Tidyverse, is installed in RStudio, Jupyter or your R dev tool of choice.
if (!require(readxl)) {
install.packages("readxl")
}
Note: If Tidyverse is installed in your environment, you can omit this step.
Step 2: Define the path to your Excel file
Next we will create a variable storing the path and optionally the sheet name/s to import. In our case we will import a simple dataset containing BI courses information.
library ("readxl")
xlsx_path = "C:\\Temp\\Courses_Data.xlsx"
sheet_name = "Courses"
Step 3: Import your Excel sheet
We will now use the read_excel function to import the contents of the workbook and worksheet defined in the previous step:
read_excel (xlsx_path, sheet_name)
The RStudio console will now display the contents of our file.
Step 4: Save your Excel worksheet content as a DataFrame
The read_excel function returns a DataFrame object we can persist as needed and use for analysis and data visualization:
courses_df <- read_excel (xlsx_path, sheet_name)
Step 5: Check the contents of your DataFrame in RStudio
From the Data section in your RStudio Environment Tab, hit your DataFrame name and browse the data.