使用Shiny构建绘图时出错

时间:2017-03-16 07:16:15

标签: r shiny

我对此很新,所以可能有一个简单的解释。我正在处理水质观测数据集。它开始像

Sample.ID,Sample.Date,Beach.Name,Sample.Location,Results,Units
050514CP13,5/5/2014,MIDLAND BEACH,Center,20,MPN/100 ml
062011GR04,6/20/2011,MANHATTAN BEACH,Left,0,MPN/100 ml
072808BH09,7/28/2008,MIDLAND BEACH,Right,28,MPN/100 ml
051214CP36,5/12/2014,SOUTH BEACH,Right,4,MPN/100 ml
081511KB07,8/15/2011,CEDAR GROVE,Left,360,MPN/100 ml
062909KB01,6/29/2009,MANHATTAN BEACH,Left,8,MPN/100 ml
082112KB07,8/21/2012,CEDAR GROVE,Left,20,MPN/100 ml

我有以下ui.R

library(shiny)

beaches <- read.csv("BeachWaterQuality.csv", header = TRUE)
beachNames <- levels(beaches$Beach.Name)
new.date <- strptime(beaches$Sample.Date, format="%m/%d/%Y")
minDate <- min(new.date)
maxDate <- max(new.date)


shinyUI(fluidPage(

  # Application title
  titlePanel("Beach Water Quality History"),

  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(
      selectInput("beach", 
                  label = "Choose a beach to display",
                  choices = beachNames),

      selectInput("sampleLocation",
                  label = "Choose a sample location",
                  choices = list("LEFT","CENTER", "RIGHT"),
                  selected="CENTER"),


      dateRangeInput("dates",
                     label= "Choose a date range to display",
                     start=minDate,
                     end=maxDate)
    ),

    # Show a plot of the selected data
    mainPanel(
       plotOutput("beachDataPlot")
    )
  )
))

这个server.R

library(shiny)  

shinyServer(function(input, output) {

  beaches <- read.csv("BeachWaterQuality.csv", header = TRUE)
  beaches$Results[is.na(beaches$Results)] <- 0
  beachNames <- levels(beaches$Beach.Name)
  new.date <- strptime(beaches$Sample.Date, format="%m/%d/%Y")
  beaches <- cbind(beaches,new.date)


  output$beachDataPlot <- renderPlot({

    plotData <- subset(beaches, Beach.Name==input$beach & Sample.Location==input$sampleLocation)


     plot(x=plotData$new.date,
         y=plotData$Results,
         xlab="Date",
         ylab="Bacterial Count",
         pch=16,
        main=paste("Bacterial Count for",input$beach,input$sampleLocation))
     lines(plotData$new.date,plotData$Results,col="red")
  })
})

当我尝试运行此操作时,我在应用程序窗口中显示错误

  

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

在控制台中

Warning in min(x) : no non-missing arguments to min; returning Inf  
Warning in max(x) : no non-missing arguments to max; returning -Inf  
Warning in min(x) : no non-missing arguments to min; returning Inf  
Warning in max(x) : no non-missing arguments to max; returning -Inf     
Warning: Error in plot.window: need finite 'xlim' values Stack trace (innermost first):
        103: plot.window
        102: localWindow
        101: plot.default
        100: plot
         99: renderPlot [C:\Users\jerem\YuckyBeach/server.R#30]
         89: <reactive:plotObj>
         78: plotObj
         77: origRenderFunc
         76: output$beachDataPlot
          1: runApp

有谁可以指出我在这里做错了什么?

2 个答案:

答案 0 :(得分:0)

如果我只是想快速猜测一下,我说它与&#34; Shiny&#34;无关。部分,而是与行

beaches <- cbind(beaches,new.date)

我认为使用&#34; cbind&#34;而不是&#34; data.frame&#34;对你的数据类做了奇怪的事情,导致函数图抛出错误...也许尝试用data.frame替换cbind,如果它仍然无法工作,请尝试:

print(beaches) 

在renderPlot块内,看看你的数据是什么样的......

答案 1 :(得分:0)

甚至比那更笨!在ui.R中,我将位置选项设置为LEFT,CENTER和RIGHT ...所有上限都是海滩名称的存储方式。但是在数据中,位置是Left,Center和Right ...所以subset()返回一个空的数据帧......