如何读取和更新'selectInput'的值

时间:2017-08-29 21:33:04

标签: r shiny

我想上传一个列名为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 个答案:

答案 0 :(得分:1)

1)在data被动,你读取输入字段uploadedcsv,但在ui中,它被称为uploadcsv(注意缺少的ed)。如果你保持一致,上传应该有效。

2)应用程序启动时运行observe;此时data()会返回NULL,因此max(data()$Timemax(NULL),即-Inf。您应该等到数据加载完毕。一种方法是将observe更改为observeEvent

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

另一种选择是保留observe并在观察者的开头添加req(data)