首页 > 解决方案 > 显示警告:Shiny 中 :: NA/NaN 参数中的错误

问题描述

对于我闪亮的应用程序,我正在阅读 CSV 格式的每小时时间序列。我想找到我使用 R 包 extRemes 的年度最大值。这很有趣,称为 blockmaxxer 以查找输入数据和块为年的年度最大值。当我选择 CSV 文件时,我收到一个 NA/NaN 参数错误。我想找出问题出在哪里。

library(shiny)
library(zoo)
library(extRemes)

Annual_max <- function(vect_table,yr){

  L = c(1,2,3,6,12,24,48)
  n = length(L)
  
  xx <- vect_table
  m = length(xx)
  D = matrix(0,m,n)
  E = matrix(0,m,n)
  
  yeardata <- unique(yr)
  NY <- length(yeardata)
  AM = data.frame(matrix(0,NY,n))
  
  for (i in 1:n) {
    D[,i] = rollapply(C,L[i], sum, fill=0, align="right" )
    E[,i] = D[,i]/L[i]
  }
  
  ##### To find annual Maximum
  for (j in 1:n) {
    datA <- as.matrix(data.frame(year=yr, y = dur[,j]))
    AM[,j] = blockmaxxer.matrix(datA[,2],blocks=datA[,1])
  }
  return(AM)
}

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      fileInput("file", "Choose observation precipitation CSV File", accept = ".csv"),
      checkboxInput("header", "Header", TRUE),
      selectInput("HR1","Choose the column number of Hour 1", choices=c()),
      selectInput("HR24","Choose the column number of Hour 24", choices=c()),
      selectInput("Year", "Choose the Year column", choices=c())
    ),
    mainPanel(
      plotOutput('pl')
    )
  )
)
server <- function(input, output,session) {
  
  inputdata <- reactive({
    infile = input$file
    if(is.null(infile))
      return(NULL)
    df <- read.csv(infile$datapath,header=input$header)
    updateSelectInput(session,"HR1",choices= colnames(df)) 
    updateSelectInput(session,"HR24",choices= colnames(df)) 
    updateSelectInput(session,"Year",choices= colnames(df))
    return(df)
  })
  ### Converting the hourly data to a vector format
  vectdata <- reactive({
    req(inputdata())
    dat <- inputdata()
    A <- t(as.matrix(dat[,input$HR1:input$HR24]))
    B = as.numeric(as.vector(A))
    replace(B,is.na(B),0)
  })
  
  AMP <-  reactive({
    req(inputdata())
    req(vectdata())
    df <- Annual_max(vectdata(),inputdata()input$Year)
    return(df)
  })
  
  output$pl <- renderPlot({
    req(AMP())
    plot(AMP())
  })
  
}

shinyApp(ui, server)

控制台显示如下。

Warning in `[.data.frame`(dat, , input$HR1:input$HR24) :
NAs introduced by coercion
Warning in `[.data.frame`(dat, , input$HR1:input$HR24) :
NAs introduced by coercion
Warning: Error in :: NA/NaN argument
  217: [.data.frame
  213: <reactive:vectdata> [D:/Work/Shiny/s1.R#62]
  211: .func
  208: contextFunc
  207: env$runWith
  200: ctx$run
  199: self$.updateValue
  197: vectdata
  190: <reactive:AMP> [D:/Work/Shiny/s1.R#68]
  188: .func
  185: contextFunc
  184: env$runWith
  177: ctx$run
  176: self$.updateValue
  174: AMP
  167: renderPlot [D:/Work/Shiny/s1.R#74]
  165: func
  125: drawPlot
  111: <reactive:plotObj>
   95: drawReactive
   82: renderFunc
   81: output$pl
    1: runApp

我是闪亮的新手,所以任何帮助将不胜感激。谢谢你。

标签: rshiny

解决方案


我认为不适A <- t(as.matrix(dat[,input$HR1:input$HR24]))用于数据框。尝试使用selectfrom dplyr

library(dplyr)

A <- dat %>% select(all_of(input$HR1):all_of(input$HR24)) %>% t

推荐阅读