【问题标题】:selectInput in R fetching data from MySqlR中的selectInput从MySql中获取数据
【发布时间】:2016-06-22 02:04:32
【问题描述】:

这几天我才开始使用 MySql。如果这是一个非常基本的查询,请原谅我。

我在 MySql 中有一个表,它有四列:内容、媒体提供者、日期和情感。 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 将“因子”否定视为列“否定”,因此我将 selectInput 中的选择选项从“pos”更改为“pos”=1,MySql 查找值 1,并返回 NULL。我改变了 1 = "pos", "pos" = "pos",没有任何效果。但是,当我直接在 MySql 中提供查询时“SELECT * FROM 服装 WHERE sentimet = “pos”;”完美无缺。不确定如何使用 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))

【问题讨论】:

    标签: mysql r shiny


    【解决方案1】:

    您的情感列中的值是字符,因此您需要用单引号将它们括起来:

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

    【讨论】:

    • 谢谢...这有效!我尝试使用双双引号......“”“”就像在excel中一样。再次感谢您。
    • 好吧,问题是,如果你在paste0 调用中使用双引号,你就不能用它们来包装你的sentimet 列的元素,否则它看起来像这样:@ 987654323@ 和 R 将抛出错误,因为这不是有效的语法。使用单引号,它看起来像这样:"SELECT * FROM apparel WHERE sentimet = 'pos'",这是有效的。
    • 再次感谢您。我只是遵循与其他小部件相同的原则......而且它的工作就像轻而易举。
    猜你喜欢
    • 2019-07-19
    • 2018-03-17
    • 1970-01-01
    • 2017-11-30
    • 2019-06-21
    • 2017-12-07
    • 1970-01-01
    • 2019-03-15
    相关资源
    最近更新 更多