【问题标题】:self created tables with shiny闪亮的自制桌子
【发布时间】:2014-06-19 13:59:36
【问题描述】:

我想知道如何通过闪亮的动态内容逐个单元格地创建(html)表格单元格? 现在我正在使用以下组合:

server.R

output$desc <- renderTable(
  hdx.desc()
)

ui.R

tabsetPanel(
  tabPanel("Description", tableOutput("desc"))
)

这很好用。我想设置一些单元格的链接,并在表格中添加一些额外的布局设置,如粗体、无边框等,也不希望在前面的行号。

我该怎么做?我尝试了 HTML() 命令,但它不起作用。 感谢您的帮助。

【问题讨论】:

    标签: html r shiny shiny-server


    【解决方案1】:

    如果您想使用renderTable 来设置表格样式,最简单的方法是使用 css。删除行号需要将选项include.rownames = FALSE 传递给print.xtablerenderTable 函数中有一个 ... 参数可以执行此操作。您可以在表格中包含 html 并使用 sanitize.text.function 参数。

    runApp(list(
      ui = bootstrapPage(
        tableOutput("myTable")
        , tags$head(tags$style(type="text/css", 
    "#myTable table th td {
    border: 1px solid black !important;
    }
    #myTable table th
    {
    background-color:green;
    color:white;
    }"
    ))
    
      ),
      server = function(input, output) {
        output$myTable <- renderTable({ 
          temp = c(runif(4), 
                   as.character(tags$a(id = 'myId', href='http://www.example.com', runif(1)))
                   )
          data.frame(date=seq.Date(Sys.Date(), by=1, length.out=5), temp = temp)
          }, include.rownames = FALSE, sanitize.text.function = function(x) x)
      }
    ))
    

    或者查看renderDataTable,它允许您使用http://datatables.net/

    【讨论】:

    • 感谢您的建议。当我将您的想法应用于我的问题output$desc &lt;- renderTable({ x &lt;- hdx.desc() x[5,2] &lt;- as.character(tags$a(id = 'myId', href=x[5,2], target='_blank', x[5,2])) }, include.rownames = FALSE, sanitize.text.function = function(x) as.data.frame(x) ) for server.R 并在 ui.R 中使用 tabPanel("Description", tableOutput("desc") 调用它时,x 都是 chr,然后我收到以下错误消息:Error: no applicable method for 'xtable' applied to an object of class "c('html', 'character')" 知道如何处理吗?谢谢!
    • 你需要在renderTable中返回x。您还更改了sanitize.text.function 的定义。
    【解决方案2】:

    如果您知道您的表格是静态的并且只有内容是动态的,您可以按照我在此处注释的方法:Shiny - populate static HTML table with filtered data based on input

    简而言之,我构建了一个静态 html 表并将其包装在一个单独的 R 文件中的函数中,在服务器中获取它并使用新过滤的数据在 renderUI() 函数中调用它。因此,表格内容会随着用户输入而更新。

    未来的项目将是一个允许用户以动态方式生成静态 html 表的函数,例如,创建具有 X 行、Y 列、rownames[]、colnames[] 等的表的函数。如果我成功了,我会在这里发布我的解决方案。

    【讨论】:

      猜你喜欢
      • 2021-04-19
      • 1970-01-01
      • 2016-12-23
      • 2020-10-16
      • 1970-01-01
      • 1970-01-01
      • 2018-06-07
      • 1970-01-01
      相关资源
      最近更新 更多