在Shinyapp上使用多个文本字符串查询Mysql

时间:2019-07-04 11:56:36

标签: mysql r shiny

我想在闪亮的应用程序上通过Mysql查询不同的文本字符串。

例如,在下面的应用中,我想搜索文本字符串“ data”,并从statquotes库提供的引号数据框中获取所有带有单词“ data”的引号。

此外,我想使用OMIT输入过滤包含某些单词(例如“技术”,“关键”)的报价。

library(shiny)
library(shinydashboard)
library(sqldf)
library(statquotes)

  data(quotes)
  quotes

ui <- dashboardPage(
    dashboardHeader(),
    dashboardSidebar(
  sidebarMenu(
  menuItem("TexSearch", tabName = "Tabs", icon = icon("object-ungroup"))) ),



dashboardBody(
   tabItem(tabName = "Tabs",
        fluidRow(
          column(width=3, 
                 box(
                   title="Search ",
                   solidHeader=TRUE,
                   collapsible=TRUE,
                   width=NULL,
                   submitButton("Search"),
                   textInput("wordsearch", "Search 1"),                       
                   textInput("wordomit", "OMIT"))),

          column( width=9,
                  tabBox(
                    width="100%",
                    tabPanel("tab1", 
                             htmlOutput("searchtext")
                    )))))))

 server <- function(input, output, session) {
         output$searchtext <- renderUI({
          if (input$wordsearch != "") {
            searches <- reactive({
                if (input$wordomit == "") {
                sqldf(paste0("SELECT  *
                           FROM quotes
                           WHERE text LIKE '%",input$wordsearch,"%'"))
                        } else {  

                sqldf(paste0("SELECT  *
                           FROM quotes
                           WHERE  (text LIKE '%",input$wordsearch,"%'
                           AND text NOT LIKE '%",input$wordomit,"%' )"))

                  }
                  }) 


     output = ""
        relevantquotes <- searches()
       if (dim(relevantquotes)[1] < 100) {
        output <-
            paste(output,
            "<b>Number of quotes: ",
            as.character(dim(relevantquotes)[1]),
            "</b>.<br/>")
    for (i in seq(from = 1,
                  to = dim(relevantquotes)[1])) {
      output <- paste(output,
                   paste("qid: ", relevantquotes[i, "qid"]),
                   sep = "<br/><br/>")
      output <- paste(output,
                   paste("topic: ", relevantquotes[i, "topic"]),
                   sep = "<br/><br/>")
      output <- paste(output,

                     paste("text: ", relevantquotes[i, "text"]),
                   sep = "<br/><br/><br/>")

    }

  }
  HTML(output)
   }
   })

   }
shinyApp(ui, server)

目前,我一次只能省略一个词。有没有一种方法可以组合不同的单词以通过一个textInput省略。 我在下面尝试了这种方法,但没有成功:

omit <-    stringr::str_c(stringr::str_c("'", input$wordomit, "'"), collapse = ',')

查询中的插入内容如下:

sqldf(paste0("SELECT  *    
                FROM quotes
                WHERE (text LIKE '%",input$wordsearch,"%'
                AND text  NOT IN (",omit,") )

由于我使用的是数据库,因此我的应用中的查询语言为MySQL,但对于此可重现的示例,我使用sqldf

0 个答案:

没有答案