【问题标题】:need finite 'xlim' values - Shiny需要有限的“xlim”值 - 闪亮
【发布时间】:2016-11-28 04:06:20
【问题描述】:

以前有人问过这个问题,但是到目前为止,没有一个解决方案对我有用。

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

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

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

剧情在 Shiny 之外运行良好。我仅在尝试运行应用程序时收到此错误。我已经通过函数传递了我认为完全相同的变量。请记住,我另外做了 5 次,只有这一次会导致问题。我所有的其他地块都使用ggplot2 包。

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

用户界面

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


}

【问题讨论】:

  • 您很可能将无效数据传递给气泡图函数。如果函数调用的参数中没有数据,则可以解释错误。
  • 谢谢,这帮助我找到了现在令人尴尬的问题。我正在使用 tolower() 转换过滤器值,但忘记在此视觉对象的数据框中执行此操作。

标签: r shiny


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多