【问题标题】:how to read and update the values of 'selectInput'如何读取和更新“selectInput”的值
【发布时间】:2017-08-30 10:24:18
【问题描述】:

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

【问题讨论】:

  • 您可以尝试将变量格式化为日期as.Date(),但除非您共享 csv,否则它或多或少是随机猜测...

标签: r shiny


【解决方案1】:

1) 在data 响应式中,您读取输入字段uploadedcsv,但在用户界面中,它被称为uploadcsv(注意缺少ed)。如果您保持一致,则上传应该可以工作。

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

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

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

【讨论】:

    猜你喜欢
    • 2020-05-05
    • 2018-07-09
    • 2022-01-15
    • 2021-11-03
    • 2016-06-21
    • 2023-01-16
    • 2013-10-01
    • 1970-01-01
    • 2019-02-11
    相关资源
    最近更新 更多