我正在嘗試更改 Shiny App 中資料表的頁腳。我可以使用以下代碼復制我在應用程式中遇到的錯誤:
dt_test <- tibble(cntry = c("A","A","B"),
city = c("X","Y","Z"),
sales = c(1000,1500,500),
score = c(1.1234,5.1234,2.1234))
footer <- sapply(dt_test, function(x) ifelse((is.numeric(x)), sum(x), ""))
sketch <- htmltools::withTags(table(
tableHeader(dt_test),
tableFooter(footer)
))
sketch
R 顯示了這一點:
Error in writeImpl(text) :
Text to be written must be a length-one character vector
但是,如果我直接將頁腳的定義作為引數,它會起作用:
sketch <- htmltools::withTags(table(
tableHeader(dt_test),
tableFooter(sapply(dt_test, function(x) ifelse( (is.numeric(x)),sum(x), "" ))
)))
不幸的是,我不能使用這種方法,因為聚合包含許多業務邏輯,并在單獨的函式中執行。我在這里做錯了什么?
uj5u.com熱心網友回復:
問題是footer標簽的名稱,即<footer>標簽。因此,當使用footerinside 時,htmltools::withTags您實際上是將tags$footer(這是一個函式)傳遞給 tableFooter而不是存盤在矢量頁腳中的內容。這就是您收到錯誤訊息的原因,這也是您直接傳遞定義時代碼作業的原因。
這表示有兩個選項可以使您的代碼正常作業:
選項 1:使用htmltools::tags$...代替htmltools::withTags
library(tibble)
library(DT)
sketch <- htmltools::tags$table(
tableHeader(dt_test),
tableFooter(footer)
)
選項 2:重命名變數
footer1 <- footer
sketch <- htmltools::withTags(table(
tableHeader(dt_test),
tableFooter(footer1)
))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/340023.html
上一篇:R中兩個二項式分布的交集
下一篇:在r中的一行中列印多個變數
