【问题标题】:With xtable and type = html how to add a class to a specific td tag使用 xtable 和 type = html 如何将类添加到特定的 td 标签
【发布时间】:2013-03-21 03:31:54
【问题描述】:

我正在尝试使用 xtable 在 html 中创建一个表格,但我需要将一个类添加到特定的 td 标签,因为我要做一个动画。问题是没有 xtable 我不能这样做,因为它太慢了。

可能我需要用 xtable 来表示这个。

myRenderTable<-function(){
  table = "<table>"
  for(i in 1:4862){
    table = paste(table,"<tr><td>",i,"</td>",sep="")
    for(j in 1:5){

      if(j == 5){
        table = paste(table,"<td class ='something'>",i+j,"</td>",sep="")  
      }
      else{
        table = paste(table,"<td>",i+j,"</td>",sep="")
      }
    }
    table = paste(table,"</tr><table>")
  }
  return(table)
}

如果我使用 xtable 执行此操作,我的应用程序需要 15 秒,但如果我使用 myRederTable 函数执行此操作,我的应用程序需要 2 分钟,那么如何使用 xtable 将此类放入 td

我正在使用 R 和闪亮。

【问题讨论】:

  • 我们能否获取一些示例数据以及所需的输出应该是什么样的?

标签: html css r xtable shiny


【解决方案1】:

问题是你正在增长一个字符串: 每次附加到它时,都必须将其复制到新的内存位置。 首先构建数据更快,作为一个数组, 然后才将其转换为 HTML。

# Sample data
n <- 4862
d <- matrix( 
  as.vector( outer( 0:5, 1:n, `+` ) ),
  nr = 10, nc = 6*n, byrow=TRUE
)
html_class <- ifelse( col(d) %% 6 == 0, " class='something'", "" )

# The <td>...</td> blocks
html <- paste( "<td", html_class, ">", d, "</td>", sep="" )
html <- matrix(html, nr=nrow(d), nc=ncol(d))

# The rows
html <- apply( html, 1, paste, collapse = " " )
html <- paste( "<tr>", html, "</tr>" )

# The table
html <- paste( html, collapse = "\n" )
html <- paste( "<table>", html, "</table>", sep="\n" )

【讨论】:

  • 感谢您的回答,但是该应用程序需要花费大量时间才能使用您的源代码运行它,这就是继续使用 xtable 的原因,如果有人知道其他选项,请告诉我。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-04
  • 2022-10-06
  • 2012-05-07
  • 1970-01-01
  • 2013-03-17
  • 1970-01-01
相关资源
最近更新 更多