闪亮的输入日期和 DT::dataTableOutput
Shiny inputDate and DT::dataTableOutput
我是 Shiny 的新手,对于我正在尝试构建的应用程序来说,它似乎是一个了不起的工具。
我目前正在尝试理解为什么 DT::dataTableOutput 命令无法读取“inputDate”字段...例如,当我在界面中输入 2021-01-01 时,table表示“introduction_date”栏中的数字“18628”。
这是我的代码,欢迎任何想法!
提前感谢社区,如果您需要 more/better 信息来回答,请随时告诉我。
# Libraries to open
library(shiny)
library(DT)
# Save function
saveData <- function(data) {
data <- as.data.frame(t(data))
if (exists("responses")) {
responses <<- rbind(responses, data)
} else {
responses <<- data
}
}
loadData <- function() {
if (exists("responses")) {
responses
}
}
# Define the fields we want to save from the form
fields <- c("introduction_date","fish_species","cohort_id","cohort_size","initial_weight","cage_id","harvest_weight","allow_transfer") #,"transfer_density","cage_destination")
fish.names <- c("Dicentrarchus.labrax","Sparus.aurata","Argyrosomus.regius","Symphodus.ocellatus","Salmo.salar","Salmo.trutta","Oncorhynchus.mykiss")
# Shiny app with 3 fields that the user can submit data for
shinyApp(
ui <- fluidPage(
titlePanel("Project"),
sidebarLayout(
sidebarPanel(
dateInput("introduction_date", "Date:", value = "2021-01-01", format = "yyyy-mm-dd"),
selectInput("fish_species", "fish species",
fish.names),
numericInput("cohort_id", "cohort id", 10, min = 1, max = 100),
numericInput("cohort_size", "cohort size", 20000, min = 1, max = 100000),
sliderInput("initial_weight", "initial weight:",
0, 100, 15, ticks = FALSE),
numericInput("cage_id", "cage id", 10, min = 1, max = 100),
sliderInput("harvest_weight", "harvest weight",
200, 5000, 300, ticks = FALSE),
checkboxInput("allow_transfer", "allow transfer", FALSE),
actionButton("submit", "submit"),
actionButton("save_planning", "save planning", class = "btn-success"),
),
mainPanel(
tabsetPanel(
tabPanel("location", plotOutput("location"),
),
tabPanel("Planning", verbatimTextOutput("planning"),
DT::dataTableOutput("responses", width = 300), tags$hr(),
),
tabPanel("Table", tableOutput("table"))
)
)
)
),
server = function(input, output, session) {
# Whenever a field is filled, aggregate all form data
formData <- reactive({
data <- sapply(fields, function(x) input[[x]])
data
})
# When the Submit button is clicked, save the form data
observeEvent(input$submit, {
saveData(formData())
})
# Show the previous responses
# (update with current response when Submit is clicked)
output$responses <- DT::renderDataTable({
input$submit
loadData()
})
# When the Submit button is clicked, save the form data
observeEvent(input$save_plan, {
stopApp()
})
}
)
根据你的评论和@denis 的评论,将你的 formData
替换为:
formData <- reactive({
data <- sapply(fields, function(x) as.character(input[[x]]))
data
})
在此之前 运行 重新启动您的 R 会话。
我是 Shiny 的新手,对于我正在尝试构建的应用程序来说,它似乎是一个了不起的工具。
我目前正在尝试理解为什么 DT::dataTableOutput 命令无法读取“inputDate”字段...例如,当我在界面中输入 2021-01-01 时,table表示“introduction_date”栏中的数字“18628”。
这是我的代码,欢迎任何想法!
提前感谢社区,如果您需要 more/better 信息来回答,请随时告诉我。
# Libraries to open
library(shiny)
library(DT)
# Save function
saveData <- function(data) {
data <- as.data.frame(t(data))
if (exists("responses")) {
responses <<- rbind(responses, data)
} else {
responses <<- data
}
}
loadData <- function() {
if (exists("responses")) {
responses
}
}
# Define the fields we want to save from the form
fields <- c("introduction_date","fish_species","cohort_id","cohort_size","initial_weight","cage_id","harvest_weight","allow_transfer") #,"transfer_density","cage_destination")
fish.names <- c("Dicentrarchus.labrax","Sparus.aurata","Argyrosomus.regius","Symphodus.ocellatus","Salmo.salar","Salmo.trutta","Oncorhynchus.mykiss")
# Shiny app with 3 fields that the user can submit data for
shinyApp(
ui <- fluidPage(
titlePanel("Project"),
sidebarLayout(
sidebarPanel(
dateInput("introduction_date", "Date:", value = "2021-01-01", format = "yyyy-mm-dd"),
selectInput("fish_species", "fish species",
fish.names),
numericInput("cohort_id", "cohort id", 10, min = 1, max = 100),
numericInput("cohort_size", "cohort size", 20000, min = 1, max = 100000),
sliderInput("initial_weight", "initial weight:",
0, 100, 15, ticks = FALSE),
numericInput("cage_id", "cage id", 10, min = 1, max = 100),
sliderInput("harvest_weight", "harvest weight",
200, 5000, 300, ticks = FALSE),
checkboxInput("allow_transfer", "allow transfer", FALSE),
actionButton("submit", "submit"),
actionButton("save_planning", "save planning", class = "btn-success"),
),
mainPanel(
tabsetPanel(
tabPanel("location", plotOutput("location"),
),
tabPanel("Planning", verbatimTextOutput("planning"),
DT::dataTableOutput("responses", width = 300), tags$hr(),
),
tabPanel("Table", tableOutput("table"))
)
)
)
),
server = function(input, output, session) {
# Whenever a field is filled, aggregate all form data
formData <- reactive({
data <- sapply(fields, function(x) input[[x]])
data
})
# When the Submit button is clicked, save the form data
observeEvent(input$submit, {
saveData(formData())
})
# Show the previous responses
# (update with current response when Submit is clicked)
output$responses <- DT::renderDataTable({
input$submit
loadData()
})
# When the Submit button is clicked, save the form data
observeEvent(input$save_plan, {
stopApp()
})
}
)
根据你的评论和@denis 的评论,将你的 formData
替换为:
formData <- reactive({
data <- sapply(fields, function(x) as.character(input[[x]]))
data
})
在此之前 运行 重新启动您的 R 会话。