Created
December 19, 2023 20:08
-
-
Save daranzolin/1a9dc908f461d7be4afa379fc19ac07f to your computer and use it in GitHub Desktop.
Shiny Gadget to load SQL Queries (ugly and not totally working)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(shiny) | |
library(miniUI) | |
ui <- miniPage( | |
gadgetTitleBar("Import SQL Query"), | |
miniTabstripPanel( | |
selected = "Input", | |
# --- UI - Tab - Input ---- | |
miniTabPanel( | |
"Input", | |
# icon = icon("file-text-o"), | |
miniContentPanel( | |
fillCol( | |
fileInput("file", "Choose SQL File", accept = ".sql") | |
) | |
) | |
), | |
# --- UI - Tab - Query ---- | |
miniTabPanel( | |
"Query", | |
miniContentPanel( | |
verbatimTextOutput("queryText") | |
) | |
), | |
# --- UI - Tab - Preview ---- | |
miniTabPanel( | |
"Preview", | |
miniContentPanel( | |
fillCol( | |
miniContentPanel( | |
tableOutput("previewTable") | |
) | |
) | |
) | |
), | |
# --- UI - Tab - Output ---- | |
miniTabPanel( | |
"Output", | |
miniContentPanel( | |
fillCol( | |
textInput("outVar", "Variable Name"), | |
br(), | |
actionButton("sendToGlobalEnv", "Send to Environment") | |
) | |
) | |
) | |
) | |
) | |
server <- function(input, output, session) { | |
input_file <- reactive({ | |
if (is.null(input$file)) { | |
return("") | |
} | |
readr::read_lines(input$file$datapath) | |
}) | |
output$queryText <- renderText({ | |
req(input_file()) | |
lines <- input_file() | |
paste(lines[2:length(lines)], collapse = "\n") | |
}) | |
output$previewTable <- renderTable({ | |
req(input_file()) | |
lines <- input_file() | |
connection_string <- stringr::str_extract(lines[1], "(?<=\\=).*") | |
connection_call <- paste0("con <- ", connection_string) | |
eval(parse(text = connection_call)) | |
on.exit(DBI::dbDisconnect(con)) | |
query <- paste(lines[2:length(lines)], collapse = " ") | |
out <- DBI::dbGetQuery(con, query) | |
table_out <- reactiveVal(out) | |
out | |
}) | |
observeEvent(input$sendToGlobalEnv, { | |
assign(input$outVar, table_out, envir = globalenv()) | |
}) | |
observeEvent(input$done, { | |
stopApp(TRUE) | |
}) | |
} | |
runGadget(ui, server, viewer = paneViewer()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment