I want to display the information of a raster layer as a table or data frame in R. In particular, I am most interested in the raster name and resolution.

I want the information which shows by just reading the raster layer or by:

r <- raster("mypath/B01.tif")

How to return print(r) as a table or data frame?

1 Answer 1


Try this:

r_df <- as.data.frame(capture.output(print(r)))

then to pretty it up

r_df <- tidyr::separate(r_df, 1, into = c('param', 'data'), sep = ' : ')
r_df <- dplyr::filter(r_df, !is.na(data))

There are baseR methods for the above steps but tbh, life's too short.

NB What you're asking here isn't actually spatial even though raster is involved - the generic problem is 'put some console output in an object and make it look nice'. For future reference this kind of question fits stackoverflow better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.