Pergunta

I am wondering how I can create a (html) table cell by cell with dynamic content in shiny? right now I am using the following combination:

server.R

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

ui.R

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

This works well. I would like to set links to some cells and also add some additional layout setting to the table like bold, without border etc. and also don't want to the row numbers at the front.

How can I do this? I tried the HTML() command, but it did't work. Thanks for your help.

Foi útil?

Solução

If you want to use renderTable the easiest way to style your table is using css. Removing the row numbers requires passing the option include.rownames = FALSE to print.xtable. There is a ... argument in the renderTable function that does this. You can include html in your table and use the sanitize.text.function argument.

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)
  }
))

Alternatively look at renderDataTable which allows you to use http://datatables.net/.

Outras dicas

If you know that your table will be static and that only the content will be dynamic, you could follow my approach annotated here: Shiny - populate static HTML table with filtered data based on input

In short, I build a static html table and wrap it in a function in a seperated R file, source it in the server and call it in the renderUI() function with the newly filtered data. Thus, the table content gets updated with the user input.

A future project will be a function that allows the user to generate a static html table in a dynamic way, e.g., a function that creates a table with X rows, Y columns, rownames[], colnames[], etc. If I succeed, I will post my solution here.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top