Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With xtable and type = html how to add a class to a specific td tag

I'm trying to create a table in html with xtable, but I need to add a class to specific td tag, because I going to do an animation. The problems is that I can't do it without xtable because it is so slow.

May be I need to represent this but with 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)
}

If I do it with xtable my app takes 15sec but if I do it with myRederTable function my app takes 2 minutes, so how can I do to put this class in a td with xtable.

I'm working with R and shiny.

like image 434
user2029940 Avatar asked Mar 30 '13 15:03

user2029940


People also ask

How do I add a class to TD?

Code explanation Two CSS classes are defined in the <style> element. The class attribute in <td> assigns one classname. Repeatedly clicking the button calls JavaScript which locates all the <td> tags using classnames. It then toggles the another class, changing the background and text color.

How to use td tag in HTML?

The <td> tag defines the standard cells in the table which are displayed as normal-weight, left-aligned text. The <tr> tag defines the table rows. There must be at least one row in the table. The <th> tag defines the header cells in the table which are displayed as bold, center-aligned text.

What is td element in HTML?

<td>: The Table Data Cell element. The <td> HTML element defines a cell of a table that contains data.


Video Answer


1 Answers

The problem is that you are growing a string: each time you append to it, it has to be copied to a new memory location. It is faster to build the data first, as an array, and only then to convert it 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" )
like image 184
Vincent Zoonekynd Avatar answered Oct 19 '22 11:10

Vincent Zoonekynd