1
require(shiny)
setwd("C://Users//Harshad//Desktop//Equiskill - BA")
cars <- read.table(file = "cars.csv", sep = ",", header = TRUE, quote="")
cars <- cars[,2:7]

ui <- fluidPage(      
   "Simple Linear Regression Modelling",

    sidebarPanel(width = 2,

    selectInput(inputId = "d_var",
                label = "Select dependent variable:",
                choices = c(names(cars))),
    selectInput(inputId = "ind_var",
                label = "Select independent variable:",
                choices = c(names(cars)))
  )
   ,
  tableOutput("summary")    
)

server <- function(input,output) {    
   model <-  reactive (lm(input$d_var ~ input$ind_var, data=cars))    
    output$summary <- renderDataTable(
      {  summary(model)    }
      ) 
}
shinyApp(ui <- ui, server <- server)

I am getting an error "object of type 'closure' is not subsettable" while executing code in R. please help.

I am getting an error "object of type 'closure' is not subsettable" while executing code in R. please help.

Harshad Patil
  • 313
  • 1
  • 3
  • 13
  • `summary(model())` – Jean Feb 13 '17 at 08:09
  • 2
    Possible duplicate of [What is “object of type ‘closure’ is not subsettable” error in Shiny?](https://stackoverflow.com/questions/40623749/what-is-object-of-type-closure-is-not-subsettable-error-in-shiny) – John Paul May 23 '19 at 20:34

1 Answers1

0

Something like this should do the trick

require(shiny)
setwd("C://Users//Harshad//Desktop//Equiskill - BA")
cars <- read.table(file = "cars.csv", sep = ",", header = TRUE, quote="")
cars <- cars[,2:7]

ui <- fluidPage(      
  "Simple Linear Regression Modelling",
  sidebarPanel(width = 2,
               selectInput(inputId = "d_var",label = "Select dependent variable:",choices = c(names(cars))),
               selectInput(inputId = "ind_var",label = "Select independent variable:",choices = c(names(cars)))
  ),verbatimTextOutput("summary")    
)

server <- function(input,output) {    
  model <- reactive({
    lm(cars[,names(cars) %in% input$d_var] ~ cars[,names(cars) %in% input$ind_var])
    })

  output$summary <- renderPrint({summary(model())}) 
}
shinyApp(ui <- ui, server <- server)
Pork Chop
  • 28,528
  • 5
  • 63
  • 77
  • Is this the standard syntax to define lm function within shiny app? Like you defined in above example. model <- reactive({ lm(cars[,names(cars) %in% input$d_var] ~ cars[,names(cars) %in% input$ind_var]) }) – Harshad Patil Feb 13 '17 at 13:43
  • No, but the sub-setting part you're doing needs to be like that. `lm` needs objects which contain information to do its job. `input$d_var` is a single character object which isnt what it needs. You can put the model definitions into separate variable if you want to get the names out. – Pork Chop Feb 13 '17 at 13:50
  • Thanks for reply. What if I have to display only certain elements of summary function like r-squared, significance factor etc. rather than displaying entire output? Actually I am trying to create an shiny App for linear regression but I am relatively new to R. – Harshad Patil Feb 13 '17 at 14:50
  • you can see how to access the elements of the `lm` function by using the dollar sign. You can then combine it to have it whatever you want. Before jumping into `shiny` try to write everything in `R` – Pork Chop Feb 13 '17 at 14:52
  • I know I can access it using model$r.squared etc. but unable to convert it into compatible shiny code. example, model <- reactive({ lm(cars[,names(cars) %in% input$d_var] ~ cars[,names(cars) %in% input$ind_var]) }) model_sumamry <- summary(model) output$summary <- renderPrint({model_sumamry$r.squared}) – Harshad Patil Feb 13 '17 at 14:54
  • 1
    you making the same mistake all the time, all the `reactive` variables are functions so they musht have `()` at the end, so its not `model` but `model()` – Pork Chop Feb 13 '17 at 15:08
  • Got it. Thanks a lot. – Harshad Patil Feb 13 '17 at 15:15