0

I am working on an R Shiny app to run a linear regression on user-input data. The app outputs a summary of the regression as well as some related results -- such as R-squared, the Mean Absolute Percentage Error and the Durbin Watson Statistic. The code also computes the share of total weight for each independent variable.

The app first asks the user to upload a CSV file. The it asks for the dependent and independent variables to run the regression as well as other statistics mentioned above. Minimum reproducible code is provided below, please use any of these variables as the dependent variable: mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb.

    library(shiny)
library(DT)
library("lmtest")
shinyUI <- pageWithSidebar(
  headerPanel("Linear Regression Model"),
  sidebarPanel(
    textInput("response", label=h4 ("Enter Dependent Variable")),
    uiOutput("drop"),
    uiOutput("drop_media"),
    uiOutput("choose_columns")
  ),
  mainPanel(
    tableOutput("MAPE"),
    tableOutput("contributions"),
    tableOutput("media_contributions")
  )
)

       library(shiny)

shinyServer <- function(input, output,session) {

  dsnames <- c()

  data_set <- dput(mtcars)
  output$choose_dataset <- renderUI({
    selectInput("dataset", "Data set", as.list(data_set))
  })
  output$drop <- renderUI({
    selectizeInput("drop", label = "Enter Independent Variables", choices = names(data_set), multiple= T)
  })
  output$drop_media <- renderUI({
    selectizeInput("drop_media", label = "Enter Media Variables Only", choices = names(data_set), multiple= T)
  })
  # Check boxes
  output$choose_columns <- renderUI({
    # If missing input, return to avoid error later in function
    if(is.null(input$dataset))
      return()

    # Get the data set with the appropriate name

    colnames <- names(contents)

    # Create the checkboxes and select them all by default
    checkboxGroupInput("columns", "Choose columns", 
                       choices  = colnames,
                       selected = colnames)
  })
  runRegression <- reactive({
    lm(as.formula(paste(input$response," ~ ",paste(input$drop,collapse="+",-1))), data = data_set)
  })
  computeMAPE<-reactive({
    (round(sum(abs(resid(runRegression())))/sum(data_set[input$response]), 4))*100
  })
  output$MAPE<-renderTable({
    if(!is.null(input$drop)){
      data.frame(MAPE=paste0(computeMAPE(),"%"))
    }else{
      print(data.frame(MAPE="Input Pending"))
    }
  })
  output$regTab <- DT::renderDataTable({
    if(!is.null(input$drop)){
      summary(runRegression())$coefficients
    } else {
      print(data.frame(Summary="Input Pending"))
    }
  })
  output$lmStats <- renderTable({
    if(!is.null(input$drop)){
      results <- summary(runRegression())
      data.frame(R2=results$r.squared)
    }else{
      print(data.frame(R2="Input Pending")) 
    }
  })
  output$tests<-renderTable({
    if(!is.null(input$drop)){
      dwtest<-dwtest(runRegression())
      data.frame(DWStat=dwtest$statistic,pValue=dwtest$p.value)
    }else{
      print(data.frame(DWStat="Input Pending"))
    }
  })

  output$contributions <- renderTable({
    if(!is.null(input$drop)){
      df <- data_set[input$drop]
      cols <- names(df)
      coeff <- t(data.frame(coff =abs((runRegression())$coefficients)))
      for(i in 1:length(cols)){
        df[i] <- df[i]*coeff[i]
      }
      df2 <- data.frame(Weight = colSums(df))
      y <- colSums(df2)
      df2$Contribution = (df2$Weight/y)*100
      df2
    }else{
      print(data.frame(Overall_Contribution ="Input Pending"))  
    }
  })
  output$media_contributions <- renderTable({
    if(!is.null(input$drop_media)){
      df <- data_set[input$drop_media]
      cols <- names(df)
      coeff <- t(data.frame(coff =abs((runRegression())$coefficients)))
      for(i in 1:length(cols)){
        df[i] <- df[i]*coeff[i]
      }
      df2 <- data.frame(Weight = colSums(df))
      y <- colSums(df2)
      df2$Contribution = (df2$Weight/y)*100
      df2
    }else{
      print(data.frame(Media_Contribution ="Input Pending")) 
    }
  })
}

This code is working fine on my computer. It runs the regression and outputs all the results as desired. However, when I run this same code on a different computer (different version of R), I run into an issue with the parts that require explicit computation -- that is, the MAPE function and the contributions function. The issue is that while the regTab, R-squared, and DW statistic show up correctly, the MAPE table only says NA% once the app launches, and the contribution tables are simply blank.

I have tried installing any math/stats packages that I suspected might be missing and checked the dataset for any formatting issues. I also tried running the code in showcase mode, but it did not help. The problem is specific to Shiny -- the same code works when run outside of the app.

My version of R (where this code works) is 3.2.4 (R-Studio version 0.99.893). A version of R where this code doesn't work is: 3.3.1 (R-Studio version 0.99.903).

Any idea why this block of code would work on certain versions of R and not on others?

Thank you for your time and help!

HH T
  • 1
  • 1
  • What is the version installed on your local machine? What is the other version involved? Include the version numbers of the packages you are using. Try to make your reproducible example as minimal as possible. Is this problem specific to shiny? If not, remove all that. – MrFlick Oct 11 '16 at 14:09
  • Thanks, made edits to the original question. The problem is specific to Shiny. – HH T Oct 11 '16 at 14:23
  • `0.99.893` isn't an R version number. The current version is R `3.3.1` right now. That's not a Shiny version number either which is currently on `0.14.1`. Where did you get that number? Also, the code you supplied isn't reproducible because there is not input data. See [how to create a minimal reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – MrFlick Oct 11 '16 at 14:40
  • You're right, I had included R-Studio versions by mistake. Corrected that and included minimum reproducible code this time. – HH T Oct 11 '16 at 15:22

0 Answers0