【问题标题】:error in sequence selection and plotting - R Shiny序列选择和绘图中的错误 - R Shiny
【发布时间】:2016-12-20 21:13:46
【问题描述】:

我的数据包括日期、温度和记录测量的海拔高度。我正在尝试使用 R Shiny 创建一个简单的反应图,其中该图会根据人们感兴趣的海拔界限而变化。一旦定义了界限,它将每天取这些界限的平均值并绘制它们。这是一个示例数据集(很抱歉生成它需要多长时间):

day <- seq.Date(as.Date("2016-01-01"),as.Date("2016-12-31"),by='day')
temperature_c <- runif(366,0,35)
sonde.elev <- 215  
df.1 <- data.frame(day,sonde.elev,temperature_c)
sonde.elev <- 220
df.2 <- data.frame(day,sonde.elev,temperature_c)
sonde.elev <- 225
df.3 <- data.frame(day,sonde.elev,temperature_c)
sonde.elev <- 230
df.4 <- data.frame(day,sonde.elev,temperature_c)
sonde.elev <- 235
df.5 <- data.frame(day,sonde.elev,temperature_c)
sonde.elev <- 240
df.6 <- data.frame(day,sonde.elev,temperature_c)
sonde.elev <- 245
df.7 <- data.frame(day,sonde.elev,temperature_c)
sonde.elev <- 250
df.8 <- data.frame(day,sonde.elev,temperature_c)
sonde.elev <- 255
df.9 <- data.frame(day,sonde.elev,temperature_c)
sonde.elev <- 260
df.10 <- data.frame(day,sonde.elev,temperature_c)
sonde.elev <- 265
df.11 <- data.frame(day,sonde.elev,temperature_c)
sonde.elev <- 270
df.12 <- data.frame(day,sonde.elev,temperature_c)
sonde.elev <- 275
df.13 <- data.frame(day,sonde.elev,temperature_c)
sonde.elev <- 280
df.14 <- data.frame(day,sonde.elev,temperature_c)
sonde.elev <- 285
df.15 <- data.frame(day,sonde.elev,temperature_c)
sonde.elev <- 290
df.16 <- data.frame(day,sonde.elev,temperature_c)
campbell_out <- rbind(df.1,df.2,df.3,df.4,df.5,df.6,df.7,df.8,df.9,df.10,df.11,df.12,df.13,df.14,df.15,df.16)
campbell_out <- campbell_out[order(campbell_out$day),] 

我已经将我的情节设置为有一个页面愿望侧边栏和一个包含情节的主面板。

这是我的 ui.R:

#===================
# ui.R
#===================

library(shiny)

shinyUI(pageWithSidebar(
  headerPanel("Stack Overflow Lough"),
sidebarPanel(
  dateRangeInput(inputId = "dateRange",
                 label = "Date Range",
                 start = "2016-01-01",
                 max = Sys.Date()
                 ),
  sliderInput(inputId = "minimumElevation",
              label="Feet (ASL) - Minimum",
              min = 215,
              max = 290,
              value = 215,
              step = 1),
  sliderInput(inputId = "maxmimumElevation",
              label="Feet (ASL) - Maximum",
              min = 215,
              max = 290,
              value = 290,
              step = 1)  
  ),
mainPanel(plotOutput("theGraph"))
 )
)

这是我的服务器。R:

#===================
# server.R
#===================
library(plyr)
    shinyServer(function(input,output){
      passData <- reactive({
        campbell_out <- campbell_out[campbell_out$day %in%
                                       seq.Date(input$dateRange[1],
                                                input$dateRange[2], by = "days"),]
        campbell_out <- campbell_out[campbell_out$sonde.elev %in%
                                       seq(as.numeric(input$minimumElevation),
                                       as.numeric(input$maximumElevation), by=0.000001),]
        campbell_out
      })

      output$theGraph <- renderPlot({
        graphData <- ddply(passData(),.(day),numcolwise(mean))
        theGraph <- plot(temperature_c~day,data=graphData, ylab = "Temperature (c)", xlab = "Time")
        print(theGraph)
      })
    })

当我在 R Shiny 中运行它并选择我感兴趣的日期时,我收到以下错误:

Warning: Error in seq.default: 'to' must be of length 1

当我运行一个类似的脚本来根据特定日期和海拔对数据集进行采样,然后绘制它时,它工作正常。所以问题似乎与我的闪亮代码有关。如果有人能告诉我哪里出错了,我将不胜感激。

【问题讨论】:

    标签: r plot shiny seq


    【解决方案1】:

    尝试将这些行添加到您的 passData reactive(用于处理响应式初始化警告):

      passData <- reactive({
        req(input$dateRange[1])
        req(input$dateRange[2])
        req(input$minimumElevation)
        req(input$maximumElevation)
        campbell_out <- campbell_out[campbell_out$day %in%
                                       seq.Date(input$dateRange[1],
                                                input$dateRange[2],by = "days"),]
        print(as.numeric(input$minimumElevation))
        print(as.numeric(input$maximumElevation))
        campbell_out <- campbell_out[campbell_out$sonde.elev %in%
                                       seq(as.numeric(input$minimumElevation),
                                           as.numeric(input$maximumElevation),by = 0.000001),]
        campbell_out
      })
    

    并在您的 shinyUI 函数中更正 inputId = "maxmimumElevation" 的拼写错误。

    那么对我来说很好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-01
      • 2018-12-20
      • 2021-04-13
      • 2014-10-27
      • 2019-06-16
      • 1970-01-01
      • 2014-03-27
      相关资源
      最近更新 更多