RShiny:创建一个数据表,该数据表既可以在单元格中显示图像,又可以在顶部显示过滤功能

我想在我的数据表之上添加一个简单的过滤器。应该可以实现如下:

##### server:    
output$table <- DT::renderDT(data,filter="top")
##### ui:
DT::DTOutput('table')

##### server: 
output$table <- DT::renderDataTable(data,filter="top")
##### ui:
DT::dataTableOutput('table')

但是,我还有一列,其中的图像嵌入“数据”表中。在以上功能中,这些图像被证明是文本。图像显示在以下功能中,但是随后滤镜消失:

##### server:
output$table <- DT::renderDataTable(
DT::datatable(data,escape=FALSE),filter="top")
##### ui:
DT::dataTableOutput('table')

如何在用户界面中同时包含图片和过滤器?

奖金:我最初利用formattable和许多其他功能来修饰表格文本。我在某处读到那个formattable,并且过滤器到目前为止还不能一起工作,因此功能(过滤器)的美观(彩色文本)有所减少。 Formattable也允许显示图像。因此,如果有人有允许使用filter格式化表格的方法,那也将彻底解决问题!

lhbfqlz 回答:RShiny:创建一个数据表,该数据表既可以在单元格中显示图像,又可以在顶部显示过滤功能

假设您在pic1.png子文件夹中有两个图像pic2.pngwww。然后,您可以这样做:

library(shiny)
library(DT)

dat <- data.frame(
  X = c(1,2),Y = c("pic1","pic2")
)

render <- c(
  "function(data,type,row){","  if(type === 'display'){","    var tag = '<img src=\"' + data + '.png\" width=\"100\"/>';","    return tag;","  } else {","    return data;","  }","}"
)

ui <- fluidPage(
  br(),DTOutput("dtable")
)

server <- function(input,output,session){

  output[["dtable"]] <- renderDT({
    datatable(
      dat,rownames = FALSE,filter = "top",options = list(
        columnDefs = list(
          list(targets = "_all",className = "dt-center"),list(targets = 1,render = JS(render)) # 1 is the index of the column of images
        )
      )
    )
  })

}

shinyApp(ui,server)

enter image description here

本文链接:https://www.f2er.com/2761199.html

大家都在问