numericInput starting with zero not recognizing

People, I'm trying to query a search using GLUE Package and a numericInput, like this:

dfPlot2 <- dbGetQuery(
    connection_reportUser,
    query <- glue(
      "
select * from test
where cod_pedido = '{input$iPedido}'
"))

My input in UI.R has:
numericInput(inputId= "iPedido", label= "Pedido:", value = 0)

The Problem is: My value in this numericInput is '0000545461'

I have seen something about numbers started with '0' what makes R understand '545461' instead of '0000545461'. So, my search never works until I pass the value direct into the code, like:
where cod_pedido = 0000545461

Someone know how to solve this?
Ps.: This number will ALWAYS start with four 0's.

You can use a textInput() or use something like this on the query

glue(
    "
select * from test
where cod_pedido = '{paste0('0000', input$iPedido)}'
"))
1 Like

You should also use glue::glue_sql() here; this protects your app from SQL injection attacks.

2 Likes

@hadley, I tried to use glue::glue_sql() with many syntax, consulting many other codes, but it just not worked.

How can I put the following query in that syntax?

dfpost <- dbGetQuery(
  connection,
  query <- glue(
  "
  select * from jcb_qt_qts.pla_ordem_producao pla
  where pla.cod_pedido = '{paste0('0000', input$iPedido)}'
  and pla.cod_item = '{paste0('0000', input$iItem)}'
  "
              )
)
con <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")

x <- 1
y <- 2

glue::glue_sql("
  SELECT * FROM jcb_qt_qts.pla_ordem_producao pla
  WHERE pla.cod_pedido = {paste0('0000', x)}
    AND pla.cod_item = {paste0('0000', y)}
", .con = con)
#> <SQL>   SELECT * FROM jcb_qt_qts.pla_ordem_producao pla
#>   WHERE pla.cod_pedido = '00001'
#>     AND pla.cod_item = '00002'

Created on 2019-10-08 by the reprex package (v0.3.0)

1 Like

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