如何将DT格式的输出转换为具有突出显示的单元格的数据框架
我试图根据其值在数据框中为特定的细胞上色。
我已经设法使用DT库中的格式函数成功地突出了单元格,但是FormatStyle的输出格式似乎是一个列表,这是一个问题,因为我现在想将格式化选项添加到RenderDatabable函数(例如,例如使用scrollx = true)滚动栏。
是否有将输出从格式化到数据框架的某种方法?
因此,这里有一些可重现的示例代码,可以有效:
library(shiny)
library(reticulate)
library(DT)
ui <- fluidPage(
mainPanel(
# first header title
h3("MTCars"),
# prepare the first output table
DT::dataTableOutput('table1'),
)
)
server <- function(input, output, session) {
myData <- mtcars
myData$wheelDiameter <- myData$wt
myData$windscreenHeight <- myData$mpg
myData$carTint <- myData$vs
myData$color <- rep(c("red","black","green","yellow"),4)
colourWeights <- reactive({
highlightData <- datatable(myData) %>% formatStyle(
'wt',
backgroundColor = styleInterval(c(1.5,3.0), c("red","yellow","green")),
fontWeight = 'bold'
)
return(highlightData)
})
# display the first output table
output$table1 <- DT::renderDataTable({
colourWeights()
})
}
shinyApp(ui, server)
输出的屏幕截图:
这是一些示例代码,由于格式输出不是一个数据框:
library(shiny)
library(reticulate)
library(DT)
ui <- fluidPage(
mainPanel(
# first header title
h3("MTCars"),
# prepare the first output table
DT::dataTableOutput('table1'),
)
)
server <- function(input, output, session) {
myData <- mtcars
myData$wheelDiameter <- myData$wt
myData$windscreenHeight <- myData$mpg
myData$carTint <- myData$vs
myData$color <- rep(c("red","black","green","yellow"),4)
colourWeights <- reactive({
highlightData <- datatable(myData) %>% formatStyle(
'wt',
backgroundColor = styleInterval(c(1.5,3.0), c("red","yellow","green")),
fontWeight = 'bold'
)
return(highlightData)
})
# display the first output table
output$table1 <- DT::renderDataTable({
datatable(colourWeights(),
options = list(
scrollX = TRUE,
autoWidth = FALSE,
dom = 'Blrtip'
)
)
})
}
shinyApp(ui, server)
这是我得到的错误:
预先感谢
I am trying to colour specific cells in a data frame with RShiny, based on their values.
I have managed to highlight the cells successfully using the formatStyle function from the DT library, however the output format of formatStyle appears to be a list, which is a problem since I would now like to add formatting options to the renderDataTable function (such as the scroll bar using scrollX=TRUE).
Is there someway of transforming the output from formatStyle to a data frame?
So here is some reproducible example code which works:
library(shiny)
library(reticulate)
library(DT)
ui <- fluidPage(
mainPanel(
# first header title
h3("MTCars"),
# prepare the first output table
DT::dataTableOutput('table1'),
)
)
server <- function(input, output, session) {
myData <- mtcars
myData$wheelDiameter <- myData$wt
myData$windscreenHeight <- myData$mpg
myData$carTint <- myData$vs
myData$color <- rep(c("red","black","green","yellow"),4)
colourWeights <- reactive({
highlightData <- datatable(myData) %>% formatStyle(
'wt',
backgroundColor = styleInterval(c(1.5,3.0), c("red","yellow","green")),
fontWeight = 'bold'
)
return(highlightData)
})
# display the first output table
output$table1 <- DT::renderDataTable({
colourWeights()
})
}
shinyApp(ui, server)
And a screen shot of the output:
And here is some example code which doesn't work since the formatStyle output is not a dataframe:
library(shiny)
library(reticulate)
library(DT)
ui <- fluidPage(
mainPanel(
# first header title
h3("MTCars"),
# prepare the first output table
DT::dataTableOutput('table1'),
)
)
server <- function(input, output, session) {
myData <- mtcars
myData$wheelDiameter <- myData$wt
myData$windscreenHeight <- myData$mpg
myData$carTint <- myData$vs
myData$color <- rep(c("red","black","green","yellow"),4)
colourWeights <- reactive({
highlightData <- datatable(myData) %>% formatStyle(
'wt',
backgroundColor = styleInterval(c(1.5,3.0), c("red","yellow","green")),
fontWeight = 'bold'
)
return(highlightData)
})
# display the first output table
output$table1 <- DT::renderDataTable({
datatable(colourWeights(),
options = list(
scrollX = TRUE,
autoWidth = FALSE,
dom = 'Blrtip'
)
)
})
}
shinyApp(ui, server)
This is the error I get:
Error: 'data' must be 2-dimensional (e.g. data frame or matrix
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它表明
颜色量级
已经是数据表。因此,将选项转移到frighightdata
部分将起作用。It shows that
colourWeights
is already a datatable. Thus, moving the options to thehighlightData
part will work.