2017-03-07 8 views
0

データを表示しようとしているデータフレームに何が問題なのか説明できますか?エラーを投げている行にはコメントがついています。入力データフレームエラーのためにバープロットが表示されない

library(dplyr) 
library(shiny) 
dlxl<-function(url,sht){ 
    tmp = tempfile(fileext = ".xlsx") 
    download.file(url = url, destfile = tmp, mode="wb") 
    library(readxl) 
    read_excel(tmp,sheet=sht) 
} 
csv<-dlxl("https://www.bls.gov/cps/cpsa2016.xlsx",sht="cpsaat14") 
colnames(csv)<-csv[4,] 
csv<-csv[6:81,] 
colnames(csv)<-make.names(names(as.data.frame(csv))) 

ui = bootstrapPage(
    titlePanel("Occupation by Race and Age"), 

    sidebarLayout(
    sidebarPanel(
     checkboxGroupInput('age',"Choose Age/Racial Group(s)", choices = unique(as.data.frame(csv)[,1]),selected=NULL), 
     checkboxGroupInput('industry',"Choose Industries", choices=colnames(csv),selected=NULL) 
    ), 
    mainPanel(
     plotOutput("plot1"), 
     htmlOutput("text1") 
    ) 
) 
) 
server = function(input, output, session){ 

    output$text1 <- renderUI({HTML(paste("GO"))}) 

    getData<-reactive({ 
    shiny::validate(need(length(input$age)>1, "Please select an age")) 
    shiny::validate(need(length(input$industry)>1, "Please select an industry")) 
    #ages are x axis, y are industry values 
    data <-csv[,grepl(input$industry, colnames(csv))] 
    data<-cbind(csv[,1],data) 
    data2<-data[grepl(input$age, data[,1]),] 
    as.data.frame(data2) 
}) 

    output$plot1 <- renderPlot({ 
    data<-getData() 
    shiny::validate(need(nrow(data)>1, "The data for the source you selected was not reported")) 
    barplot(data[,2:ncol(data)]) #########ERROR HERE 
    #browser() 
    #plot(data,names.arg=colnames(data),legend.text = T,beside=T,col =palette()[1:nrow(data)]) 
    }) 
} 

shinyApp(ui, server) 

答えて

0

カップルのエラー:>0

変更>1選択された一つの引数をチェックする(2を必要としない)

shiny::validate(need(length(input$age)>0, "Please select an age")) 
shiny::validate(need(length(input$industry)>0, "Please select an industry")) 

read_excelを文字列としてすべての数字を読んでいるようだ、あなたはプロットする前にas.numericを数字に変換する必要があります。

barplot(as.numeric(data[,2:ncol(data)])) 
関連する問題