0

Shiny newbie here.

I am trying to write a R shiny script, and one of things I want to do is generate various plots. I have a written this code for plotting by taking input from user but getting error of

Error in exists(name, envir = env, mode = mode) : 
  argument "env" is missing, with no default

Need Help to solve this I am uploading my server and ui code.

 Server.r      


 shinyServer(function(input,output){
      data<-reactive({
        file1<-input$file
        if(is.null(file1)){return()}
        read.table(file=file1$datapath,sep = input$sep,header = input$header,stringsAsFactors = input$stringAsFactors)
      })
        output$variable <- renderUI({ 
            obj<-data()   
            if (is.null(obj)) 
              return(NULL)
            var.opts<-namel(colnames(obj))
            selectInput("variable","Variable:", var.opts)                
          })
          # y variable
          output$group <- renderUI({ 
            obj<-data()   
            if (is.null(obj)) 
              return(NULL)
            var.opts<-namel(colnames(obj))
            selectInput("group","Groups:", var.opts)                 
          })
          #caption
          output$caption<-renderText({
            switch(input$plot.type,
                   "boxplot"   =    "Boxplot",
                   "histogram" =    "Histogram",
                   "density"    =   "Density plot",
                   "bar"        =   "Bar graph")
          })
          #plot
          output$plot <- renderUI({
            plotOutput("p")
          })
          #plotting function using ggplot2
          output$p <- renderPlot({
            obj<-data()
            plot.type<-switch(input$plot.type,
                              "boxplot"   =     geom_boxplot(),
                              "histogram" = geom_histogram(alpha=0.5,position="identity"),
                              "density"     =   geom_density(alpha=.75),
                              "bar"         =   geom_bar(position="dodge")
            )

            require(ggplot2)
            #plotting theme
            .theme<- theme(
              axis.line = element_line(colour = 'gray', size = .75), 
              panel.background = element_blank(),  
              plot.background = element_blank()
            )   


            if(input$plot.type=="boxplot")  {       #control for 1D or 2D graphs 
              p<-ggplot(data=obj, 
                        aes(
                          x         = obj$group, 
                          y         = obj$variable,
                          fill  = as.factor(obj$group)
                        )
              ) + plot.type

              if(input$show.points==TRUE)
              { 
                p<-p+ geom_point(color='black',alpha=0.5, position = 'jitter')
              }

            } else {

              p<-ggplot(data=obj, 
                        aes(
                          x         = obj$variable,
                          fill  = as.factor(obj$group),
                          group     = as.factor(obj$group)
                          #color    = as.factor(plot.obj$group)
                        )
              ) + plot.type
            }

            p<-p+labs(
              fill  = input$group,
              x         = "",
              y         = input$variable
            )  +
              .theme
            print(p)
          }) 
    })

ui.R

shinyUI(fluidPage(
  #Heading panel
  titlePanel(title="Machine Learning and Statistics",),
  #input data set
  sidebarLayout(position = "right",
                sidebarPanel(fileInput('file', 'Choose a File', multiple = T, accept=c('text/csv', 
                                                                                       'text/comma-separated-values,text/plain', 
                                                                                       '.csv')),
                             #default size for dataset
                             helpText("Default max. size is 7mb"),
                             #input number of observations
                             numericInput("obs", "Number of observations to view:", 10),

                             tags$hr(),
                             checkboxInput(inputId = 'header',label = 'Header',value = TRUE),
                             checkboxInput(inputId = "stringAsFactors","stringAsFactors",TRUE),
                             br(),
                             radioButtons(inputId = 'sep',label = 'Seprator',choices=c(comma=',',Semicolon=';',Tab='\t',Space=' '),selected = ','),
                             sliderInput("train_percent",
                                         "Training Percentage:",
                                         min = 10, max = 90, 
                                         value = 20, step = 10),
                             uiOutput("variable"),   # depends on dataset ( set by output$variable in  server.R)
                             uiOutput("group"),     # depends on dataset    ( set by output$group in  server.R)
                             selectInput("plot.type","Plot Type:", 
                                         list(boxplot = "boxplot", histogram = "histogram", density = "density", bar = "bar")
                             ),
                             checkboxInput("show.points", "show points", TRUE)


                ),
                mainPanel(
                  ("output"),
                  h3(textOutput("caption")),
                  uiOutput("plot")
)
)))

Help?Thanks.

2
  • Could you have an empty data frame? Commented Aug 19, 2015 at 14:01
  • Not sure whether this is causing it, but inside your aes calls, I think you may need to use the variable names itself instead of dollar notation. Commented Aug 19, 2015 at 17:10

1 Answer 1

1

My solution to this error message when using ggplot in Shiny is:

ggplot(data = obj, aes(...), environment = environment())

Would appreciate if someone could explain the reason behind the extra need for this in a Shiny app.

Sign up to request clarification or add additional context in comments.

1 Comment

The variables insides aes(...) is evaluated at the global scope, when calling a ggplot within a function, the environment has to be redefined with environment = environment(), see: github.com/hadley/ggplot2/issues/743

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.