0

I am trying to create some dynamic outputs based on some qualitative data analysis I have recently completed. I need users to make a selection in Shiny. Based on this, I need to run a query in RQDA using SQLite.

I've watched multiple videos on reactivating inputs in Shiny but everything I've tried to apply hasn't worked. I've tried to create a text block of SQLite commands for my query using paste() and paste0() but shiny won't let me run this.

UI code

library(shiny)
library(igraph)
library(sqldf)
library(RQDA)
library(DBI)
# Open the project
openProject("C:/SWOT Analysis/Qual Analysis of SWOTs.rqda")

# Get the table required for inputs
db <- dbConnect(SQLite(), dbname="Qual Analysis of SWOTs.rqda")

# Read in the inputs
codeCat <- dbReadTable(db, "codecat")[which(dbReadTable(db,"codecat")$status==1),c(3,1)]

# Set to a vector format for shiny
codeCat1 <- setNames(codeCat$catid, codeCat$name)

ui <- fluidPage(

  tabsetPanel(
    tabPanel(title = "2 Categories",
             tags$h1("Compare two coded categories"),
             wellPanel(selectInput(inputId = "Opt1", 
                                   label = "Select the first category",
                                   choices = codeCat1,
                                   selected = codeCat1[1]),

                       selectInput(inputId = "Opt2", 
                                   label = "Select the second category",
                                   choices = codeCat1,
                                   selected = codeCat1[2])),
             textOutput(outputId = "cat2"),
             plotOutput(outputId = "pcat2"),
             tableOutput(outputId = "tbl")


             )))

Sever code

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

  output$cat2 <- renderText({paste(input$Opt1, "and", input$Opt2, "have been selected.")})


a <- renderText({paste("SELECT codecat.name, freecode.name FROM codecat, freecode, treecode WHERE codecat.catid=treecode.catid AND freecode.id=treecode.cid AND treecode.status=1 and treecode.catid = ", input$Opt1, "or codecat.catid=treecode.catid AND freecode.id=treecode.cid AND 
treecode.status=1 and treecode.catid = ", input$Opt2)})


  # testing to see if the RQDA query works - this did
  output$tbl <-renderTable({RQDAQuery(paste("SELECT codecat.name, freecode.name FROM codecat, freecode, treecode WHERE codecat.catid=treecode.catid AND freecode.id=treecode.cid AND treecode.status=1 and treecode.catid = ", input$Opt1, "or codecat.catid=treecode.catid AND freecode.id=treecode.cid AND 
treecode.status=1 and treecode.catid = ", input$Opt2))})

# this will be used for plotting after doing clusters later
 # edges <- RQDAQuery(a())
  on.exit(DBI::dbDisconnect(db))
}

Warning: Error in .getReactiveEnvironment()$currentContext: Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.) This is the error message I get - I know I'm doing something obviously wrong but I can't seem to see it!

Adrita Sharma
  • 21,581
  • 10
  • 69
  • 79

1 Answers1

0

I have found the answer to my own question using modules in Shiny - I have recently discovered these links to modules in R and rebuilt built my Shiny App based on this and it all seems to be running fine.

Modularizing Shiny Apps Communication Between Modules

Thanks!