Rshiny交互式表格⽬录
1.需求
在shiny的使⽤中,需要有⼀个交互式的表格来实现查看,操作以及管理功能。
具体场景是⽤户提交⼀则申请,管理员进⾏批准和拒绝操作。
2.问题
有现成的插件,但是不够美观
不⽅便使⽤observe、observeEvent等在shiny server监听button事件
3.解决
3.1
DT::datatable(
cal_df_display,
# filter = list(position="top",targets=c(3,4,5,6)),
selection = "none",
# callback = childtb_js,
# rownames = rowNames,
escape = FALSE,
options = list(
keys = TRUE,
fixedHeader = TRUE,
dom="frtip",
search = list(regex=TRUE,caseInsensitive=TRUE),
# columnDefs = list(list(targets = c(2,3,4,5,6,7,8), orderable = FALSE)),
pageLength = 15
)
# extensions = c("Select","SearchPanes")
)
3.2
问题:想要修改DT包的theme风格
R代码:
library(reactable)
reactable(
cal_df_display,
showPageInfo = TRUE,
# filterable = TRUE,
searchable = TRUE,
highlight = TRUE,
highlight = TRUE,
# selection = "multiple",
defaultPageSize = 10,
# onClick = "select",
bordered = TRUE,
resizable = TRUE,
columns = list(
# 序号 = colDef(footer = function(values) sprintf("总计:%.0f⼈", max(values)))序号 = colDef(footer = sprintf("总计:%.0f⼈", nrow(cal_df_display))),
全天检查 = colDef(minWidth = 50),
分类 = colDef(minWidth = 80),
是否课题 = colDef(minWidth = 80),
是否确认 = colDef(cell = function(value) {
# Render as ✘ or ✓
if (value == "0") "\u2718" else "\u2713"
},style = function(value) {
# Render as ✘ or ✓
if (value == "0"){
color="#e00000"
}
else{
color = "#008000"
}
list(color = color, fontWeight = "bold")
}
),
确认结果 = colDef(cell = function(value) {
# Render as ✘ or ✓
if (value == "0") "\u2718" else "\u2713"
},
style = function(value) {
# Render as ✘ or ✓
if (value == "0"){
color="#e00000"
}
else{
color = "#008000"
}
list(color = color, fontWeight = "bold")
fontweight属性bold
}
),
是否管理员 = colDef(cell = function(value) {
# Render as ✘ or ✓
if (value == "0") "\u2718" else "\u2713"
},
style = function(value) {
# Render as ✘ or ✓
if (value == "0"){
color="#e00000"
}
else{
color = "#008000"
}
list(color = color, fontWeight = "bold")
}
),
operation = colDef(
name = "",
sortable = FALSE,
resizable=TRUE,
html = TRUE,
minWidth = 150
# footer = sprintf("总计:%.0f⼈", nrow(cal_df_display))
)
),
defaultColDef = colDef(footerStyle = list(fontWeight = "bold"))
)
问题:if (value == "0") "\u2718" else "\u2713"  # Render as ✘ or ✓,此处的"\u2718" 为ASCII编码,rstudio默认为utf-8编码,字符⽆法识别会报错。
解决:使⽤,采⽤图标替代,✘图标为”times“,✓图标为“check”()。
R代码:
#先添加图标列以及颜⾊列
cal_df_display <- cal_df_display %>% mutate(
admin_icon=case_when(
是否管理员==0 ~ "times",
是否管理员==1 ~ "check"),
admin_icon_color=case_when(是否管理员==0 ~ "#e00000",
是否管理员==1 ~ "#008000"),
confirm_icon=case_when(
是否确认==0 ~ "times",
是否确认==1 ~ "check"),
confirm_icon_color=case_when(是否确认==0 ~ "#e00000",
是否确认==1 ~ "#008000"),
result_icon=case_when(
确认结果==0 ~ "times",
确认结果==1 ~ "check"),
result_icon_color=case_when(确认结果==0 ~ "#e00000",
确认结果==1 ~ "#008000"))
#使⽤icon_ref,icon_color_ref添加对应图标及颜⾊
cal_df_display %>% select(-operation)%>%
reactable(
.,
defaultSorted = "是否确认",
columns = list(
# 序号 = colDef(cell = icon_sets(df,icons = "times",colors = "#e00000",icon_position = "over")),
admin_icon =colDef(show = FALSE),
admin_icon_color=colDef(show = FALSE),
confirm_icon =colDef(show = FALSE),
confirm_icon_color=colDef(show = FALSE),
result_icon =colDef(show = FALSE),
result_icon_color=colDef(show = FALSE),
`是否管理员` = colDef(
cell = icon_sets(.,icon_size = 20,icon_ref = "admin_icon",icon_color_ref="admin_icon_color",icon_position = "over")),
是否确认 = colDef(
cell = icon_sets(.,icon_size = 20,icon_ref = "confirm_icon",icon_color_ref="confirm_icon_color",icon_position = "over")),
确认结果 = colDef(
cell = icon_sets(.,icon_size = 20,icon_ref = "result_icon",icon_color_ref="result_icon_color",icon_position = "over"))
))
4 结果

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。