我正在尝试创建一个输入表单,将行添加到现有数据帧中。我使用了描述here的方法。
我遇到的问题是,我有两个日期输入类型,这两个类型通过这段代码转换为整数。
以下是一些示例数据:
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")我的代码是:
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()将日期转换为整数,有人知道我是如何工作的吗?任何帮助都是非常感谢的。如果我的问题不够清楚,请让我知道,我会尽我所能改进它。
谢谢!
发布于 2018-01-06 18:49:59
操作员帖子中的代码不起作用。因此,它被更改为使用DT包中的renderDataTable获取输出。此外,还更改了as.matrix和unlist,当存在混合类列时,这两个参数可能会更改列类型。例如
unlist(as.list(Sys.Date() + 0:5))
#[1] 17537 17538 17539 17540 17541 17542更正后的代码如下
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

数据
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]https://stackoverflow.com/questions/48125847
复制相似问题