r语言 - 如何读取和更新"selectInput"的值



我想上传一个列名为Time的数据集。然后,代码找到列Time的最大值,并将其除以4,以找出存在多少大小为4的间隔,然后在selectInput中打印出该序列。这是我的密码。它有效,但max(data()$Time)中有一个小错误,我不明白为什么它会给出-inf。错误显示no non-missing arguments to max; returning -Inf

这是代码:

library(shiny)
ui <- fluidPage(
fileInput(inputId = "uploadcsv","", accept = '.csv'),
selectInput("select", label = h3("Select box"), 
choices = "", 
selected = 1)
)
server <- function(input, output, session) {
data <- reactive({
infile <- input$uploadedcsv
if (is.null(infile))
return(NULL)
read.csv(infile$datapath, header = TRUE, sep = ",")
})
observe({
if (max(data()$Time) %% 4 == 0){
numberofinterval <- max(data()$Time) %/% 4
} else {
numberofinterval <- (max(data()$Time) %/% 4)+1
}
NumPeriod <- seq(0, numberofinterval)
updateSelectInput(session, inputId = "select",
choices = NumPeriod,
selected = NumPeriod)
})
}
shinyApp(ui = ui, server = server)

1(在datareactive中,您读取输入字段uploadedcsv,但在ui中,它被称为uploadcsv(注意缺少ed(。如果您使这一点一致,上传应该可以工作。

2(observe在应用程序启动时运行;此时data()返回NULL,所以max(data()$Timemax(NULL),也就是-Inf。您应该等待数据加载。一种方法是将observe更改为observeEvent:

observeEvent(data, { # and so on...

另一种选择是保留CCD_ 19并在观察者的开头添加CCD_。

最新更新