Shiny:当用户选择" All"时,BigQuery失败值

时间:2015-12-29 22:14:51

标签: r shiny google-bigquery

我正在尝试使用BigQuery查询来填充Shiny中的图。该查询包括使用selectInput从ui输入的值。如果用户选择数据库中存在的值,例如年份是2014,则查询正常工作,但是,我希望用户也能够选择"全部。" "所有"应该是所有值的选择,但是,我不知道如何使用selectInput在查询中表达它。

server.r

data1 <- eventReactive(input$do_sql, {
bqr_auth(token = NULL, new_user = FALSE, verbose = FALSE)

query = paste('select month, event, partner_name, sum(f0_) from [dataset.table] where year =',input$year1,' and partner_name = \"',input$partner_name,'\"
              GROUP by 1,2,3
              ORDER by 1 asc
              LIMIT 10000', sep="")

bqr_query(projectId, datasetId, query, maxResults =2000)
})

ui.r

 ( 
  selectInput("year1", 
              "Year:", 
              c("All",2014,2015
                ))
),

 (
  selectInput("partner_name", 
              "Partner:", 
              c("All", 
                unique(as.character(data5$partner_name))))

2 个答案:

答案 0 :(得分:2)

您应该稍微更改正在构建的查询 所以,目前你有

SELECT month, event, partner_name, SUM(f0_) 
FROM [dataset.table] 
WHERE year = selected_year
AND partner_name = "selected_partner_name"
GROUP BY 1,2,3
ORDER BY 1 ASC
LIMIT 10000

分别:

selected_year --> input$year1
selected_partner_name --> input$partner_name

相反,您应该构建以下查询

SELECT month, event, partner_name, SUM(f0_) 
FROM [dataset.table] 
WHERE (year = selected_year OR "selected_year" = "All") 
AND (partner_name = "selected_partner_name" OR "selected_partner_name" = "All")
GROUP BY 1,2,3
ORDER BY 1 ASC
LIMIT 10000
  

我根本不是闪亮的用户 - 所以请原谅我的语法 - 下面只是我的   关于实施上述建议的猜测

query = paste('SELECT month, event, partner_name, sum(f0_) 
    FROM [dataset.table] 
    WHERE (year =',input$year1,' OR "All" ="',input$year1,'") 
    AND (partner_name = \"',input$partner_name,'\" OR "All" = \"',input$partner_name,'\")
          GROUP by 1,2,3
          ORDER by 1 asc
          LIMIT 10000', sep="")

答案 1 :(得分:0)

Mikhail的解决方案适用于字符变量,但是数字不能正常工作。我决定使用字符日期范围而不是我最初使用的年份数字。感谢。