Mahdi Moqri
Mahdi Moqri

Reputation: 27

R Shiny: Rendering summary.ivreg output

I'm trying to render an instrumental variable regression summary in R Shiny

Here is the code:

iv=ivreg(lwage~educ+exper|nearc4+exper)
summary(iv)

When I use renderTable I get the following error: no applicable method for 'xtable' applied to an object of class "summary.ivreg"

Any suggestions how to go around this issue?

This is my website, if you want to see what I'm doing exactly: https://ieconometrics.shinyapps.io/test/

Upvotes: 3

Views: 2775

Answers (1)

Victorp
Victorp

Reputation: 13856

renderTable expect an object for which a xtable methods exist, you can see methods avaible with : methods(xtable), and it doen't work with summary.ivreg, you can build a method yourself or obtain a result like these with the code below :

enter image description here

library(shiny)
library(AER)
library(ReporteRs)

# define server
server <- function(input, output) {

  output$raw_summary <- renderPrint({
    fm <- ivreg(log(packs) ~ log(rprice) + log(rincome) | log(rincome) + tdiff + I(tax/cpi),
                data = CigarettesSW, subset = year == "1995")
    print(summary(fm))
  })

  output$summary_table <- renderUI({
    fm <- ivreg(log(packs) ~ log(rprice) + log(rincome) | log(rincome) + tdiff + I(tax/cpi),
    data = CigarettesSW, subset = year == "1995")
    data = summary(fm)$coefficients
    data = as.data.frame(data)
    # get signif codes
    signif.codes = cut( data[,4]
                    , breaks = c( -Inf, 0.001, 0.01, 0.05, Inf)
                    , labels= c("***", "**", "*", "" ) )

    # format the data values
    data[, 1] = formatC( data[, 1], digits=3, format = "f")
    data[, 2] = formatC( data[, 2], digits=3, format = "f")
    data[, 3] = formatC( data[, 3], digits=3, format = "f")
    data[, 4] = ifelse( data[, 4] < 0.001, "< 0.001", formatC( data[, 4], digits=5, format = "f"))
    # add signif codes to data
    data$Signif = signif.codes

    # create an empty FlexTable
    coef_ft = FlexTable( data = data, add.rownames=TRUE
                     , body.par.props = parRight(), header.text.props = textBold()
                     , header.columns = T
    )
    # center the first column and set text as bold italic
    coef_ft[,1] = parCenter()
    coef_ft[,1] = textBoldItalic()

    # define borders
    coef_ft = setFlexTableBorders( coef_ft
                               , inner.vertical = borderNone(), inner.horizontal = borderDotted()
                               , outer.vertical = borderNone(), outer.horizontal = borderSolid()
    )
    return(HTML(as.html(coef_ft)))
  })
}

# define ui
ui <- shinyUI(fluidPage(
  p("Raw summary"),
  verbatimTextOutput(outputId = "raw_summary"),

  p("Pretty model summary table :"),
  uiOutput(outputId = "summary_table")
))

# Call the app
shinyApp(ui = ui, server = server)

Upvotes: 3

Related Questions