Syntax problem with Inputs and SQLDF

Hi people!

I'm having a peoblem with syntax in RShiny.

If i have some input called that way:

insertUI(
        selector = "#pesquisa",
        where = "afterEnd",
        ui = selectInput(
          "grau",
          label = "Grau:",
          choices = df$GRAU
        )
      )

How can i handle a the data using SQLDF?
I'm trying this:

test <- sqldf('select something
                    from my_df
                    where something = (input$grau)
                    ')

There is an error in my code, but i could not find it.

I have to put my selected choise in the "grau" input in the WHERE condition.

Thx!

library("sqldf")
database: Oracle

I think glue::glue_sql can be of help here. See the example there:

I usually use it with DBI not slqdf but I guess it is ok also.

1 Like

Hi guys!

How can I store a value selected by an insertUI widget?
Like:

observeEvent(input$buscaciclo, {
      
      insertUI(
        selector = "#buscaciclo",
        where = "afterEnd",
        ui = selectInput(
          "ciclo",
          label = "Ciclo:",
          choices = df$CICLO
        )
      )
    })

I just to get the value selected in this widget and pass it like a value to my query:

sqldf('select anything
                  from my_df
                  where anything = "input$ciclo"
                  ')

I'm trying to store this value to pass in WHERE clause, but it just not working.

You have the right idea, the selection from the inserted selectInput is stored in input$ciclo. But in your query it's inside quotes so it's just a string. Use paste() to access the string directly.

input <- list(ciclo = "segundo")

cat('where anything = "input$ciclo"')
#> where anything = "input$ciclo"

cat(paste0('where anything = "', input$ciclo, '"'))
#> where anything = "segundo"

Note that because you control the options in the selectInput it's generally safe to send the results into a query, but that you should consider using a more safe method such as DBI::sqlInterpolate(). (I'm not familiar with sqldf() so not sure what the analogue there would be.)

1 Like

I'll try this, man.

Very thanks!

This topic was automatically closed 54 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.