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:
Create a folder to store the file:
dir.create("census-app/data")
Download the file (~60KB):
download.file("http://shiny.rstudio.com/tutorial/lesson5/census-app/data/counties.rds", "census-app/data/counties.rds")
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
x bytes / 1024
to return the value in KB? I agree this is a simple calculation and part of my goal is to avoid manual intervention a) in case I accidentally enter something like 1000, instead of 1024 and b) to forgo researching the correct conversion ratio. – Brandebrandea