0

I want to display a html-table in a shiny page. The table is generated by a function and uses the following data:

rep_loop <- data.frame(Activity = LETTERS[1:3], ID_resource_rep = sample(1:10, 3),
  Div_resource_rep = sample(1:10, 3),
  ID_resource_loop = sample(1:10, 3),
  Div_resource_loop = sample(1:10, 3))

build_table <- function(data){
  html.table <- 
    tags$table(style = "border:1px solid black; padding: 3%; width: 80%",
      tags$tr(
        tags$th(" "),
        tags$th(colspan = 2, "Repeat (Act-X-Act)"),
        tags$th(colspan = 2, "Selfloop (Act-Act)")
      ),
      tags$tr(
        tags$th(" "),
        tags$th("Same Resource"),
        tags$th("Other Resource"),
        tags$th("Same Resource"),
        tags$th("Other Resource")
      ),
      tags$tr(
        tags$td(data$Activity),
        tags$td(data$ID_resource_rep),
        tags$td(data$Div_resource_rep),
        tags$td(data$ID_resource_loop),
        tags$td(data$Div_resource_loop)
      )
  )  
  return(html.table)
}

This is the result from

test <- build_table(rep_loop)

<table style="border:1px solid black; padding: 5%; width: 80%">
  <tr>
    <th> </th>
    <th colspan="2">Repeat (Act-X-Act)</th>
    <th colspan="2">Selfloop (Act-Act)</th>
  </tr>
  <tr>
    <th> </th>
    <th>Same Resource</th>
    <th>Other Resource</th>
    <th>Same Resource</th>
    <th>Other Resource</th>
  </tr>
  <tr>
    <td>A</td>
    <td>1</td>
    <td>1</td>
    <td>4</td>
    <td>6</td>
  </tr>
</table>
Warning messages:
1: In charToRaw(enc2utf8(text)) :
  argument should be a character vector of length 1
  all but the first element will be ignored
2: In charToRaw(enc2utf8(text)) :
  argument should be a character vector of length 1
  all but the first element will be ignored
3: In charToRaw(enc2utf8(text)) :
  argument should be a character vector of length 1
  all but the first element will be ignored
4: In charToRaw(enc2utf8(text)) :
  argument should be a character vector of length 1
  all but the first element will be ignored
5: In charToRaw(enc2utf8(text)) :
  argument should be a character vector of length 1
  all but the first element will be ignored

I would expect that build_table would return a table with a header, followed by data-lines. The header is as expected but instead of using all the rows from the data.frame, it shows only the first line, followed by 5 warnings.

Where did I make a mistake?

Ben

1 Answer 1

2

That's because you can't have a vector in a tags$td. Do:

rep_loop <- data.frame(Activity = LETTERS[1:3], ID_resource_rep = sample(1:10, 3),
                       Div_resource_rep = sample(1:10, 3),
                       ID_resource_loop = sample(1:10, 3),
                       Div_resource_loop = sample(1:10, 3))

build_table <- function(data){
  html.table <- 
    tags$table(style = "border:1px solid black; padding: 3%; width: 80%",
               tags$tr(
                 tags$th(" "),
                 tags$th(colspan = 2, "Repeat (Act-X-Act)"),
                 tags$th(colspan = 2, "Selfloop (Act-Act)")
               ),
               tags$tr(
                 tags$th(" "),
                 tags$th("Same Resource"),
                 tags$th("Other Resource"),
                 tags$th("Same Resource"),
                 tags$th("Other Resource")
               ),
               mapply(function(x1,x2,x3,x4,x5){
                 tags$tr(
                   tags$td(x1),
                   tags$td(x2),
                   tags$td(x3),
                   tags$td(x4),
                   tags$td(x5)
                 )
               }, data$Activity,
                  data$ID_resource_rep,
                  data$Div_resource_rep,
                  data$ID_resource_loop,
                  data$Div_resource_loop, 
               SIMPLIFY = FALSE)
    )  
  return(html.table)
}

build_table(rep_loop)

<table style="border:1px solid black; padding: 3%; width: 80%">
  <tr>
    <th> </th>
    <th colspan="2">Repeat (Act-X-Act)</th>
    <th colspan="2">Selfloop (Act-Act)</th>
  </tr>
  <tr>
    <th> </th>
    <th>Same Resource</th>
    <th>Other Resource</th>
    <th>Same Resource</th>
    <th>Other Resource</th>
  </tr>
  <tr>
    <td>A</td>
    <td>5</td>
    <td>10</td>
    <td>4</td>
    <td>2</td>
  </tr>
  <tr>
    <td>B</td>
    <td>6</td>
    <td>8</td>
    <td>3</td>
    <td>3</td>
  </tr>
  <tr>
    <td>C</td>
    <td>3</td>
    <td>7</td>
    <td>2</td>
    <td>5</td>
  </tr>
</table>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.