对于我闪亮的应用程序,我正在阅读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
我是新来的,所以任何帮助都会很感激。谢谢你。
我不认为A <- t(as.matrix(dat[,input$HR1:input$HR24]))
会在数据框架上工作。试着从dplyr
中使用select
library(dplyr)
A <- dat %>% select(all_of(input$HR1):all_of(input$HR24)) %>% t