Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

format

r

filesize

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 
like image 273
Daniel Fletcher Avatar asked Apr 22 '15 03:04

Daniel Fletcher


People also ask

How do I convert MB to KB?

How to convert MegaBytes to KiloBytes. Converting from MB to KB is easy to do: multiply by 1024 under the binary system; multiply by 1000 under the SI system, or move the decimal dot three positions to the right.

How do you convert bytes to kilobytes to megabytes?

To convert smaller units to larger units (convert bytes to kilobytes or megabytes) you simply divide the original number by 1,024 for each unit size along the way to the final desired unit.

How do I convert MB to file size?

So to get the MB size, you need to divide the file size from (1024*1024).

How do I find the file size in KB?

Step 2: Multiply total number of pixels by the bit depth of the detector (16 bit, 14 bit etc.) to get the total number of bits of data. Step 3: Dividing the total number of bits by 8 equals the file size in bytes. Step 4: Divide the number of bytes by 1024 to get the file size in kilobytes.


2 Answers

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).

like image 96
MrFlick Avatar answered Sep 19 '22 22:09

MrFlick


Use the humanReadable() function in the gdata package. It has options to report the size in base 1000 ('SI') or base 1024 ('IEC') units, and it is also vectorized so you can process an entire vector of sizes at the same time.

For example:

> humanReadable(c(60810, 124141, 124, 13412513), width=4) [1] "60.8 kB" "124 kB"  "124 B"   "13.4 MB" > humanReadable(c(60810, 124141, 124, 13412513), standard="IEC", width=4) [1] "59.4 KiB" "121 KiB"  "124 B"    "12.8 MiB" 

I'm currently working to prepare release 2.16.0 of gdata, which adds the ability to indicate which unit you would like to use for reporting the sizes, as well as "Unix"-style units.

> humanReadable(c(60810, 124141, 124, 13412513), standard="SI", units="kB") [1] "   60.8 kB" "  124.1 kB" "    0.1 kB" "13412.5 kB" > humanReadable(c(60810, 124141, 124, 13412513), standard="IEC", units="KiB") [1] "   59.4 KiB" "  121.2 KiB" "    0.1 KiB" "13098.2 KiB" humanReadable(c(60810, 124141, 124, 13412513), standard="Unix", units="K") [1] "   59.4 K" "  121.2 K" "    0.1 K" "13098.2 K" 

-Greg [maintainer of the gdata package]

Update

CRAN has accepted gdata version 2.16.1, which supports standard="Unix" and units= options, and it should be available on a CRAN mirrors shortly.

like image 27
Gregory R. Warnes Avatar answered Sep 18 '22 22:09

Gregory R. Warnes