[英]Reactive formattable in shiny?
如何使用formattable為反應式表中的某些值着色?
這是一個可重現的示例:如果我在我創建的反應結果表中的p值小於0.05,我想用紅色上色。
library(DT)
library(shiny)
library(shinydashboard)
library(formattable)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
selectizeInput("v.dependent", "", choices = names(mtcars),
selected = "mpg", multiple = FALSE),
selectizeInput("predictor", "", choices = names(mtcars),
selected = "disp",
multiple = TRUE)),
dashboardBody(
tabsetPanel(
tabPanel("test with mtcars",
box(formattableOutput("tab"))
)
)
)
)
server <- function(input, output) {
dep.var <- reactive({
out <- input$v.dependent
out
})
ind.var <- reactive({
out <- input$predictor
out
})
var.selected <- reactive({
out <- append(ind.var(), dep.var(), 0)
out
})
user.selection <- reactive({
mtcars[, names(mtcars) %in% var.selected()]
})
lmod <- reactive({
lm(as.formula(paste(input$v.dependent, "~", paste(input$predictor, collapse = "+"))), data = user.selection())
})
output$tab <- renderFormattable({
tmp <- summary(lmod())$coefficients
colnames(tmp) <- c("Coefficients", "SD", "t statistic", "Pvalue")
tmp <- signif(x = tmp, digits = 3)
tmp <- formattable(tmp, list(Pvalue = formatter("span",
style = x ~ style(color = ifelse(x < 0.05, style(color = "red", "black")))
)))
})
}
shinyApp(ui, server)
我知道如何處理“靜態”表,但是當我嘗試使用此代碼時,出現了錯誤:
Warning: Error in formatC: 'format' must be one of {"f","e","E","g","G", "fg", "s"}
知道如何解決嗎?
您正在使用formattable.data.table
的語法,但是在您的情況下, tmp
是一個行為不同的矩陣。 看來您希望它是一個data.frame,以便您自己進行投射。 另外,在ifelse
設置顏色似乎有些問題。 這似乎在做你想要的
output$tab <- renderFormattable({
tmp <- summary(lmod())$coefficients
colnames(tmp) <- c("Coefficients", "SD", "t statistic", "Pvalue")
tmp <- signif(x = tmp, digits = 3)
tmp <- as.data.frame(tmp)
tmp <- formattable(tmp, list(
Pvalue = formatter("span", style = x ~ style(color = ifelse(x < 0.05, "red", "black"))))
)
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.