【发布时间】:2018-07-08 06:41:19
【问题描述】:
使用“formattable”包,我可以在 Shiny 应用程序中为数据表的条目着色。但是,既然我已经向我的 Shiny 应用程序添加了一个带有下拉菜单的数据表,我无法让我的原始代码为下拉菜单中的各个下拉项目着色(着色背景而不是文本将是最好的)。我要么收到类似于
的错误Ops.factor(x) 中的警告:'>' 对因子没有意义
或者只是字符串“未定义”而不是数据表。
到目前为止,我所拥有的是:
server.R(仅打印重要部分):
# Setting up the input for the buttons in the table
shinyInput <- function(FUN, len, id, dropdown_input, ...) {
print(dropdown_input)
inputs <- character(len)
for (i in seq_len(len)) {
inputs[i] <- as.character(FUN(paste0(id, i), label = "", choices = dropdown_input[, i]))
}
return(inputs)
}
# Generate the dropdown items
dropdowns <- data.frame(
probabilities = shinyInput(selectInput, length(category_descriptions), id = 'dropdown_', dropdown_input = input_data))
df$data <- cbind(category, dropdowns)
})
prob_formatter <- formatter("span",
style = x ~ style(color = ifelse(x > 0.07, "green", ifelse(x < 0.07, "red", "black"))))
# Load the datatable after a csv file is uploaded
observeEvent(input$dataset, {
output$table <- DT::renderDataTable( {
return(as.datatable(formattable(df$data, list(probabilities = prob_formatter)), escape = FALSE, selection = 'none')
)})
})
ui.R(仅打印重要部分):
# Main panel for displaying outputs ----
mainPanel(formattableOutput("table"))
可选背景信息:下拉菜单中的项目等于机器学习算法返回的用于对一组词进行分类的概率。最终,用户将能够选择正确的类别来提供反馈并改进底层模型。为了帮助用户,最好根据基础概率为下拉菜单中模型返回的可能性背景着色。我已经阅读了'formattable'的documentation,我认为'as.datatable.formattable'可能是一种方法,但它似乎从我在R中的功能中丢失(重新安装包不会使它出现了)。
【问题讨论】:
-
您的
x似乎被视为一个因素......您的代码中的x是什么? -
如果我理解正确
x只是formattable中调用的任何列名的占位符。所以在这种情况下x是probabilities(包含概率的列),随后会对其进行评估。我已经尝试使用stackoverflow.com/questions/3418128/… 将概率转换为数值,但这并没有什么不同。我相信probabilities甚至不包含因素。 -
如果您只是在关于格式化程序的行中添加了注释,而不在 renderDataTable 中调用 formattable 会发生什么?它显示表格吗?
标签: r datatable shiny data.table