selectInput in R从MySql中获取数据

时间:2016-03-08 08:07:10

标签: mysql r shiny

我刚开始使用MySql几天了。如果这是一个非常基本的问题,请原谅我。

我在MySql中有一个包含四列的表:content,media_provider,date和sentimet。 Sentimet有三个级别,即pos,neg& neu的。

使用闪亮的selectInput,我试图获取所有标记为pos,neg或neu的记录。以下是代码:

ui <- shinyUI(fluidPage(
    titlePanel("Generic grapher"),
    sidebarLayout(
        sidebarPanel(

            selectInput(inputId = "wafer", label = "Select sentiment", choices = c("pos", "neg", "neu"), 
                        multiple = FALSE, selectize = TRUE, width = "40%"),

            actionButton("do", "An action button")
        ),

        mainPanel(
            verbatimTextOutput("value"),
            verbatimTextOutput("que"),
            verbatimTextOutput("wq_print"),
            dataTableOutput(outputId="post")
        )
    )
)
)

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

    values <- reactiveValues()
    values$df <- data.frame()

    d <- eventReactive(input$do, { input$wafer })

    output$value <- renderPrint({ d() }) 

    a <- reactive({ paste("SELECT * FROM apparel WHERE sentimet = ", d(), sep="") })

    output$que <- renderPrint({ a() }) 

    observe({
        if (!is.null(d())) {
            wq <- reactive({  query( a() ) })

            output$wq_print <- renderPrint({ print(str(wq())) })

            values$df <- rbind(isolate(values$df), wq())
        }
    })

    output$post <- renderDataTable({ values$df })  

})

shinyApp(ui, server)

当我跑步时,我收到以下错误:

Warning: Error in .local: could not run statement: Unknown column 'neg' in 'where clause'

我理解MySql会考虑&#39;因素&#39;作为列&#39; neg&#39;,所以我在&#34; pos&#34;中改变了selectInput中的选择选项。到&#39; pos&#39; = 1,MySql查找值1,并返回NULL。我改变了1 =&#34; pos&#34;,&#34; pos&#34; =&#34; pos&#34;,没有任何效果。但是,当我直接在MySql&#34; SELECT * FROM服装中提供查询时,WHERE sentimet =&#34; pos&#34 ;;&#34;完美无瑕。不确定如何使用selectInput。提供下面的3行可重现的例子:

structure(list(CONTENT = c("@myntra Good Morning If you are born poor its not your mistake, But if you die poor its your mistake. -Bill Gates Good Day", 
"@myntra i have been mailing my issue daily since past week.All i get in reply is an auto generated assurance mail. 1st time pissed wd myntra", 
"@myntra I'm a big Arsenal fan & made a big PUMA collection! ¥Ë_ Shared that collection yesterday. Wanna win... ¥Ë_¥Ë_¥Ë_ #myPUMAcollection"
), MEDIA_PROVIDER = c("TWITTER", "TWITTER", "TWITTER"), PUBLISH_DATE = structure(list(
    sec = c(0, 0, 0), min = c(30L, 22L, 11L), hour = c(7L, 7L, 
    7L), mday = c(21L, 21L, 21L), mon = c(10L, 10L, 10L), year = c(115L, 
    115L, 115L), wday = c(6L, 6L, 6L), yday = c(324L, 324L, 324L
    ), isdst = c(0L, 0L, 0L), zone = c("IST", "IST", "IST"), 
    gmtoff = c(NA_integer_, NA_integer_, NA_integer_)), .Names = c("sec", 
"min", "hour", "mday", "mon", "year", "wday", "yday", "isdst", 
"zone", "gmtoff"), class = c("POSIXlt", "POSIXt"), tzone = "Asia/Calcutta"), 
    sentimet = c("neg", "pos", "neu")), .Names = c("CONTENT", 
"MEDIA_PROVIDER", "PUBLISH_DATE", "sentimet"), class = "data.frame", row.names = c(NA, 
3L))

1 个答案:

答案 0 :(得分:0)

sentimet列中的值是字符,因此您需要用单引号将它们包装起来:

a <- reactive({ paste0("SELECT * FROM apparel WHERE sentimet = '", d(), "'") })