0

Is there any way when using the str() function in R to start at the beginning of the output provided rather than the end after the function is run?

Basically I'd like a faster way to get to the beginning of the output rather than scrolling manual back up through the output. This would be especially useful when looking at the structure of larger objects like spatial data.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Flammulation
  • 336
  • 2
  • 16
  • Its very unclear to me exactly what you are asking. What type of object are you using `str()` on? What types of properties do you want to see? A [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) would be helpful. – MrFlick Apr 10 '17 at 15:16
  • 2
    If the output from `str` is very long, it is sometimes helpful to just start out with just the top level of the structure with `str(object, max.level=2)` – G5W Apr 10 '17 at 15:20
  • http://stackoverflow.com/questions/3837520/how-to-prevent-the-output-of-r-to-scroll-away-in-bash – Dason Apr 10 '17 at 15:24

1 Answers1

1

A variation of the answer linked to by Dason (https://stackoverflow.com/a/3837885/1017276) would be to redirect the output to a browser.

view_str <- function(x)
{
  tmp <- tempfile(fileext = ".html")
  x <- capture.output(str(x))

  write(paste0(x, collapse = "<br/>"),
        file = tmp)

  viewer <- getOption("viewer")
  if (!is.null(viewer)) # If in RStudio, use RStudio's browser
  {
    viewer(tmp)
  }
  else{                 # Otherwise use the system's default browser
    utils::browseURL(tmp)
  }
}

view_str(mtcars)
Community
  • 1
  • 1
Benjamin
  • 16,897
  • 6
  • 45
  • 65