Output a good-looking matrix using renderTable()

前端 未结 3 1424
执念已碎
执念已碎 2020-12-05 05:52

I am creating a matrix in my server. I would like to then output this matrix on the screen using renderTable(). (I create it in the server because its length (among others)

相关标签:
3条回答
  • 2020-12-05 06:11

    You can start fiddling with CSS, but for quick work the googleVis package is nice. Additional options to add decorations can be found in the documentation.

    shiny::runApp(
      list(
        ui = pageWithSidebar(
    
          headerPanel("TEST"),
    
          sidebarPanel(
            helpText('This matrix is quite nice:')
          ),
    
          mainPanel(    
            uiOutput('matrix')     
          )
        )
        , 
        server = function(input,output){
          library(googleVis)
          output$matrix <- renderGvis({
            df <- as.data.frame(matrix(rnorm(9),nrow=3))      
            rownames(df) <- c('a','b','c')
            gvisTable(df);
    
          })
        }
      )
    )
    
    0 讨论(0)
  • 2020-12-05 06:34

    Mathjax rendering:

    library(xtable)
    
    shiny::runApp(
      list(
        ui = pageWithSidebar(
    
          headerPanel("TEST"),
    
          sidebarPanel(
            helpText('Is this matrix cool ?')
          ),
    
          mainPanel(    
            uiOutput('matrix')     
          )
        )
        , 
        server = function(input,output){
          output$matrix <- renderUI({
            M <- matrix(rep(1,6),nrow=3)
            rownames(M) <- c('a','b','c')
            M <- print(xtable(M, align=rep("c", ncol(M)+1)), 
                              floating=FALSE, tabular.environment="array", comment=FALSE, print.results=FALSE)
            html <- paste0("$$", M, "$$")
            list(
              tags$script(src = 'https://c328740.ssl.cf1.rackcdn.com/mathjax/2.0-latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML', type = 'text/javascript'),
              HTML(html)
            )
          })
        }
      )
    )
    

    enter image description here

    Update July 2015

    Something has changed and the MathJax rendering does not work anymore. Maybe this is the link to the MathJax library, I don't know. Anyway, there's a new function in Shiny, withMathJax, which does the job. Replace the server function by the following one:

    server = function(input,output){
        output$matrix <- renderUI({
            M <- matrix(rep(1,6),nrow=3)
            rownames(M) <- c('a','b','c')
            M <- print(xtable(M, align=rep("c", ncol(M)+1)), 
                       floating=FALSE, tabular.environment="array", comment=FALSE, print.results=FALSE)
            html <- paste0("$$", M, "$$")
            list(
                withMathJax(HTML(html))
            )
        })
    }
    
    0 讨论(0)
  • 2020-12-05 06:34

    For rownames in googleVis package use:

     shiny::runApp(
      list(
    ui = pageWithSidebar(
    
      headerPanel("TEST"),
    
      sidebarPanel(
        helpText('This matrix is quite nice:')
      ),
    
      mainPanel(    
        uiOutput('matrix')     
      )
    )  
    , 
    server = function(input,output){
      library(googleVis)
      output$matrix <- renderGvis({
        df <- as.data.frame(matrix(rnorm(9),nrow=3))      
        df <- cbind(' ' = c('a','b','c'),df) 
        gvisTable(df);
    
      })
    }
      )
    )
    
    0 讨论(0)
提交回复
热议问题