Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用R比较条件面板中的日期_Javascript_R_Shiny - Fatal编程技术网

Javascript 使用R比较条件面板中的日期

Javascript 使用R比较条件面板中的日期,javascript,r,shiny,Javascript,R,Shiny,我正在ui.R文件中使用conditionalPanel。我想比较selectinput中的给定日期是在某个日期(2019年9月30日)之前还是之后 我的selectInput如下所示: selectInput(inputId = 'date', label = 'Stichtag:', choices = sub("([0-9]{4}).([0-9]{2}).([0-9]{2})", "\\3.\\2.\\1",sort(as.Date( su

我正在
ui.R
文件中使用
conditionalPanel
。我想比较
selectinput
中的给定日期是在某个日期(2019年9月30日)之前还是之后

我的
selectInput
如下所示:

selectInput(inputId = 'date',
            label = 'Stichtag:',
            choices = sub("([0-9]{4}).([0-9]{2}).([0-9]{2})", "\\3.\\2.\\1",sort(as.Date(
 sub("([0-9]{2})([0-9]{2})([0-9]{4})KRB.csv", "\\1.\\2.\\3",
 list.files('./data', full.names = FALSE,
 recursive = FALSE)),format="%d.%m.%Y"),decreasing = T)
   )),
和我的
条件面板

conditionalPanel(
     #condition = " input.date == '30.09.2019'",  #(this works)                        
     condition="Date.parse(input.date)>Date.parse(30.08.2019)", #(it dose not work)
     ## select the variables and order
               pickerInput(
                   inputId = "assetclass",
                   label = "Assetklassen:",
                   choices = c(sort(unique(bestand.name))),    
                   sort(unique(bestand.name)),  
                   multiple = T
               ) ),
在上面的代码中,您可以看到2个条件。第一个

 condition = " input.date == '30.09.2019'"
工作,但不是一般的智能解决方案,因为我将每3个月,我将有一个额外的日期。 因此,我正在寻找一个通用的解决方案,如

condition="Date.parse(input.date)>Date.parse(30.08.2019)"
我知道我必须使用
Js
。但它不起作用

附录:我试图在
server.R

Browse[2]> input$date
[1] "30.09.2019"
因此,在我比较
JS
中的字符串之前,我可能必须转换日期中的字符串!? 我只是为了好玩,试着说了以下几句话:

 condition= "new Date('2013-05-23') > new Date('2013-05-24')",

但是,它不起作用

>这是如何将日期转换为有效日期,您可以考虑从中创建一个函数:

var date = "30.09.2019"
var arr = date.split(".")

var array = Array.from(arr);
array.reverse();
array.join("/"); // this will return a valid date 
试着像下面这样比较

new Date(input.date) > new Date("2019/09/30")
以下是将为您提供转换日期的函数

function convertDate(data){
var arr = data.split(".")

var array = Array.from(arr);
var converted = array.reverse().join("/")

return converted;
}
现在,在比较的地方,只需使用普通的比较方法

  new Date() > new Date(convertDate("30.09.2019"))
如果它是真的或不是,它将返回


我希望这能对您有所帮助。

我知道您正在询问如何在
conditionalPanel
中比较日期,但无论您想做什么,在服务器端使用
renderUI
都会容易得多

根据您的问题,我假设您有一些季度报告正在运行,并且随着季度的变化,您希望显示不同的过滤器/选择输入

下面我展示了一个玩具示例,它检查所选的
输入$date
是否等于上一个季度的结束日期(
round_date(Sys.date(),“quarty”)-days(1))

注意,我向
stringr
lubridate
添加了库调用

由于我无法重现您提供的代码,因此我进一步构建了csv文件名的字符向量

library("shiny")
library("shinyWidgets")
library("lubridate")
library("stringr")

# made up character vector of csv file names
date_vec <- c("30092019KRB.csv",
              "31082019KRB.csv",
              "31072019KRB.csv",
              "30062019KRB.csv",
              "31052019KRB.csv",
              "30042019KRB.csv")

shinyApp(

  ui = fluidPage( # user interface

    sidebarLayout( # layout with Sidebar

      sidebarPanel( # input sidebarPanel

        selectInput(inputId = 'date',
                    label = 'Stichtag:',
                    choices = sub("([0-9]{4}).([0-9]{2}).([0-9]{2})",
                                  "\\3.\\2.\\1",
                                  sort(as.Date(sub("([0-9]{2})([0-9]{2})([0-9]{4})KRB.csv",
                                                   "\\1.\\2.\\3",
                                       # below date_vec replaces your list.files() call
                                                   date_vec), 
                                               format="%d.%m.%Y"),
                                       decreasing = T)
                    )
        ) ,

        uiOutput("classes")

      ), # closes sidebarPanel

      mainPanel( # Output in mainPabel


      ) # closes mainPanel

    ) # closes sidebarLayout

  ), # closes fluidPage

  server = function(input, output) {

    output$classes <- renderUI({

      # example condition: if input$date is equal to the date of the actual quarter minus 1 day then...
      if(dmy(str_remove(input$date, "KRB.csv")) == (round_date(Sys.Date(), "quarter") - days(1))) {

      # use show this pickerInput ....
      pickerInput(
        inputId = "assetclass",
        label = "Assetklassen:",
        choices = c("class a", "class b", "class c"),
        multiple = T
      )

      # otherwise show this pickerInput ...
      } else {

        pickerInput(
          inputId = "equity",
          label = "Equity classes:",
          choices = c("class d", "class e", "class f"),
          multiple = T
        )  

      }

    })


  }

) # closes shinyApp

正如你正确提到的,由于格式的原因,它不起作用。我在问题中添加了一个附录。我可能必须这样做,但如何做?我已经更新了答案,如果有帮助,请标记为解决方案。条件=“新日期('2019/09/30')>新日期('2019/08/30'),有效!现在,我如何在ur.R中构建trensormation部分,以便将input.date转换为正确的格式?这就是我所说的。用上面的代码创建一个函数,并像convertDate(input.date)一样传递input.date,这样它将转换并返回转换后的日期。如果您对此有问题,我将更新代码。如果您使用
renderUI
而不是
conditionalPanel
,您可以省去很多麻烦。
library("shiny")
library("shinyWidgets")

# made up character vector of csv file names
date_vec <- c("30092019KRB.csv",
              "31082019KRB.csv",
              "31072019KRB.csv",
              "30062019KRB.csv",
              "31052019KRB.csv",
              "30042019KRB.csv")

choice_vec <- gsub("[-]",
                   "/",
                   sort(as.Date(sub("([0-9]{2})([0-9]{2})([0-9]{4})KRB.csv",
                                    "\\3.\\2.\\1",
                                    # below date_vec replaces your list.files() call
                                    date_vec), 
                                format="%Y.%m.%d"),
                        decreasing = T)
)



names(choice_vec) <- sub("([0-9]{4}).([0-9]{2}).([0-9]{2})",
                         "\\3.\\2.\\1",
                         sort(as.Date(sub("([0-9]{2})([0-9]{2})([0-9]{4})KRB.csv",
                                          "\\1.\\2.\\3",
                                          # below date_vec replaces your list.files() call
                                          date_vec), 
                                      format="%d.%m.%Y"),
                              decreasing = T)
)

shinyApp(

  ui = fluidPage( # user interface

    sidebarLayout( # layout with Sidebar

      sidebarPanel( # input sidebarPanel

        selectInput(inputId = 'date',
                    label = 'Stichtag:',
                    choices = choice_vec
        ) ,

        conditionalPanel(
          condition = "new Date('2019/09/30') > new Date(input.date)",
          pickerInput(
            inputId = "assetclass",
            label = "Asset casses:",
            choices = c("class a", "class b", "class c"),
            multiple = T
          )
        )

      ), # closes sidebarPanel

      mainPanel( # Output in mainPabel

      ) # closes mainPanel

    ) # closes sidebarLayout

  ), # closes fluidPage

  server = function(input, output) {



  }

) # closes shinyApp