2017-08-29 55 views
0

我想上传一个列名为Time的数据集。然后,代码找到Time列的最大值,并将其除以4以找出有多少个大小为4的区间,然后在selectInput中打印出该序列。这是我的代码。它的作品,但在max(data()$Time)有一个小错误,我不明白为什么它给-inf。错误说no non-missing arguments to max; returning -Inf如何读取和更新'selectInput'的值

下面是代码:

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) 
+0

你可以尝试格式化你的变量作为日期'as.Date()',但除非你分享你的CSV,它或多或少的随机猜测,... – BigDataScientist

回答

0

1)在data反应,你读的输入字段uploadedcsv,但在用户界面,这就是所谓的uploadcsv(注意失踪ed)。如果你使这个一致,上传应该工作。

2)observe在应用程序启动时运行;那时data()返回NULL,所以max(data()$Timemax(NULL),这是-Inf。您应该等到数据加载完毕。这样做的一个办法是改变observeobserveEvent

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

另一种选择,就是保持observe和观察者的开头添加req(data)