How do I quickly convert the size element of file.info() from bytes to KB, MB, GB, etc.?

Daniel Fletcher picture Daniel Fletcher · Apr 22, 2015 · Viewed 7.3k times · Source

I expect there is already an answer for this on stackoverflow, and I simply failed to find it.

Desired outcome: Quickly convert the file size element in a file.info() call from bytes to KB, MB, etc. I'm fine if the output is either i) a character string with the desired size type, e.g., "96 bytes" or ii) simply a numeric conversion, e.g., from 60963 bytes to 60.963 KB (per Google).

Repro steps:

  1. Create a folder to store the file:

    dir.create("census-app/data")
    
  2. Download the file (~60KB):

    download.file("http://shiny.rstudio.com/tutorial/lesson5/census-app/data/counties.rds",
    "census-app/data/counties.rds")
    
  3. Use file.info()$size to return the file size in bytes:

    file.info("census-app//data//counties.rds")$size
    [1] 60963
    

From there, I'm stuck. I realize I can do some complicated/manual parsing and calculation to make the conversion (see Converting kilobytes, megabytes etc. to bytes in R).

However, I'm hoping I can simply use a base function or something similar:

    format(file.info("census-app//data//counties.rds")$size, units = "KB")
    [1] "60963"
    # Attempt to return file size in KB simply returns the size in bytes
    # NOTE: format(x, units = "KB") works fine when I
    # pass it object.size() for an object loaded in R

Answer

MrFlick picture MrFlick · Apr 22, 2015

The object.size() function does this type of formatting for it's results, but its meant to tell you the size of the R object you pass to it. It is not set up to take an arbitrary by value.

However, we can "steal" some of it's formatting logic. You can call it with

utils:::format.object_size(60963, "auto")
# [1] "59.5 Kb"

In that way we can call the un-exported formatting function. You can bring up the additional formatting options on the ?format.object_size help page. Note that it uses the rule that 1 Kb = 1024 bytes (not 1000 as in your example).