2014-12-02 224 views

回答

1

renderTable期待其存在的xtable方法,你可以看到avaible對象與方法:methods(xtable),它DOEN 「T與summary.ivreg工作,你可以建立自己的方法或獲得這樣的結果與下面的代碼:

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) 
+0

謝謝Victo RP,這太棒了! 我還發現stargazer(),它給了我一個可以使用renderUI渲染的輸出。你的代碼很難給我更多的選擇來改變輸出 – 2014-12-02 20:02:00

+0

Thanks @victorp – 2017-03-18 03:23:27