首页 > 解决方案 > 闪亮的 RQDA 查询

问题描述

我正在尝试根据我最近完成的一些定性数据分析来创建一些动态输出。我需要用户在 Shiny 中进行选择。基于此,我需要使用 SQLite 在 RQDA 中运行查询。

我看过多个关于在 Shiny 中重新激活输入的视频,但我尝试应用的所有内容都没有奏效。我尝试使用 paste() 和 paste0() 为我的查询创建 SQLite 命令的文本块,但闪亮不会让我运行它。

界面代码

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")


             )))

服务器代码

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))
}

警告:.getReactiveEnvironment()$currentContext 中的错误:如果没有活动的反应上下文,则不允许操作。(您试图做一些只能从反应式表达式或观察者内部完成的事情。)这是我得到的错误消息 - 我知道我做的事情显然是错误的,但我似乎看不到它!

标签: rsqliteshinyrqda

解决方案


我使用 Shiny 中的模块找到了我自己的问题的答案——我最近发现了这些指向 R 中模块的链接,并在此基础上重建了我的 Shiny 应用程序,一切似乎都运行良好。

模块化 Shiny Apps 模块之间的通信

谢谢!


推荐阅读