::p_load(readr) pacman
Read & Export Data
Read a csv file using the tidyverse
packages
readr::read_csv()
is a part of the tidyverse
universe.
You can use this function to read a csv file not only from your local storage, but also from an online source.
<- "https://raw.githubusercontent.com/yuchen-xue/Learn-R-Quarto/main/content/data/Clewer&Scarisbrick2001.csv"
path <- read_csv(path) # use path from above dat
Rows: 12 Columns: 5
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (2): block, cultivar
dbl (3): yield, row, col
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Difference between read.csv()
and read_csv()
Its output of readr::read_csv()
is a tibble
, whereas the native read.csv()
function produces a data.frame
object.
read.csv(path_to_csv) # base R import as data.frame
read_csv(path_to_csv) # load as `tidyverse::tibble`
Suppressing the message regarding column types
When loading the csv file, there might be a message goes like:
Specify the column types or set `show_col_types = FALSE` to quiet this message.
According to this Stack Overflow discussion, you have two options. Either – as suggested by the message – pass the show_col_types = FALSE
argument, or add information about the separator.
<- read_csv(
dat show_col_types = FALSE) path,
<- read_csv(
dat col_types = cols()) path,
Read a Excel .xlsx
Files
Method 1: using readxl::read_excel()
The conventional readxl::read_excel()
function can be used for reading a .xlsx
file from local storage. But it fails to read a file directly from an online source. Thus downloading the data is necessary before reading a .xlsx
file using readxl::read_excel()
function.
::p_load(readxl, httr)
pacman<- "https://raw.githubusercontent.com/yuchen-xue/Learn-R-Quarto/main/content/data/vision_fixed.xlsx"
xlsx_path
# Download the `.xlsx` file as a tempfile and loaded it locally
GET(xlsx_path, write_disk(tf <- tempfile(fileext = ".xlsx")))
Response [https://raw.githubusercontent.com/yuchen-xue/Learn-R-Quarto/main/content/data/vision_fixed.xlsx]
Date: 2025-03-18 21:57
Status: 200
Content-Type: application/octet-stream
Size: 11.5 kB
<ON DISK> /tmp/Rtmp4i1tdn/file16bc4f43ca89.xlsx
<- read_excel(tf, 2L)
xlsx_dat
# Remove the tempfile
unlink(tf)
xlsx_dat
# A tibble: 0 × 0
Method 2: using read.xlsx()
from package openxlsx
read.xlsx()
from package openxlsx
can read a .xlsx
file not only locally, but also directly from the online source. Thus it’s the recommended method of file loading.
::p_load(openxlsx)
pacman<- "https://raw.githubusercontent.com/yuchen-xue/Learn-R-Quarto/main/content/data/vision_fixed.xlsx"
xlsx_path <- read.xlsx(xlsx_path)
xlsx_dat xlsx_dat
Person Ages Gender Civil.state Height Profession Vision Distance
1 Andrés 25 M S 180 Student 10 1.5
2 Anja 29 F S 168 Professional 10 4.5
3 Armando 31 M S 169 Professional 9 4.5
4 Carlos 25 M M 185 Professional 8 6.0
5 Cristina 23 F <NA> 170 Student 10 3.0
6 Delfa 39 F M 158 Professional 6 4.5
7 Eduardo 28 M S 166 Professional 8 4.5
8 Enrique NA <NA> <NA> NA Professional NA 6.0
9 Fanny 25 F M 164 Student 9 3.0
10 Francisco 46 M M 168 Professional 8 4.5
11 Franklin 55 M M 165 Professional 7 4.5
12 Hannele 22 F S 161 Student 10 1.5
13 Henrry 28 M S 168 Professional 10 6.0
14 Iván 29 M S 169 Professional 8 3.0
15 Jaime 23 M M 190 Student 9 1.5
16 Josué 25 M S 175 Student 10 1.5
17 Lorena 26 F M 168 Student 7 3.0
18 Marco 28 M S 175 Professional 10 1.5
19 Maria Augusta 25 F S 163 Student 10 3.0
20 Marina 25 F S 166 Student 10 3.0
21 Marta 47 F S 152 Professional 7 1.5
22 Nora 50 F M 145 Professional 6 6.0
23 Pablo 24 M S 172 Student 9 3.0
24 Pablo 49 M M 165 Professional 6 4.5
25 Patricia 25 F S 152 Student 7 3.0
26 Raúl 26 M S 178 Professional 9 1.5
27 Rolando 26 M M 180 Professional 3 4.5
28 Silvio 27 M S 168 Professional 8 4.5
29 Victor 26 M S 170 Professional 10 1.5
PercDist
1 15.00000
2 45.00000
3 50.00000
4 75.00000
5 30.00000
6 75.00000
7 56.25000
8 NA
9 33.33333
10 56.25000
11 64.28571
12 15.00000
13 60.00000
14 37.50000
15 16.66667
16 15.00000
17 42.85714
18 15.00000
19 30.00000
20 30.00000
21 21.42857
22 100.00000
23 33.33333
24 75.00000
25 42.85714
26 16.66667
27 150.00000
28 56.25000
29 15.00000
Learn more about loading a .xlsx
file from an online resource by following this Stack Overflow discussion.
Write a csv file using the tidyverse
packages
Learn more from this doc.