【问题标题】:as.Date function not working n data tableas.Date 函数在数据表中不起作用
【发布时间】:2019-10-13 05:51:28
【问题描述】:

我正在使用一个数据输入屏幕,其中有三个 dateInput。我想以“月份,YYYY”格式显示月份。我使用了许多不同的格式,但无法达到预期的效果。 我需要以下格式的以下列: 1.月份:2019年1月 2. 截止日期:dd-mm-yyyy 3.实际日期:dd-mm-yyyy

如何做到这一点?

我在下面附上我的代码

fields <- c("Financial_Year","Quarter","Month", "Due_Date","Actual_Date")
library(shiny)
library(lubridate)
library(DT)

ui<-fluidPage(
  titlePanel("Monthly Civil Accounts"),
  sidebarLayout(
    sidebarPanel(width=3,
                 textInput("Financial_Year","Financial Year"),
                 selectInput("Quarter","Quarter",c("Q1","Q2","Q3","Q4")),
                 dateInput("Month","Month of Account", format="MM, yyyy"),
                 dateInput("Due_Date", "Due date for submission", format="dd-mm-yyyy"),
                 dateInput("Actual_Date","Actual date of submission",format="dd-mm-yyyy"),
                 actionButton("save","Add",icon=icon("plus-square")),
                 actionButton("reset","Delete",icon=icon("minus-square"))
    ),
    mainPanel(
      mainPanel(
        DT::dataTableOutput("responses1", width = 600), tags$hr()
      ))))

server<-function(input,output,session){
  saveData <- function(data) {
    data <- data.frame(
      Financial_Year=data["Financial_Year"],
      Quarter=data["Quarter"],
      Month=as.Date(as.numeric(data[["Month"]]),"1970-01-01"),
      Due_Date=as.Date(as.numeric(data[["Due_Date"]]),"1970-01-01"),
      Actual_Date=as.Date(as.numeric(data[["Actual_Date"]]),"1970-01-01")
    )
    if (exists("responses")) {
      responses <<- rbind(responses, data)
    } else {
      responses <<- data
    }
  }
  mcaloadData <- function() {
    if (exists("responses")) {
      responses
    }
  }
  formData <- reactive({
    data <- sapply(fields, function(x) input[[x]])
    data
    #print(data)
  })
  observeEvent(input$save, {
    saveData(formData())
  })

  output$responses1 <- DT::renderDataTable({
    input$save
    input$reset
    #mcadata<-transform(mcaloadData(), Delay=as.numeric((mcaloadData()$Actual_Date)-(mcaloadData()$Due_Date)))
    datatable(mcaloadData(),rownames=FALSE,options = list(ordering=FALSE, searching=FALSE,paging=FALSE,pageLength=FALSE,info=FALSE))
  })
}
shinyApp(ui,server)

请帮忙!!

【问题讨论】:

    标签: r date datatable shiny


    【解决方案1】:

    这对我有用。

    library(shiny)
    library(DT)
    
    ui <- fluidPage(
    
      titlePanel("DT")
    
      ,sidebarLayout(
        sidebarPanel(
    
          width = 2
    
          ,textInput(
            inputId = "Financial_Year"
            ,label = "Financial Year"
            )
    
          ,selectInput(
            inputId = "Quarter"
            ,label = "Quarter"
            ,c("Q1","Q2","Q3","Q4")
            )
    
          ,dateInput(
            inputId = "Month"
            ,label = "Month of Account"
            ,format = "MM, yyyy"
            )
    
          ,dateInput(
            inputId = "Due_Date"
            ,label = "Due date for submission"
            ,format = "dd-mm-yyyy"
            )
    
          ,dateInput(
            inputId = "Actual_Date"
            ,label = "Actual date of submission"
            ,format = "dd-mm-yyyy"
            )
    
          ,actionButton(
            inputId = "save"
            ,label = "Add"
            ,icon = icon("plus-square")
            )
    
          ,actionButton(
            inputId = "reset"
            ,label = "Delete"
            ,icon = icon("minus-square")
            )
        )
    
        ,mainPanel(
          DT::dataTableOutput("dt", width = 600)
        )
    
      )
    )
    
    server <- function(input, output, session) {
    
      observeEvent(input$save,{
    
        df <- data.frame(
          Fin_year = input$Financial_Year
          ,Quarter = input$Quarter
          ,Mth = format(input$Month, "%B, %Y")
          ,Due_Date = format(input$Due_Date, "%d-%m-%Y")
          ,Actual_Date = format(input$Actual_Date, "%d-%m-%Y")
        )
    
        if (exists("responses")) {
          responses <<- rbind(responses, df)
        } else {
          responses <<- df
        }
    
      })
    
      output$dt <- DT::renderDataTable({
    
        input$save
    
        if (exists("responses")) {
    
          DT::datatable(responses
            ,rownames = FALSE
            ,options = list(
              ordering = FALSE
              ,searching = FALSE
              ,paging = FALSE
              ,pageLength = FALSE
              ,info = FALSE
              )
            )
        }
    
      })
    
    }
    
    shinyApp(ui,server)
    
    

    【讨论】:

      【解决方案2】:

      一种方法是使用formatDatedatatable。我将 in-TG 包括在印度 Telangana 的语言环境中。我希望这可能会有所帮助。

      datatable(mcaloadData(),rownames=FALSE,options = list(ordering=FALSE, searching=FALSE,paging=FALSE,pageLength=FALSE,info=FALSE)) %>%
            formatDate(c('Actual_Date', 'Due_Date'), method = 'toLocaleDateString', params = list('in-TG')) %>% 
            formatDate('Month', method = 'toLocaleDateString', params = list('in-TG', list(year = 'numeric', month = 'long')))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-30
        • 2016-12-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多