MLEN
MLEN

Reputation: 2561

Executing SQL with sqldf using reactive values in Shiny

I have an app where some data is stored as reactive values and the possibility to execute SQL queries using sqldf()

Is it possible for a user to join df_one and df_two and store it as df_tot? What is a good way to have sqldf look inside the reactive values?

library(shiny)
library(sqldf)

ui <- fluidPage(

  pickerInput('available_data_sets', 'Data sets', choices = c('df_one', 'df_two')),
  textInput('store_table_as', 'Store table as', value = ''),
  textInput('sql_query', 'sql_query'),
  actionButton('execute', 'execute')

)

server <- function(input, output, session) {

  available_data_sets <- reactiveValues()

  df_one <- data.frame(a = c(1, 2, 3), b = c('a', 'b', 'c'))
  df_two <- data.frame(x = c(4, 5, 6), b = c('a', 'b', 'c'))


  available_data_sets[['df_one']] <- df_one
  available_data_sets[['df_two']] <- df_two

  observeEvent(input$execute, {

    req(input$sql_query, input$store_table_as)

    available_data_sets[[input$store_table_as]] <- sqldf(input$sql_query)

    new_names <- names(available_data_sets)

    updatePickerInput(session, 'available_data_sets', choices = new_names)


  })


}

shinyApp(ui, server)

Upvotes: 1

Views: 260

Answers (1)

MLEN
MLEN

Reputation: 2561

sqldf(input$sql_query, envir = list2env(available_data_sets))

Upvotes: 1

Related Questions