R:ggplot中的错误:缺少参数“env”,没有默认值

时间:2015-08-19 11:18:38

标签: r ggplot2 shiny

这里有光泽的新手。

我正在尝试编写一个R闪亮的脚本,我想要做的一件事就是生成各种图。 我通过从用户那里获取输入但是得到

的错误来编写用于绘图的代码
Error in exists(name, envir = env, mode = mode) : 
  argument "env" is missing, with no default

需要帮助来解决这个问题    我正在上传我的服务器和ui代码。

 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")
)
)))

帮助?谢谢。

1 个答案:

答案 0 :(得分:1)

在Shiny中使用ggplot时,我对此错误消息的解决方案是:

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

如果有人能够在一个闪亮的应用程序中解释这个额外需要背后的原因,我将不胜感激。