使用 {rhandsontable} 包可以创建一个表格,其中日期列包含每个单元格的下拉列表,既提供了流畅的 UI 又验证了用户输入。
我想在列中设置单个单元格以使用此格式,但包的 API 似乎无法实现这种级别的特异性:
library(rhandsontable)
rhandsontable(
data.frame(
date = seq(as.Date("2020-01-01"), by = "days", length.out = 4)
)
)
例如,整列的数据可能是这样的:
# E.g. for the third cell in column `x`
rhandsontable(
data.frame(
x = c("foo", "bar", "2020/01/01")
)
)
不幸的是,{rhandsontable} 似乎不支持将第三个单元格设置为开箱即用的日期格式的方法。
hot_cell()
用于设置单个单元格属性,例如注释,但不能用于设置单元格格式。
rhandsontable()
和 hot_cell()
的源代码,我发现我可以相当轻松地实现这样的目标,尽管这绝对是一个“标签外”功能:
library(rhandsontable)
df <- data.frame(
y = c("foo", "bar", "2020/01/01")
)
hot <- rhandsontable(df)
hot$x$cell <- list(list(
row = 2,
col = 0,
type = "date",
correctFormat = TRUE,
dateFormat = "MM/DD/YYYY"
))
hot