0

I saw this app https://rich.shinyapps.io/regression/ and I wanted to do something similar displaying the regression output as html.

I divided my app in three parts as below.

server.R has no "engine" problems and the regression result is correct and pander is converting summary() to markdown correctly.

ui.R is also working ok but the markdown displayed after htmlOutput("model") is suppossed to be obtained from

output$model <- renderPrint({
        pander(summary(model()))
      })

in server.R, and the table is not well displayed even when its a valid markdown table.

How can I display my markdown summary as a common table like in R Markdown? my current output is:

enter image description here

Full MWE of the app

global.R

#library(shinyapps)
library(googleVis)
library(knitr)
library(pander)
library(shiny)
#library(shinysky)

ui.R

# Define UI for application 
shinyUI(fluidPage(

  # Application title

  titlePanel("Bivariate Regression"),

  # Sidebar 

  sidebarLayout(
    sidebarPanel(      
      textInput("name", label = h5("Name"), value = "Name"),
      HTML('</br>'),
      #selectInput("dataset", h5("Choose a dataset:"), choices = c("cars", "longley", "MLB","rock", "pressure")),        
      selectInput("dataset", h5("Choose a dataset:"), choices = c("cars", "longley","rock", "pressure")),        
      HTML('</br>'),
      uiOutput('dv'),    
      HTML('</br>'),
      uiOutput('iv'),
      HTML('</br>')),
      #radioButtons('format', h5('Document format'), c('PDF', 'HTML', 'Word'), inline = TRUE),
      #downloadButton('downloadReport')),
      #includeHTML('help.html')),

    # main panel 

    mainPanel(
      tabsetPanel(type = "tabs", 
                  tabPanel("Data",
                           HTML("</br>Select a data set from the 'Choose a dataset menu' or enter your own data below </br> </br>"),
                           numericInput("obs", label = h5("Number of observations to view"), 10),
                           tableOutput("view")),

                  tabPanel("Summary Statistics",
                           verbatimTextOutput("summary"),
                           textInput("text_summary", label = "Interpretation", value = "Enter text...")),

                  tabPanel("Model",                   
                           htmlOutput("model"),
                           textInput("text_model", label = "Interpretation", value = "Enter text..."))
      )                         
    ))
))

server.R

shinyServer(function(input, output) {

  # list of data sets
  datasetInput <- reactive({
    switch(input$dataset,
           "cars" = mtcars,
           "longley" = longley,
           "MLB" = mlb11,    
           "rock" = rock,
           "pressure" = pressure, 
           "Your Data" = df())
  })

  # dependent variable
  output$dv = renderUI({
    selectInput('dv', h5('Dependent Variable'), choices = names(datasetInput()))
  })

  # independent variable
  output$iv = renderUI({
    selectInput('iv', h5('Independent Variable'), choices = names(datasetInput()))
  })

  # regression formula
  regFormula <- reactive({
    as.formula(paste(input$dv, '~', input$iv))
  })

  # bivariate model
  model <- reactive({
    lm(regFormula(), data = datasetInput())
  })


  # create graphics 

  # data view 
  output$view <- renderTable({
    head(datasetInput(), n = input$obs)
  })

  # summary statistics
  output$summary <- renderPrint({
    summary(cbind(datasetInput()[input$dv], datasetInput()[input$iv]))
  })

  # bivariate model
  output$model <- renderPrint({
    pander(summary(model()))
  })

  # residuals
  output$residuals_hist <- renderPlot({
    hist(model()$residuals, main = paste(input$dv, '~', input$iv), xlab = 'Residuals') 
  })

  output$residuals_scatter <- renderPlot({
    plot(model()$residuals ~ datasetInput()[,input$iv], xlab = input$iv, ylab = 'Residuals')
    abline(h = 0, lty = 3) 
  })

  output$residuals_qqline <- renderPlot({
    qqnorm(model()$residuals)
    qqline(model()$residuals) 
  })

})
pachadotdev
  • 3,345
  • 6
  • 33
  • 60

0 Answers0