Expressions with cat and paste to go through an SQL request

Hi,
The following simple command:
algo <- "wait see"
expr <-paste(algo, "%")
line <-toString(cat(paste0('select * from tri where unigram like "', expr, '"')))
generates:
select * from tri where unigram like "wait see %"

but as a NULL character
Requests with SQL are not possible therefore. I am looking to get the parameter line as a character string which could go into a sqldf command

Thanks !

algo <- "wait see"
expr <-paste(algo, "%")
# remove errant " from here============================>X
line <-glue::glue('select * from tri where unigram like ', expr, '"')

line
#> select * from tri where unigram like wait see %"

Thanks, you removed a thorn from my foot !

Here below a recap of the solution which works

algo <- "wait see"
expr <-paste(algo, "%")
tri <- data.frame( n= c(15,25), unigram =c("wait see city", "wait and see"))
sen <- glue :: glue('select * from tri where unigram like "', expr, '"')
sqldf(sen)
-> n unigram
1 15 wait see city

-> sen
select * from tri where unigram like "wait see %"

However, I need to limit my previous wording
Although perfect on R Studio, it fails on R shiny
My input "algo" is variable on R Shiny (input$index)

For clarifying, here is an extract of my code:

ui:

ui <- fluidPage(
    fluidRow(column(width = 12, 
                       textInput("Intext",
                       label = h1("Enter here after:"), 
                       value = "")),
             ), 
    textOutput("mytext"),
    
    fluidRow(column(width = 12,
                       br(),
                       submitButton(h2("Predict")),
                       br())
                       ),    
    textOutput("Intext2"),
)

server:

tri <- data.frame( n= c(15,25), unigram =c("wait see city", "wait and see"))

server <- function(input, output) {
    
    #input_vector <- reactive(({input$Intext}))
    output$mytext <- renderText({
    input$Predict
    input$Intext 
    })
    
    output$Intext2 <- renderText({
    algo <- input$index
    #algo <- "wait see"
    expr <-paste(algo, "%")
    sen <- glue::glue('select * from tri where unigram like "', expr, '"')
    sql <- sqldf ::sqldf(sen)
    paste("The algorithm proposes now:", sql[1,2])
    })
}

shinyApp(ui = ui, server = server)

Thanks for any suggestions on this ! :blush:

Glad that helped. (Why, o why, is punctuation so darned invisible?) The sen query looks well-formed. What about sql[1,2] is troubling?

algo <- "wait see"
expr <-paste(algo, "%")
sen <- glue::glue('select * from tri where unigram like "', expr, '"')
sen
#> select * from tri where unigram like "wait see %"

That works perfectly !
I was struggling with punctuation
input$index instead of input$Intext :blush:

1 Like