【问题标题】:barplot not displaying due to input dataframe errors由于输入数据框错误,条形图未显示
【发布时间】:2017-03-07 00:35:45
【问题描述】:

有人可以解释我试图显示数据的数据框有什么问题吗?引发错误的行标有注释。

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)

【问题讨论】:

    标签: r dataframe shiny bar-chart


    【解决方案1】:

    几个错误:

    &gt;1 更改为 &gt;0 以检查是否选择了一个参数(不需要两个)

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

    【讨论】:

      猜你喜欢
      • 2022-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-11
      相关资源
      最近更新 更多