我最近发现了R的formattable包,我试图在闪亮的时候使用它,但我没有成功。这是我在github上找到的代码:
-fsanitize=address
当我运行应用程序时,我得到的是桌子而不是闪亮的模板。你能帮忙吗?
由于
答案 0 :(得分:2)
我会尝试将其分解为几点。
闪亮使用的功能已经在formattable
中。见lines。我们可以从代码中删除这些行,因为它们不是必需的。
在您的示例中,box
是什么?通常用于在静态图周围绘制一个框,但不适用于htmlwidgets
。
fluidRow(
box(
formattableOutput("formattableexample")
),
)
此外,您的示例中的上述行中有一个迷路,
。
不再需要使用formattable
明确转换as.htmlwidget
,因此我们可以将其删除。
如上所述纠正错误并删除行,为我们提供了以下工作代码。
library(shiny)
library(formattable)
library(htmlwidgets)
#Define a dataframe (example stolen from formattable readme)
df <- data.frame(
id = 1:10,
name = c("Bob", "Ashley", "James", "David", "Jenny",
"Hans", "Leo", "John", "Emily", "Lee"),
age = c(28, 27, 30, 28, 29, 29, 27, 27, 31, 30),
grade = c("C", "A", "A", "C", "B", "B", "B", "A", "C", "C"),
test1_score = c(8.9, 9.5, 9.6, 8.9, 9.1, 9.3, 9.3, 9.9, 8.5, 8.6),
test2_score = c(9.1, 9.1, 9.2, 9.1, 8.9, 8.5, 9.2, 9.3, 9.1, 8.8),
final_score = c(9, 9.3, 9.4, 9, 9, 8.9, 9.25, 9.6, 8.8, 8.7),
registered = c(TRUE, FALSE, TRUE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE),
stringsAsFactors = FALSE)
##################################
# Shiny server
##################################
server <- function(input, output) {
#use our new function to create an output called formattableexample. We require an explicit call to the as.htmlwidget function as this does not register as an interactive environment
output$formattableexample <- renderFormattable({
formattable(df, list(
age = color_tile("white", "orange"),
grade = formatter("span",
style = x ~ ifelse(x == "A", style(color = "green", font.weight = "bold"), NA)),
test1_score = color_bar("pink"),
test2_score = color_bar("pink"),
final_score = formatter("span",
style = x ~ style(color = ifelse(rank(-x) <= 3, "green", "gray")),
x ~ sprintf("%.2f (rank: %02d)", x, rank(-x))),
registered = formatter("span",
style = x ~ style(color = ifelse(x, "green", "red")),
x ~ icontext(ifelse(x, "ok", "remove"), ifelse(x, "Yes", "No")))
))
})
}
##################################
# Shiny ui
##################################
ui <- fluidPage(
#use our new function to show the output we just defined.
fluidRow(
formattableOutput("formattableexample")
)
)
shinyApp(ui = ui, server = server)