Render regression table?

839 views
Skip to first unread message

ignacio martinez

unread,
Dec 7, 2015, 9:09:16 AM12/7/15
to Shiny - Web Framework for R

Hi,

What is the best way to render a regression table in shiny? I could generate a html table using stargazer

    set.seed(1272015)
    n<-1000
    x1 <- rnorm(n = n, mean = 7, sd = 2)
    x2 <- rnorm(n = n, mean = 4, sd = 2)
    treatment <- sample(x = c(1,0), size = n, replace = T, prob=c(0.3,0.7))
    y <- 2.1234*x1 + 1.1234*x2 + 0.5*treatment + rnorm(n, mean=0, sd=1)
    mydata = data.frame(x1, x2, treatment, y)
    lm1 <- lm(formula = y~x1+x2+treatment, data=mydata)
    
    stargazer::stargazer(lm1, type="html")

but, I'm not sure how to render it in shiny. For example:

    ## app.R ##
    library(shiny)
    library(shinydashboard)
    library(stargazer)
    
    ui <- dashboardPage(
      dashboardHeader(title = "Basic dashboard"),
      dashboardSidebar(),
      dashboardBody(
        # Boxes need to be put in a row (or column)
        fluidRow(
          box(title = "Regression table", renderUI(lm1))
        )
      )
    )
    
    server <- function(input, output) { 
      lm1 <- htmlOutput(stargazer(lm1, type="html"))
      }
    
    shinyApp(ui, server)
    
Thanks for the help!

ignacio martinez

unread,
Dec 7, 2015, 9:21:38 AM12/7/15
to Shiny - Web Framework for R
This works:

    ## app.R ##
    library(shiny)
    library(shinydashboard)
    library(stargazer)
    
    ui <- dashboardPage(
      dashboardHeader(title = "Basic dashboard"),
      dashboardSidebar(),
      dashboardBody(
        # Boxes need to be put in a row (or column)
        fluidRow(
          box(title = "Regression table", uiOutput("lm1"))
        )
      )
    )
    
    server <- function(input, output) { 
      output$lm1 <- renderText(stargazer(lm1, type="html"))
      }
    
    shinyApp(ui, server)

Is there a better way?

Thanks!

Joe Cheng

unread,
Dec 7, 2015, 12:51:59 PM12/7/15
to ignacio martinez, Shiny - Web Framework for R
This is fine, you could also do:

output$lm1 <- renderUI(HTML(stargazer(lm1, type="html")))

The extra HTML() call is to notify Shiny that the stargazer return value should be treated as raw markup instead of text to be escaped.


--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/9d82727b-b4c4-47ec-be8d-1d711393ef8b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages