f <- tempfile()
x <- data.frame(a = letters[1:10], b = 1:10, c = 2:11)
# ------------------ Auto-detect row names ------------------
# Write with rownames
utils::write.csv(x, f, row.names = LETTERS[2:11])
# read csv with base library utils
table1 <- utils::read.csv(f, colClasses = c('character', 'character'))
# 4 columns including row names
str(table1)
# read csv via safe_read_csv
table2 <- safe_read_csv(f, colClasses = c('character', 'character'))
# row names are automatically detected, hence 3 columns
# Only first columns are characters, the third column is auto
# detected as numeric
str(table2)
# read table without row names
utils::write.csv(x, f, row.names = FALSE)
table2 <- safe_read_csv(f, colClasses = c('character', 'character'))
# still 3 columns, and row names are 1:nrow
str(table2)
# --------------- Blank data frame when nrow too large ---------------
# instead of raising errors, return blank data frame
safe_read_csv(f, skip = 1000)
Run the code above in your browser using DataLab