Using dateRangeInput with BETWEEN logic in SQL

library(shiny)
library(ROracle)
library(glue)

shinyApp(
    ui = fluidPage(
dateRangeInput("iData", 
                                  label = ("Periodo"),
                                  start = Sys.Date() - 365, end = Sys.Date() - 1,
                                  format = "dd/mm/yyyy",
                                  language = "pt-BR",
                                  separator = "ate"
                                  
                   )
    ),

    server = function(input, output, session) {
df2 <- dbGetQuery(
      connection,
      query <- glue(
        "select 
        DATA_ENFORNAMENTO_TF2,
        YS,
        UTS
        
        from 
        DWA_AM_TRACA 

        where 
        DATA_ENFORNAMENTO_TF2 between DATE '{as.character(input$iData[1])}' and DATE '{as.character(input$iData[2])}'
                  ")
                )
    }
  )

I tried with other filters in WHERE CONDITION and it worked. But when I insert the DATA CONDITION, it generates an error.

As I said, for getting access to input$ values you need to put your code inside a reactive expression.

This seems to produce valid sql code

library(shiny)
library(glue)

ui <- fluidPage(
    
    dateRangeInput("iData", 
                   label = "Periodo",
                   start = Sys.Date() - 365, end = Sys.Date() - 1,
                   format = "dd/mm/yyyy",
                   language = "pt-BR",
                   separator = "ate"
    ),
    verbatimTextOutput(outputId = "query")
)

server <- function(input, output) {
    
    output$query <- renderText({
    glue("
    select 
        DATA_ENFORNAMENTO_TF2,
        YS,
        UTS
    from 
        DWA_AM_TRACA
    where 
        DATA_ENFORNAMENTO_TF2 between DATE '{as.character(input$iData[1])}' and DATE '{as.character(input$iData[2])}'"
         )
    })
}

shinyApp(ui = ui, server = server)

In your case, you should use renderTable()

output$query <- renderTable({
        df2 <- dbGetQuery(
            con,
            glue("
    select 
        DATA_ENFORNAMENTO_TF2,
        YS,
        UTS
    from 
        DWA_AM_TRACA
    where 
        DATA_ENFORNAMENTO_TF2 between DATE '{as.character(input$iData[1])}' and DATE '{as.character(input$iData[2])}'"
            )
        )
        df2
    })

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.