Types

Values (for assignment)

Bare types: 4 problems

assign_bracket <- function(x, i, value) {
  x[i] <- value
  dput(x)
}

assign_vec_slice <- function(x, i, value) {
  vec_slice(x, i) <- value
  dput(x)
}

# OK
assign_bracket(1:3, 2, 4L)
#> c(1L, 4L, 3L)
assign_vec_slice(1:3, 2, 4L)
#> c(1L, 4L, 3L)

# assign_vec_slice() should be stricter and coerce to integer
assign_bracket(1:3, 2, 4)
#> c(1, 4, 3)
assign_vec_slice(1:3, 2, 4)
#> c(1, 4, 3)

# assign_vec_slice() should be stricter and coerce to integer
assign_bracket(1:3, 2, 4.5)
#> c(1, 4.5, 3)
assign_vec_slice(1:3, 2, 4.5)
#> c(1, 4.5, 3)

# OK
assign_bracket(1:3, 2, FALSE)
#> c(1L, 0L, 3L)
assign_vec_slice(1:3, 2, FALSE)
#> c(1L, 0L, 3L)

# assign_vec_slice() should be stricter and cast lossy or throw an error
assign_bracket(c(TRUE, TRUE, FALSE), 2, 2L)
#> c(1L, 2L, 0L)
assign_vec_slice(c(TRUE, TRUE, FALSE), 2, 2L)
#> c(1L, 2L, 0L)

# OK
assign_bracket(as.character(1:3), 2, 4L)
#> c("1", "4", "3")
assign_vec_slice(as.character(1:3), 2, 4L)
#> c("1", "4", "3")

# assign_vec_slice() should be stricter and coerce to integer
assign_bracket(1:3, 2, "4")
#> c("1", "4", "3")
assign_vec_slice(1:3, 2, "4")
#> c("1", "4", "3")

# assign_vec_slice() should be stricter and coerce lossy or throw an error
assign_bracket(1:3, 2, "a")
#> c("1", "a", "3")
assign_vec_slice(1:3, 2, "a")
#> c("1", "a", "3")

# OK
assign_bracket(1:3, 2, NA)
#> c(1L, NA, 3L)
assign_vec_slice(1:3, 2, NA)
#> c(1L, NA, 3L)

Date: 1 problem, 1 unclear

x <- Sys.Date() + 0:2
dput(x)
#> structure(c(17978, 17979, 17980), class = "Date")

# OK
assign_bracket(x, 2, Sys.Date() - 1)
#> structure(c(17978, 17977, 17980), class = "Date")
assign_vec_slice(x, 2, Sys.Date() - 1)
#> structure(c(17978, 17977, 17980), class = "Date")

# assign_vec_slice() should warn about lossy cast
assign_bracket(x, 2, Sys.time())
#> structure(c(17978, 17978, 17980), class = "Date")
assign_vec_slice(x, 2, Sys.time())
#> structure(c(17978, 17978, 17980), class = "Date")

# OK (?)
assign_bracket(x, 2, as.character(Sys.Date() - 1))
#> structure(c(17978, 17977, 17980), class = "Date")
assign_vec_slice(x, 2, as.character(Sys.Date() - 1))
#> structure(c(17978, 17977, 17980), class = "Date")

# OK
assign_bracket(x, 2, "bogus date")
#> Error in charToDate(x): character string is not in a standard unambiguous format
assign_vec_slice(x, 2, "bogus date")
#> Error in charToDate(x): character string is not in a standard unambiguous format

# OK
assign_bracket(x, 2, NA)
#> structure(c(17978, NA, 17980), class = "Date")
assign_vec_slice(x, 2, NA)
#> structure(c(17978, NA, 17980), class = "Date")

POSIXct: all OK

x <- as.POSIXct(Sys.time() + c(0, 1, 60))
dput(x)
#> structure(c(1553347381.85488, 1553347382.85488, 1553347441.85488
#> ), class = c("POSIXct", "POSIXt"))

# OK
assign_bracket(x, 2, Sys.time() - 1)
#> structure(c(1553347381.85488, 1553347380.85657, 1553347441.85488
#> ), class = c("POSIXct", "POSIXt"))
assign_vec_slice(x, 2, Sys.time() - 1)
#> structure(c(1553347381.85488, 1553347380.8575, 1553347441.85488
#> ), class = c("POSIXct", "POSIXt"))

# OK
assign_bracket(x, 2, as.POSIXlt(Sys.time() - 1))
#> structure(c(1553347381.85488, 1553347380.85837, 1553347441.85488
#> ), class = c("POSIXct", "POSIXt"))
assign_vec_slice(x, 2, as.POSIXlt(Sys.time() - 1))
#> structure(c(1553347381.85488, 1553347380.85911, 1553347441.85488
#> ), class = c("POSIXct", "POSIXt"))

# OK
assign_bracket(x, 2, Sys.Date())
#> structure(c(1553347381.85488, 1553299200, 1553347441.85488), class = c("POSIXct", 
#> "POSIXt"))
assign_vec_slice(x, 2, Sys.Date())
#> structure(c(1553347381.85488, 1553299200, 1553347441.85488), class = c("POSIXct", 
#> "POSIXt"))

# OK
assign_bracket(x, 2, as.character(Sys.time() - 1))
#> structure(c(1553347381.85488, 1553347380, 1553347441.85488), class = c("POSIXct", 
#> "POSIXt"))
assign_vec_slice(x, 2, as.character(Sys.time() - 1))
#> structure(c(1553347381.85488, 1553347380, 1553347441.85488), class = c("POSIXct", 
#> "POSIXt"))

# OK
assign_bracket(x, 2, "bogus time")
#> Error in as.POSIXlt.character(x, tz, ...): character string is not in a standard unambiguous format
assign_vec_slice(x, 2, "bogus time")
#> Error in as.POSIXlt.character(x, tz, ...): character string is not in a standard unambiguous format

# OK
assign_bracket(x, 2, NA)
#> structure(c(1553347381.85488, NA, 1553347441.85488), class = c("POSIXct", 
#> "POSIXt"))
assign_vec_slice(x, 2, NA)
#> structure(c(1553347381.85488, NA, 1553347441.85488), class = c("POSIXct", 
#> "POSIXt"))

POSIXlt: 1 problem

x <- as.POSIXlt(Sys.time() + c(0, 1, 60))
dput(x)
#> structure(list(sec = c(1.88168382644653, 2.88168382644653, 1.88168382644653
#> ), min = c(23L, 23L, 24L), hour = c(14L, 14L, 14L), mday = c(23L, 
#> 23L, 23L), mon = c(2L, 2L, 2L), year = c(119L, 119L, 119L), wday = c(6L, 
#> 6L, 6L), yday = c(81L, 81L, 81L), isdst = c(0L, 0L, 0L), zone = c("CET", 
#> "CET", "CET"), gmtoff = c(3600L, 3600L, 3600L)), class = c("POSIXlt", 
#> "POSIXt"), tzone = c("", "CET", "CEST"))

# OK
assign_bracket(x, 2, Sys.time() - 1)
#> structure(list(sec = c(1.88168382644653, 0.884351968765259, 1.88168382644653
#> ), min = c(23L, 23L, 24L), hour = c(14L, 14L, 14L), mday = c(23L, 
#> 23L, 23L), mon = c(2L, 2L, 2L), year = c(119L, 119L, 119L), wday = c(6L, 
#> 6L, 6L), yday = c(81L, 81L, 81L), isdst = c(0L, 0L, 0L), zone = c("CET", 
#> "CET", "CET"), gmtoff = c(3600L, 3600L, 3600L)), tzone = c("", 
#> "CET", "CEST"), class = c("POSIXlt", "POSIXt"))
assign_vec_slice(x, 2, Sys.time() - 1)
#> structure(list(sec = c(1.88168382644653, 0.886616945266724, 1.88168382644653
#> ), min = c(23L, 23L, 24L), hour = c(14L, 14L, 14L), mday = c(23L, 
#> 23L, 23L), mon = c(2L, 2L, 2L), year = c(119L, 119L, 119L), wday = c(6L, 
#> 6L, 6L), yday = c(81L, 81L, 81L), isdst = c(0L, 0L, 0L), zone = c("CET", 
#> "CET", "CET"), gmtoff = c(3600L, 3600L, 3600L)), tzone = c("", 
#> "CET", "CEST"), class = c("POSIXlt", "POSIXt"))

# OK
assign_bracket(x, 2, as.POSIXct(Sys.time() - 1))
#> structure(list(sec = c(1.88168382644653, 0.888098239898682, 1.88168382644653
#> ), min = c(23L, 23L, 24L), hour = c(14L, 14L, 14L), mday = c(23L, 
#> 23L, 23L), mon = c(2L, 2L, 2L), year = c(119L, 119L, 119L), wday = c(6L, 
#> 6L, 6L), yday = c(81L, 81L, 81L), isdst = c(0L, 0L, 0L), zone = c("CET", 
#> "CET", "CET"), gmtoff = c(3600L, 3600L, 3600L)), tzone = c("", 
#> "CET", "CEST"), class = c("POSIXlt", "POSIXt"))
assign_vec_slice(x, 2, as.POSIXct(Sys.time() - 1))
#> structure(list(sec = c(1.88168382644653, 0.889528036117554, 1.88168382644653
#> ), min = c(23L, 23L, 24L), hour = c(14L, 14L, 14L), mday = c(23L, 
#> 23L, 23L), mon = c(2L, 2L, 2L), year = c(119L, 119L, 119L), wday = c(6L, 
#> 6L, 6L), yday = c(81L, 81L, 81L), isdst = c(0L, 0L, 0L), zone = c("CET", 
#> "CET", "CET"), gmtoff = c(3600L, 3600L, 3600L)), tzone = c("", 
#> "CET", "CEST"), class = c("POSIXlt", "POSIXt"))

# assign_vec_slice() should work here the same as for POSIXct
assign_bracket(x, 2, Sys.Date())
#> Error in x[[n]][i] <- value[[n]]: replacement has length zero
assign_vec_slice(x, 2, Sys.Date())
#> Error in x[[n]][i] <- value[[n]]: replacement has length zero

# OK
assign_bracket(x, 2, as.character(Sys.time() - 1))
#> structure(list(sec = c(1.88168382644653, 0, 1.88168382644653), 
#>     min = c(23L, 23L, 24L), hour = c(14L, 14L, 14L), mday = c(23L, 
#>     23L, 23L), mon = c(2L, 2L, 2L), year = c(119L, 119L, 119L
#>     ), wday = c(6L, 6L, 6L), yday = c(81L, 81L, 81L), isdst = c(0L, 
#>     0L, 0L), zone = c("CET", "CET", "CET"), gmtoff = c(3600L, 
#>     NA, 3600L)), tzone = c("", "CET", "CEST"), class = c("POSIXlt", 
#> "POSIXt"))
assign_vec_slice(x, 2, as.character(Sys.time() - 1))
#> structure(list(sec = c(1.88168382644653, 0, 1.88168382644653), 
#>     min = c(23L, 23L, 24L), hour = c(14L, 14L, 14L), mday = c(23L, 
#>     23L, 23L), mon = c(2L, 2L, 2L), year = c(119L, 119L, 119L
#>     ), wday = c(6L, 6L, 6L), yday = c(81L, 81L, 81L), isdst = c(0L, 
#>     0L, 0L), zone = c("CET", "CET", "CET"), gmtoff = c(3600L, 
#>     NA, 3600L)), tzone = c("", "CET", "CEST"), class = c("POSIXlt", 
#> "POSIXt"))

# OK
assign_bracket(x, 2, "bogus time")
#> Error in as.POSIXlt.character(value): character string is not in a standard unambiguous format
assign_vec_slice(x, 2, "bogus time")
#> Error in as.POSIXlt.character(value): character string is not in a standard unambiguous format

# OK
assign_bracket(x, 2, NA)
#> structure(list(sec = c(1.88168382644653, NA, 1.88168382644653
#> ), min = c(23L, NA, 24L), hour = c(14L, NA, 14L), mday = c(23L, 
#> NA, 23L), mon = c(2L, NA, 2L), year = c(119L, NA, 119L), wday = c(6L, 
#> NA, 6L), yday = c(81L, NA, 81L), isdst = c(0L, -1L, 0L), zone = c("CET", 
#> "", "CET"), gmtoff = c(3600L, NA, 3600L)), tzone = c("", "CET", 
#> "CEST"), class = c("POSIXlt", "POSIXt"))
assign_vec_slice(x, 2, NA)
#> structure(list(sec = c(1.88168382644653, NA, 1.88168382644653
#> ), min = c(23L, NA, 24L), hour = c(14L, NA, 14L), mday = c(23L, 
#> NA, 23L), mon = c(2L, NA, 2L), year = c(119L, NA, 119L), wday = c(6L, 
#> NA, 6L), yday = c(81L, NA, 81L), isdst = c(0L, -1L, 0L), zone = c("CET", 
#> "", "CET"), gmtoff = c(3600L, NA, 3600L)), tzone = c("", "CET", 
#> "CEST"), class = c("POSIXlt", "POSIXt"))

hms: 1 problem

x <- hms(c(0, 1, 60))
dput(x)
#> structure(c(0, 1, 60), units = "secs", class = c("hms", "difftime"
#> ))

# OK
assign_bracket(x, 2, hms(-1))
#> structure(c(0, -1, 60), units = "secs", class = c("hms", "difftime"
#> ))
assign_vec_slice(x, 2, hms(-1))
#> structure(c(0, -1, 60), units = "secs", class = c("hms", "difftime"
#> ))

# OK
assign_bracket(x, 2, Sys.time() - Sys.time())
#> structure(c(0, -2.62260437011719e-06, 60), units = "secs", class = c("hms", 
#> "difftime"))
assign_vec_slice(x, 2, Sys.time() - Sys.time())
#> structure(c(0, -2.62260437011719e-06, 60), units = "secs", class = c("hms", 
#> "difftime"))

# Should throw an error
assign_bracket(x, 2, "bogus time")
#> structure(c("0", "bogus time", "60"), units = "secs", class = c("hms", 
#> "difftime"))
assign_vec_slice(x, 2, "bogus time")
#> structure(c("0", "bogus time", "60"), units = "secs", class = c("hms", 
#> "difftime"))

# OK
assign_bracket(x, 2, NA)
#> structure(c(0, NA, 60), units = "secs", class = c("hms", "difftime"
#> ))
assign_vec_slice(x, 2, NA)
#> structure(c(0, NA, 60), units = "secs", class = c("hms", "difftime"
#> ))

blob: all ok

x <- blob(raw(0), raw(1), raw(2))
dput(x)
#> structure(list(raw(0), as.raw(0x00), as.raw(c(0x00, 0x00))), ptype = raw(0), class = c("blob", 
#> "vctrs_list_of", "vctrs_vctr"))

# OK
assign_bracket(x, 2, blob(raw(3)))
#> structure(list(raw(0), as.raw(c(0x00, 0x00, 0x00)), as.raw(c(0x00, 
#> 0x00))), ptype = raw(0), class = c("blob", "vctrs_list_of", "vctrs_vctr"
#> ))
assign_vec_slice(x, 2, blob(raw(3)))
#> structure(list(raw(0), as.raw(c(0x00, 0x00, 0x00)), as.raw(c(0x00, 
#> 0x00))), ptype = raw(0), class = c("blob", "vctrs_list_of", "vctrs_vctr"
#> ))

# OK
assign_bracket(x, 2, list(raw(3)))
#> structure(list(raw(0), as.raw(c(0x00, 0x00, 0x00)), as.raw(c(0x00, 
#> 0x00))), ptype = raw(0), class = c("blob", "vctrs_list_of", "vctrs_vctr"
#> ))
assign_vec_slice(x, 2, list(raw(3)))
#> structure(list(raw(0), as.raw(c(0x00, 0x00, 0x00)), as.raw(c(0x00, 
#> 0x00))), ptype = raw(0), class = c("blob", "vctrs_list_of", "vctrs_vctr"
#> ))

# OK (required for RSQLite)
assign_bracket(x, 2, "data")
#> structure(list(raw(0), as.raw(c(0x64, 0x61, 0x74, 0x61)), as.raw(c(0x00, 
#> 0x00))), ptype = raw(0), class = c("blob", "vctrs_list_of", "vctrs_vctr"
#> ))
assign_vec_slice(x, 2, "data")
#> structure(list(raw(0), as.raw(c(0x64, 0x61, 0x74, 0x61)), as.raw(c(0x00, 
#> 0x00))), ptype = raw(0), class = c("blob", "vctrs_list_of", "vctrs_vctr"
#> ))

# OK
assign_bracket(x, 2, 3)
#> Error: Can't cast <double> to <blob>
assign_vec_slice(x, 2, 3)
#> Error: Can't cast <double> to <blob>

# OK
assign_bracket(x, 2, list("data"))
#> Error: `x` must be a list of raw vectors
assign_vec_slice(x, 2, list("data"))
#> Error: `x` must be a list of raw vectors

# OK
assign_bracket(x, 2, NA)
#> structure(list(raw(0), NULL, as.raw(c(0x00, 0x00))), ptype = raw(0), class = c("blob", 
#> "vctrs_list_of", "vctrs_vctr"))
assign_vec_slice(x, 2, NA)
#> structure(list(raw(0), NULL, as.raw(c(0x00, 0x00))), ptype = raw(0), class = c("blob", 
#> "vctrs_list_of", "vctrs_vctr"))

# OK
assign_bracket(x, 2, x[NA_integer_])
#> structure(list(raw(0), NULL, as.raw(c(0x00, 0x00))), ptype = raw(0), class = c("blob", 
#> "vctrs_list_of", "vctrs_vctr"))
assign_vec_slice(x, 2, x[NA_integer_])
#> structure(list(raw(0), NULL, as.raw(c(0x00, 0x00))), ptype = raw(0), class = c("blob", 
#> "vctrs_list_of", "vctrs_vctr"))