如何将DT格式的输出转换为具有突出显示的单元格的数据框架

发布于 2025-02-01 09:11:53 字数 2216 浏览 3 评论 0原文

我试图根据其值在数据框中为特定的细胞上色。

我已经设法使用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)

输出的屏幕截图:

Shiny输出表

这是一些示例代码,由于格式输出不是一个数据框:

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:

Shiny output table

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眉目亦如画i 2025-02-08 09:11:53

它表明颜色量级已经是数据表。因此,将选项转移到frighightdata部分将起作用。

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,
                               options = list(
                                 scrollX = TRUE,
                                 autoWidth = FALSE,
                                 dom = 'Blrtip'
                               )) %>% 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)

It shows that colourWeights is already a datatable. Thus, moving the options to the highlightData part will work.

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,
                               options = list(
                                 scrollX = TRUE,
                                 autoWidth = FALSE,
                                 dom = 'Blrtip'
                               )) %>% 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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文