Reads data based on file extension

Reads in .rda files like any other data file would (outputs an object, which you can name whatever you want)

read_smart(x, ...)

read_rda(x)

Arguments

x

Name of file to read.

...

Other args passed on to read-in function, see details for more info.

Value

Info from file returned as R data object.

Details

Here is the function logic

  • csv = readr::read_csv

  • rds = readRDS

  • rda|Rdata = load

  • xlsx = openxlsx::read.xlsx

  • sav = haven::read_sav

  • dta = haven::read_dta

  • tsv = read.delim

  • fst = fst::read_fst

  • dt|data.table = data.table::fred

  • txt|table = read.table

  • html = xml2::read_html

  • xml = xml2::read_xml

  • json = jsonlite::stream_in

  • yaml = yaml::read_yaml

Examples

## save congress data as .rds, .csv, and .rda files saveRDS(congress, "congress.rds")
#> Error in saveRDS(congress, "congress.rds"): object 'congress' not found
save(congress, file = "congress.rda")
#> Error in save(congress, file = "congress.rda"): object ‘congress’ not found
write.csv(congress, "congress.csv", row.names = FALSE)
#> Error in is.data.frame(x): object 'congress' not found
## now read and view each file (c1 <- read_smart("congress.rds"))
#> Warning: cannot open compressed file 'congress.rds', probable reason 'No such file or directory'
#> Error in gzfile(file, "rb"): cannot open the connection
(c2 <- read_smart("congress.csv"))
#> Error: 'congress.csv' does not exist in current working directory ('/Users/kearneymw/R/smartread/docs/reference').
(c3 <- read_smart("congress.rda"))
#> Warning: cannot open compressed file 'congress.rda', probable reason 'No such file or directory'
#> Error in readChar(con, 5L, useBytes = TRUE): cannot open the connection