6

Disclaimer: The title of the question might be wrong, because I don't know what causes this behaviour. The 'issue' is that viewing a POSIXlt object in RStudio viewer shows an infinite linked list, as far as I can determine.

MWE:

x <- strptime("2021-01-17", format = "%Y-%m-%d")
class(x)# outputs "POSIXlt" "POSIXt" 
View(x) # Opens the viewer in RStudio

When opening the viewer, you can click 'expand' on each item, and it goes all the way down: Screenshot of the RStudio viewer of the time variable

I can access the content via the console as well, for example typing:

x[[1]][[1]][[1]][[1]][[1]][[1]][[1]][[1]][[1]][[1]][[1]][[1]][[1]][[1]][[1]][[1]][[1]]

The output is always the same: the date "2021-01-17 CET".

What is causing this? I suspect this might be due to RStudio rather than the POSIXlt class itself? I also tried reading the ?strptime documentation but I found no note regarding the output of the function causing this.

Henrik
  • 65,555
  • 14
  • 143
  • 159
marts
  • 658
  • 1
  • 10
  • 15
  • the `\`[[\`` here is not the default extracting, try `debugonce(\`[[.POSIXlt\`); x[[1]]` vs `unclass(x)[[1]]` – rawr Mar 08 '21 at 11:37
  • 3
    That's another RStudio bug, title and `rstudio` tag are correct. Works as expected in R-gui. – jay.sf Mar 08 '21 at 11:49
  • 1
    From *Details* in `?POSIXlt`: Class "`POSIXlt`" is a named list of vectors representing...". Fine. But: "Note that the internal list structure is somewhat hidden". It may be the latter that confuses the print methods of `View` in Rstudio. Try `View(unclass(x))`. – Henrik Mar 08 '21 at 11:51
  • 1
    See also the *Example* "## look at *internal* representation of "POSIXlt", where `str` only "fails", and `str(unclass(.))` is used. Poor RStudio, a lot to think of when viewing POSIXlt. – Henrik Mar 08 '21 at 11:59
  • @Henrik This is all the more bizarre since `str` is a generic, and `POSIXt` provides a suitable method, but `POSIXlt` doesn’t. Bizarre decision. – Konrad Rudolph Mar 08 '21 at 12:27
  • 1
    i thought the more interesting question was why is `x[[1]]` recursive rather than whatever nonsense and bloat rstudio is on today. `utils::View` is supposed to coerce the object to a data frame first, so `View(as.data.frame(x))` instead of unclass – rawr Mar 08 '21 at 13:53
  • @rawr The *why* is pretty trivially answered: because `x` is a `list`, and `x[[1]]` returns `x`. RStudio really can’t do much here, except hard-code a check for this specific type (well, it *could* check whether subsetting an element returns that element itself, but honestly such a check makes very little sense with the standard semantics of `[[`). – Konrad Rudolph Mar 08 '21 at 18:03
  • @KonradRudolph it's pretty trivial to prove how wrong you are: `x <- list(1, 2)` is a list, `x[[1]]` does not return `x`. you're welcome – rawr Mar 08 '21 at 20:07
  • 1
    @rawr No need to be rude, especially since you’ve completely misunderstood my comment. In fact, your comment illustrates the *expected* behaviour of subsetting a list with `[[`. The point of *my* comment was that `POSIXlt` behaves *differently*, which breaks the expected semantics of `[[` for list subsetting that your comments shows. – Konrad Rudolph Mar 08 '21 at 22:57
  • @rawr My comment was in response to your *last* comment not your first comment, and it wasn’t meant to contradict or otherwise attack your comments at all. It was merely a small technical addition (and a small defence of RStudio’s not *that* unreasonable behaviour). That said, I think I misunderstood your rhetorical “why” as asking *why RStudio displays the value recursively*, rather than as asking why `[[.POSIXct` returns its own value (I wouldn’t call this “recursive” but I can see why someone might reasonably do so). – Konrad Rudolph Mar 08 '21 at 23:30

0 Answers0