我该如何解决$中的错误:类型为'closure'的对象不可子集化

时间:2019-12-28 12:26:17

标签: r shiny

我写了这个Shiny应用程序,但遗憾的是使用表时出现了问题。我已经使用了几个小时,并且由于其他类似的问题,我得以发现问题出在:

 output$table <- renderTable({
if(is.null(data())){return ()}
data()
})

每次我尝试在图表中使用此表时,都会出现错误:

警告:$中的错误:“ closure”类型的对象不可子集化

问题出现在:

else if(identical(input$att,"Ranking 10 najlepszych producentow")){
      a<- as.data.frame(table(table$Publisher))
      a <- a[order(a$Freq),] 
      z<-nrow(a)
      x<-nrow(a)-9
      b<- a[x:z,]
      print(pie(b$Freq, b$Var1, col = rainbow(length(b$Freq)), main="Ranking 10 producentow, ktorzy wydali najwiecej gier")
      )
    }

else if(identical(input$att,"Sprzedaz gier w danym regionie")){
  Region <- c("NA", "EU", "JP", "OTHER", "GLOBAL")
  Sales <- c(sum(table$NA_Sales), sum(table$EU_Sales), sum(table$JP_Sales), sum(table$Other_Sales), sum(table$Global_Sales))
  newd<- data.frame(Region, Sales)
  print(ggplot(newd, aes(x=Region, y=Sales)) + 
          geom_bar(stat="identity", width=.5, fill='darkgoldenrod1') 
        + labs(title="Sprzedaz gier w zależnosci od regionu ")
        + theme(text = element_text(size=20))
  )
}

我看到很多人都遇到了这个问题,但是我找不到真正可以解决我问题的解决方案。据我了解,这是从csv读取的data()问题吗?它没有看到它,因此从其他地方获取数据?如果您能提供解决方案并解释到底出了什么问题,我将不胜感激。

library(shiny)
library(ggplot2)

#test<-read.csv("C:/Users/pawel/Desktop/AWD/PROJEKT/moj.csv")
#View(test)

ui <- fluidPage(
  titlePanel("Projekt zaliczeniowy AWD - Pawel Zurawski I7E1S1"), sidebarLayout(
    sidebarPanel(
      helpText("Aplikacja pokazujaca dane odnosnie ludzi, ktorzy ulegli powaznym wypadkom oraz informacje o ich rezultacie"),
      tags$hr(),
      fileInput("file"," Prosze podać scieżke do pliku CSV: "), 
      selectInput("att", "Prosze wybrac wykres", choices=c("Dane poczatkowe","Ranking 10 najlepszych producentow","Sprzedaz gier w danym regionie","Global Sales","NA Sales","EU Sales","Japan Sales","Other Sales"),
                  selected="Dane poczatkowe", multiple=FALSE,selectize=TRUE)
    ),
    mainPanel(
      uiOutput("tb"),
      plotOutput("line")             
    ))
)

#server.R
server <- function(input,output, session){

  data <- reactive({

    file1 <- input$file
    if(is.null(file1)){return()} 

    read.csv(file=file1$datapath, sep=",", header=TRUE)})

  output$filedf <- renderTable({
    if(is.null(data())){return ()}
    input$file
  }) 

  output$sum <- renderTable({
    if(is.null(data())){return ()}
    summary(data())
  })

  output$table <- renderTable({
    if(is.null(data())){return ()}
    data()
  })


  output$line <- renderPlot({
    if (is.null(data())) { return() }


    else if(identical(input$att,"Dane poczatkowe")){
      print(ggplot(data(), aes(Genre, fill = as.factor(Genre) ) ) 
            + geom_bar() 
            + theme(text = element_text(size=20),axis.text.x = element_text(angle=90, hjust=1)) 
            + labs(title = "Ilosc tytulow, na ktorych jest przeprowadzana analiza z kazdego z gatunkow", x="Gatunek", y="Ilosc tytulow")      
      )}


    else if(identical(input$att,"Ranking 10 najlepszych producentow")){
      a<- as.data.frame(table(table$Publisher))
      a <- a[order(a$Freq),] 
      z<-nrow(a)
      x<-nrow(a)-9
      b<- a[x:z,]
      print(pie(b$Freq, b$Var1, col = rainbow(length(b$Freq)), main="Ranking 10 producentow, ktorzy wydali najwiecej gier")
      )
    }


    else if(identical(input$att,"Sprzedaz gier w danym regionie")){
      Region <- c("NA", "EU", "JP", "OTHER", "GLOBAL")
      Sales <- c(sum(table$NA_Sales), sum(table$EU_Sales), sum(table$JP_Sales), sum(table$Other_Sales), sum(table$Global_Sales))
      newd<- data.frame(Region, Sales)
      print(ggplot(newd, aes(x=Region, y=Sales)) + 
              geom_bar(stat="identity", width=.5, fill='darkgoldenrod1') 
            + labs(title="Sprzedaz gier w zależnosci od regionu ")
            + theme(text = element_text(size=20))
      )
    }


    else if(identical(input$att,"Global Sales")){
      print(ggplot(data(), aes(Genre, Global_Sales)) 
            + geom_boxplot(colour="red") 
            + theme(text = element_text(size=15),axis.text.x = element_text(angle=90, hjust=1))  
            + facet_wrap(~Platform)
      )
    }



    else if(identical(input$att,"NA Sales")){
      print(ggplot(data(), aes(Genre, NA_Sales)) 
            + geom_boxplot(colour="red") 
            + theme(text = element_text(size=15),axis.text.x = element_text(angle=90, hjust=1))  
            + facet_wrap(~Platform)
      )
    }

    else if(identical(input$att,"EU Sales")){
      print(ggplot(data(), aes(Genre, EU_Sales)) 
            + geom_boxplot(colour="red") 
            +   scale_colour_continuous()
            + theme(text = element_text(size=15),axis.text.x = element_text(angle=90, hjust=1))  
            + facet_wrap(~Platform)
      )
    }

    else if(identical(input$att,"Japan Sales")){
      print(ggplot(data(), aes(Genre, JP_Sales)) 
            + geom_boxplot(colour="red") 
            + theme(text = element_text(size=15),axis.text.x = element_text(angle=90, hjust=1))  
            + facet_wrap(~Platform)
      )
    }

    else if(identical(input$att,"Other Sales")){
      print(ggplot(data(), aes(Genre, Other_Sales)) 
            + geom_boxplot(colour="red") 
            + theme(text = element_text(size=15),axis.text.x = element_text(angle=90, hjust=1))  
            + facet_wrap(~Platform)
      )
    }
  })

  output$tb <- renderUI({if(is.null(data())) h5()
    else
      tabsetPanel(tabPanel("Informacje o wczytanym pliku", tableOutput("filedf")),
                  tabPanel("Wczytane dane", tableOutput("table")),
                  tabPanel("Podsumowanie wczytanych danych", tableOutput("sum")))
  })
}

shinyApp(ui = ui, server = server)

0 个答案:

没有答案