需要有限的'xlim'值 - 闪亮

时间:2016-07-24 21:49:13

标签: r shiny

之前已经提出过这个问题,但到目前为止,这些解决方案都没有对我有用。

我为一门课程创建了一个Shiny应用程序,到目前为止,我已经为应用程序添加了5个视觉效果。添加完最后一个(bubbleChart)后,我开始收到此错误:

  

'错误:需要有限'xlim'值'

我试图在所有输入变量上添加!is.null()逻辑无济于事。我检查了所有的打字,以确保没有任何拼写错误,我没有找到任何打字机。

情节在Shiny之外运作得很好。我在尝试运行应用时只收到此错误。我通过函数传递了我认为完全相同的变量。请记住,我其他5次这样做,只有这一个导致问题。我的所有其他图都使用ggplot2包。

这是代码,如果需要更多,我可以添加它,但我不想包含整个应用程序,因为有很多。

UI

tabPanel('Panel 3', 
         titlePanel('Waterfowl Migration Analysis'), 
         sidebarLayout(
             sidebarPanel(
                 uiOutput('scatterSpecies'),
                 sliderInput('year_range3',
                             'Year Range',
                             min = 1955,
                             max = 2014,
                             value = c(1955,2014),
                             sep="")
                 ), 
             ## Main panel for Panel 3
             mainPanel(
                 tabsetPanel(
                     tabPanel('Scatter Plot',plotOutput('scatterPlot')),
                     tabPanel('Bubble Chart',plotOutput('bubbleChart'))
                 )) 

服务器代码

UI输入渲染

output$scatterSpecies <- renderUI({

migratingList <- unique(tolower(migrating$Species))

breedingList <- unique(tolower(breeding$Species))

species <- breedingList[breedingList %in% migratingList]

selectizeInput('scatterSpecies', 
               'Select Multiple Species', 
               choices = species,
               selected = species[1],
               multiple = TRUE
               ) })

UI输出渲染

output$bubbleChart <- renderPlot({

bubbleChart(breeding,migrating,temps,input$scatterSpecies,input$year_range3) })

bubbleChart功能

bubbleChart <- function(dfb,dfm,dft,species,years){

  #dfb = breeding
  #dft = temps
  #dfm = migrating

  # #Rename columns for differentiation
  # colnames(dfb)[colnames(dfb)=='Population'] <- 'Breeding Population'
  # colnames(dft)[colnames(dft)=='Population'] <- 'Migrating Population'

  #Filter to year range
  dfb <- dfb[dfb$Year >= years[1] & dfb$Year <= years[2],]
  dfm <- dfm[dfm$Year >= years[1] & dfm$Year <= years[2],]

  dft <- dft[dft$Year >= years[1] & dft$Year <= years[2],]

  #Filter to species list
  dfb <- dfb[dfb$Species %in% species,]
  dfm <- dfm[dfm$Species %in% species,]

  #Convert abbreviated states to full name in lower case
  dfm$State <- tolower(state.name[match(dfm$State,state.abb)])

  ###### Create single data frame ######

  #Aggregate data by year and state
  dfb <- melt(tapply(dfb$Population,dfb$Year,sum,na.rm=TRUE))
  dfm <- melt(tapply(dfm$Population,dfm[,c('Year','State')],sum,na.rm=TRUE))

  #Convert state to factors for joining
  dfm$State <- as.factor(dfm$State)
  dft$State <- as.factor(dft$State)

  #Left join the temp data
  df <- left_join(dfm,dft,by=c('Year','State'))

  #Leave only complete data
  df <- df[complete.cases(df),]
  dfb <- dfb[complete.cases(dfb),]

  #Clean new column names
  colnames(df)[colnames(df)=='value.x'] <- 'Migrating'
  colnames(df)[colnames(df)=='value.y'] <- 'Temp'
  colnames(dfb)[colnames(dfb)=='value'] <- 'Breeding'
  colnames(dfb)[colnames(dfb)=='Var1'] <- 'Year'

  #Average temp values and sum population in temp df (ndf)
  years <- unique(df$Year)

  ndf <- data.frame(Year = years,
                    Migrating = rep(0,times = length(years)),
                    Temp = rep(0,times = length(years)))

  for(i in ndf$Year){

    ndf[ndf$Year == i,'Migrating'] <- sum(df[df$Year == i,'Migrating'], na.rm = TRUE)
    ndf[ndf$Year == i,'Temp'] <- mean(df[df$Year == i,'Temp'], na.rm = TRUE)

  }

  df <- ndf

  #Final joining of datasets
  df <- inner_join(df, dfb, by='Year')

  ###### Plotting ######

  #Create function to scale the temp data to see the changes in temperature
  range <- function(x){(x-min(x))/(max(x)-min(x))}

  radius <- sqrt( range(df$Temp)/ pi )

  symbols(df$Breeding,df$Migrating, circles=radius, inches = .5,
          fg='white',bg='red',xlab = 'Breeding Population',ylab='Migrating Population', 
          main = 'Breeding and Migrating Pops by Avg Temperature (size)')

  text(df$Breeding, df$Migrating, df$Year, cex=0.5)


}

1 个答案:

答案 0 :(得分:1)

问题是我的过滤器值与数据框的格式不匹配。一旦我转换了使用tolower()过滤的列,就解决了问题。