简体   繁体   English

闪亮的 inputDate 和 DT::dataTableOutput

[英]Shiny inputDate and DT::dataTableOutput

I am new to Shiny, which seem to be an amazing tool for an app I'm trying to build.我是 Shiny 的新手,对于我正在尝试构建的应用程序来说,它似乎是一个了不起的工具。

I am currently trying to understand why an "inputDate" field can't be read by the DT::dataTableOutput command... For example, when I enter 2021-01-01 in the interface, the table indicates the number "18628" in the "introduction_date" column.我目前正在尝试理解为什么 DT::dataTableOutput 命令无法读取“inputDate”字段......例如,当我在界面中输入 2021-01-01 时,表格显示数字“18628”在“介绍日期”列中。

Here is my code, any idea would be welcome !这是我的代码,欢迎任何想法!

Thanks by advance to the community, don't hesitate to telle me if you need more/better information to answer.提前感谢社区,如果您需要更多/更好的信息来回答,请不要犹豫告诉我。


    # 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()
        })
      }
    )

Following up from your comment and @denis's comment, replace your formData with:根据您的评论和formData的评论,将您的formData替换为:

 formData <- reactive({
      data <- sapply(fields, function(x) as.character(input[[x]]))
      data
    })

Restart your R session before running this.在运行之前重新启动 R 会话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM