首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R Shiny isolate()以整数形式返回日期

R Shiny isolate()以整数形式返回日期
EN

Stack Overflow用户
提问于 2018-01-06 17:09:25
回答 1查看 469关注 0票数 1

我正在尝试创建一个输入表单,将行添加到现有数据帧中。我使用了描述here的方法。

我遇到的问题是,我有两个日期输入类型,这两个类型通过这段代码转换为整数。

以下是一些示例数据:

代码语言:javascript
复制
x_df <- data.frame(title = character(1), start = character(1), end = character(1))
x$title <- "Test1"
x$start <- as.Date("2017-12-16")
x$end <- as.Date("2017-12-17")

我的代码是:

代码语言:javascript
复制
library(shiny)

runApp(
list(
ui = fluidPage(

  sidebarLayout(

    sidebarPanel(

      selectInput("Employee", "Choose an employee:",
                  choices_employee),

      dateInput("Date_from", "From:", format = "yyyy/mm/dd"),

      dateInput("Datum_until", "Until:", format = "yyyy/mm/dd"),

      actionButton("Add", "Add")

    )
  )
),

server = function(input, output) {

  values <- reactiveValues()
  values$df <- x_df
  addData <- observe({

    # your action button condition
    if(input$Add > 0) {
      # create the new line to be added from your inputs
      newLine <- isolate(c(input$Employee, input$Date_from, input$Date_until))
      # update your data
      # note the unlist of newLine, this prevents a bothersome warning message that the rbind will return regarding rownames because of using isolate.
      values$df <- isolate(rbind(as.matrix(values$df), unlist(newLine)))
      x_df <<- as.data.frame(values$df)
    }
  })

}
)
)

我认为是isolate()将日期转换为整数,有人知道我是如何工作的吗?任何帮助都是非常感谢的。如果我的问题不够清楚,请让我知道,我会尽我所能改进它。

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2018-01-06 18:49:59

操作员帖子中的代码不起作用。因此,它被更改为使用DT包中的renderDataTable获取输出。此外,还更改了as.matrixunlist,当存在混合类列时,这两个参数可能会更改列类型。例如

代码语言:javascript
复制
unlist(as.list(Sys.Date() + 0:5))
#[1] 17537 17538 17539 17540 17541 17542

更正后的代码如下

代码语言:javascript
复制
library(DT)
library(shiny)

runApp(
  list(
    ui = fluidPage(
     
      
      sidebarLayout(
        
        sidebarPanel(
          
          selectInput("Employee", "Choose an employee:",
                      choices_employee),
          
          dateInput("Date_from", "From:", format = "yyyy/mm/dd"),
          
          dateInput("Date_until", "Until:", format = "yyyy/mm/dd"),
          
          actionButton("Add", "Add")
          
        ),
        mainPanel(
          dataTableOutput("table")
        )
      )
      
    
    ),
    
    server = function(input, output) {
      
      values <- reactiveValues()
      values$df <- x_df
      addData <- observe({
        
       
        if(input$Add > 0) {
          # create the new line to be added from your inputs
          newLine <- isolate(data.frame(title = input$Employee, start = as.Date(input$Date_from),
                                          end = as.Date(input$Date_until), stringsAsFactors= FALSE))
          
          values$df <- isolate(rbind(values$df, newLine))
         
          
        }
      })
      
      output$table <- renderDataTable({values$df })
      
    }
  )
)

-output

数据

代码语言:javascript
复制
x_df <- data.frame(title = character(1), start = character(1), end = character(1))
x_df$title <- "Test1"
x_df$start <- as.Date("2017-12-16")
x_df$end <- as.Date("2017-12-17")
choices_employee = LETTERS[1:5]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48125847

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档