Error : invalid 'envir' argument of type 'character'

3,257 views
Skip to first unread message

Harshad Patil

unread,
Feb 18, 2017, 1:45:19 PM2/18/17
to Shiny - Web Framework for R
I am getting error "invalid 'envir' argument of type 'character'" while executing shiny R code. Please help as I am unable to figure out the workout.


  library(MASS)
  library
(shinythemes)
  library
(shiny)
  library
(ggplot2)
 
  mass
.tmp <- data(package = "MASS")[3]
  mass
.datasets <- as.vector(mass.tmp$results[,3])
 
  ui
<- fluidPage(
   
    theme
= shinytheme("superhero"),
    titlePanel
("Linear Regression Modelling"),
    sidebarLayout
(
      sidebarPanel
(
        selectInput
("dsname", "Dataset:",choices = c(mass.datasets)),
        uiOutput
("y_axis"),
        uiOutput
("x_axis")
     
)     ,
      mainPanel
(
        tags$br
(),
        tags$br
(),
       
"R-squared:",
        tags$span
(tags$b(textOutput("rsquared")),style="color:blue")
     
)
     
   
)
 
)
  server
<- function(input, output) {
   
    output$x_axis
<- renderUI({
      col_opts
<- get(input$dsname)
      selectInput
("x_axis2", "Independent Variable:", choices = c(names(col_opts)))
   
})
   
    cols2
<- reactive({
      col_opts2
<- get(input$dsname)
     
#names(col_opts2)[!grepl(input$x_axis2, names(col_opts2))]
   
})
   
    output$y_axis
<- renderUI({
      selectInput
("y_axis2", "Dependent Variable:", choices = c(names(cols2())))
   
})
   
    model
<- reactive({
     
#lm(input$dsname[,names(input$dsname) %in% input$y_axis2] ~ input$dsname[,names(input$dsname) %in% input$x_axis2])


      lm
( as.formula(paste(input$y_axis2," ~ ", input$x_axis2)) , data = input$dsname )
   
})
   
    model_summary
<- reactive({summary(model())})
    output$rsquared
<- renderText({  model_summary()$r.squared   })
 
 
}
 
  shinyApp
(ui = ui, server = server)

Bárbara Borges

unread,
Feb 19, 2017, 9:47:00 PM2/19/17
to Shiny - Web Framework for R
You're passing the data argument to lm as a string instead of a variable. I.e., on startup, for example, input$dsname is equal to "Aids2" (a character vector), not  Aids2 (a data.frame).

If you change  data = input$dsname to  data = eval(parse(text = input$dsname)) , it should work fine.

Harshad Patil

unread,
Feb 20, 2017, 7:05:26 AM2/20/17
to Shiny - Web Framework for R
Thanks. Will check and confirm.
Reply all
Reply to author
Forward
0 new messages