rcsvhttrzuora

httr CSV Content reading as integer instead of double


I'm trying to import a csv using httr from an online source, everything is fine except for it's reading a column as integer when it should be a double resulting in those values to show up as NA

I'm using and getting the below issue.

getdata <- GET(paste("https://rest.zuora.com/v1/files/",r$FileId, sep = ''), auth) invoice <- content(getdata, type = "text/csv")

  Parsed with column specification:
cols(
  Account.external_id__c = col_integer(),
  Invoice.Amount = col_double(),
  Invoice.Balance = col_integer(),
  Invoice.CreatedDate = col_datetime(format = "")
)
Warning: 171 parsing failures.
 row             col               expected actual
2475 Invoice.Balance no trailing characters    .4 
2726 Invoice.Balance no trailing characters    .71
3197 Invoice.Balance no trailing characters    .3 
3287 Invoice.Balance no trailing characters    .5 
3350 Invoice.Balance no trailing characters    .1 
.... ............... ...................... ......
See problems(...) for more details.

Any help is appreciated, thanks.


Solution

  • The httr::content function uses readr::read_csv when you pass type = "text/csv". You can pass parameters through to read_csv within content and luckily read_csv allows you to define the column types of the csv you're importing.

    invoice <- content(getdata, type = "text/csv", col_types = cols(
      Account.external_id__c = col_integer(),
      Invoice.Amount = col_double(),
      Invoice.Balance = col_double(),
      Invoice.CreatedDate = col_datetime(format = "")
    ))
    

    Notice Invoice.Balance = col_double().

    See vignette("column-types", package = "readr") for more info.