Search code examples
rrstudiosurvival

Viewing survival:Surv objects using View from RStudio


I ran the below code chunk from a fresh start from within RStudio and get a Error in View : subscript out of bounds error when trying to use View. The issue appears to be with viewing the Surv objects ("censored data" where the exact value is represented by a range rather than an exact number).

library(survival)
df <- data.frame(id = c(1:10)
 , cen1_lo = c(0, 0, 2, 0, 2, 6, 2, 0, 2, 2) 
 , cen1_hi = c(6, 0, 6, 0, 2, 6, 2, 0, 6, 3)
 , cen2_lo = c(1, 1, 3, 2, 3, 6, 1, 3, 1, 0) 
 , cen2_hi = c(6, 3, 6, 2, 4, 6, 2, 5, 6, 3))

df$cen1 <- Surv(df$cen1_lo, df$cen1_hi, type = "interval2")
df$cen2 <- Surv(df$cen2_lo, df$cen2_hi, type="interval2")

The above code appears to work correctly as outputting df to console yields:

   id cen1_lo cen1_hi cen2_lo cen2_hi   cen1   cen2
1   1       0       6       1       6 [0, 6] [1, 6]
2   2       0       0       1       3      0 [1, 3]
3   3       2       6       3       6 [2, 6] [3, 6]
4   4       0       0       2       2      0      2
5   5       2       2       3       4      2 [3, 4]
6   6       6       6       6       6      6      6
7   7       2       2       1       2      2 [1, 2]
8   8       0       0       3       5      0 [3, 5]
9   9       2       6       1       6 [2, 6] [1, 6]
10 10       2       3       0       3 [2, 3] [0, 3]

However, running View(df) yields the error:

Error in View : subscript out of bounds

Below is the session information (sessioninfo::session_info())

─ Session info ─────────────────────────────────────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.2.0 (2022-04-22 ucrt)
 os       Windows 10 x64 (build 18363)
 system   x86_64, mingw32
 ui       RStudio
 language (EN)
 collate  English_United States.utf8
 ctype    English_United States.utf8
 tz       America/New_York
 date     2022-05-02
 rstudio  2022.02.2+485 Prairie Trillium (desktop)
 pandoc   NA

─ Packages ─────────────────────────────────────────────────────────────────────────────────────────────────
 package     * version date (UTC) lib source
 cli           3.3.0   2022-04-25 [1] CRAN (R 4.2.0)
 lattice       0.20-45 2021-09-22 [1] CRAN (R 4.2.0)
 Matrix        1.4-1   2022-03-23 [1] CRAN (R 4.2.0)
 sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.2.0)
 survival    * 3.3-1   2022-03-03 [1] CRAN (R 4.2.0)

The View(df) command runs correctly when the same code (replicated below) is run in native R

library(survival)
df <- data.frame(id = c(1:10)
, cen1_lo = c(0, 0, 2, 0, 2, 6, 2, 0, 2, 2) 
, cen1_hi = c(6, 0, 6, 0, 2, 6, 2, 0, 6, 3)
, cen2_lo = c(1, 1, 3, 2, 3, 6, 1, 3, 1, 0) 
, cen2_hi = c(6, 3, 6, 2, 4, 6, 2, 5, 6, 3))

df$cen1 <- Surv(df$cen1_lo, df$cen1_hi, type = "interval2")
df$cen2 <- Surv(df$cen2_lo, df$cen2_hi, type="interval2")
View(df)

enter image description here


Solution

  • All of the RStudio 2022 versions seem to have this issue. If you install one of the older 2021 versions of RStudio then View(df) should work as expected.