【问题标题】:Shiny: Filter/Selection of variables without downloading all data from BigQuery tableShiny:过滤/选择变量而不从 BigQuery 表中下载所有数据
【发布时间】:2025-11-30 22:20:02
【问题描述】:

我想在 BigQuery 的 baseball 公共数据中选择变量(yearseasonTypedayNighthomeFinalRuns),而不下载所有数据。我尝试使用reactive() 变量作为目标字符串的过滤器但没有成功,因为我有错误:

Complete
Billed: 0 B
Downloading first chunk of data.
First chunk includes all requested rows.

Listening on http://127.0.0.1:4784
Warning: Error in : Cannot translate a shiny reactive to SQL.
* Force evaluation in R with (e.g.) `!!foo()` or `local(foo())`
70: <Anonymous>
  Error : Cannot translate a shiny reactive to SQL.
* Force evaluation in R with (e.g.) `!!foo()` or `local(foo())

我尝试的代码是:

library(shinythemes)
library(dplyr)
library(ggplot2)
library(bigrquery)
library(DBI)

# Open a public BigQuery dataset eg. "baseball"
bq_con <- dbConnect(
  bigrquery::bigquery(),
  project = "bigquery-public-data",
  dataset = "baseball",
  billing = "my_project_id"
)
bigrquery::dbListTables(bq_con) # List all the tables in BigQuery data set
# My email google option
1
#[1] "games_post_wide" "games_wide"      "schedules"    
#

# Selection of target data set
dataset <- dplyr::tbl(bq_con, 
                      "games_wide") # connects to a table

# Selection of reactive strings 
dataset_vars <- dataset %>% distinct(year,seasonType,dayNight,homeFinalRuns)%>% 
  collect() 
dataset_vars
# Create the shiny dash
ui <- fluidPage(
  theme = shinytheme("cosmo"),
  titlePanel(title="My Baseball Dashboard"),  
  sidebarLayout(
    sidebarPanel(
      selectInput(inputId = "selectedvariable0",
                  label = "Year", 
                  choices = c(unique(dataset_vars$year)),selected = TRUE ), 
      selectInput(inputId = "selectedvariable1",
                  label = "Season", 
                  choices = c(unique(dataset_vars$seasonType)),selected = TRUE ), 
      selectInput(inputId = "selectedvariable2",
                  label = "Period", 
                  choices = c(unique(dataset_vars$dayNight)),selected = TRUE ),
      
      selectInput(inputId = "selectedvariable3",
                  label = "Final Runs", 
                  choices = c(unique(dataset_vars$homeFinalRuns)),selected = TRUE )      
    ),
    mainPanel(
      textOutput("idSaida"),
      fluidRow(
        splitLayout(plotOutput("myplot")))
    )
  )
)
server <- function(input, output){
  
  currentvariable0 <- reactive({input$selectedvariable0})
  currentvariable1 <- reactive({input$selectedvariable1})
  currentvariable2 <- reactive({input$selectedvariable2})
  currentvariable3 <- reactive({input$selectedvariable3})
  
  
  # Selection of variables for plots constructions 
  dataset_sel <- dataset %>% filter(year=currentvariable0(),homeFinalRuns==currentvariable0())%>% 
    collect() 
  
  
  observe({ 
    if(currentvariable2()=="D"){
      output$myplot <- renderPlot({
        
        #Create the plot
        ggplot(data=dataset_sel, aes(x=currentvariable0(), y=currentvariable3())) +
          geom_bar(stat="identity")
      })
    } else {
      #Create the plot
      ggplot(data=dataset_sel, aes(x=currentvariable0(), y=currentvariable3())) +
        geom_bar(stat="identity")
      
    }
  }) #end of observe function.
}
shinyApp(ui, server)
#

如果我在中使用!!

  # Selection of variables for plots constructions 
  dataset_sel <- dataset %>% filter(year=!!currentvariable0(),homeFinalRuns==!!currentvariable0())%>% 
    collect() 

发生了一个新错误:

Complete
Billed: 0 B
Downloading first chunk of data.
First chunk includes all requested rows.

Listening on http://127.0.0.1:4784
Warning: Error in : Operation not allowed without an active reactive context.
* You tried to do something that can only be done from inside a reactive consumer.
  60: <Anonymous>
Error : Operation not allowed without an active reactive context.
* You tried to do something that can only be done from inside a reactive consumer.

如果我通过以下查询方法进行更改:

var1 <- currentvariable0()
var2 <- currentvariable3()
sqlInput <- paste("SELECT",var1,"as year, 'REG' as seasonType,'N' as dayNight,",var2,"as homeFinalRuns FROM games_wide LIMIT 30")
dataset_sel <- dbGetQuery(bq_con, sqlInput, stringsAsFactors = T)

也不行:

Warning: Error in : Operation not allowed without an active reactive context.
* You tried to do something that can only be done from inside a reactive consumer.
  55: <Anonymous>
Error : Operation not allowed without an active reactive context.
* You tried to do something that can only be done from inside a reactive consumer.

请帮忙解决一下?

【问题讨论】:

    标签: r shiny google-bigquery bigrquery


    【解决方案1】:

    问题解决了,没有更多错误了!!选择数据集也必须是reactive({}),并且第一个选择中的选择元素是查询 paste("SELECT",var1,"as year, 'REG' as seasonType,'N' as dayNight,",var2,"as homeFinalRuns FROM games_wide LIMIT 30")

    library(shinythemes)
    library(dplyr)
    library(ggplot2)
    library(bigrquery)
    library(DBI)
    
    # Open a public BigQuery dataset eg. "baseball"
    bq_con <- dbConnect(
      bigrquery::bigquery(),
      project = "bigquery-public-data",
      dataset = "baseball",
      billing = "my_project_id"
    )
    bigrquery::dbListTables(bq_con) # List all the tables in BigQuery data set
    
    #[1] "games_post_wide" "games_wide"      "schedules"    
    #
    
    # Selection of target data set
    dataset <- dplyr::tbl(bq_con, 
                          "games_wide") # connects to a table
    # My email google option
    2
    
    # Selection of reactive strings 
    dataset_vars <- dataset %>% distinct(year,seasonType,dayNight,homeFinalRuns)%>% 
      collect() 
    dataset_vars
    
    
    # Create the shiny dash
    ui <- fluidPage(
      theme = shinytheme("cosmo"),
      titlePanel(title="My Baseball Dashboard"),  
      sidebarLayout(
        sidebarPanel(
          selectInput(inputId = "selectedvariable0",
                      label = "Year", 
                      choices = c(unique(dataset_vars$year)),selected = TRUE ), 
          selectInput(inputId = "selectedvariable1",
                      label = "Season", 
                      choices = c(unique(dataset_vars$seasonType)),selected = TRUE ), 
          selectInput(inputId = "selectedvariable2",
                      label = "Period", 
                      choices = c(unique(dataset_vars$dayNight)),selected = TRUE ),
          
          selectInput(inputId = "selectedvariable3",
                      label = "Final Runs", 
                      choices = c(unique(dataset_vars$homeFinalRuns)),selected = TRUE )      
        ),
        mainPanel(
          textOutput("idSaida"),
          fluidRow(
            splitLayout(plotOutput("myplot")))
        )
      )
    )
    server <- function(input, output){
      
      currentvariable0 <- reactive({input$selectedvariable0})
      currentvariable1 <- reactive({input$selectedvariable1})
      currentvariable2 <- reactive({input$selectedvariable2})
      currentvariable3 <- reactive({input$selectedvariable3})
    
      
      # # Selection of variables for plots constructions
       
      dataset_sel <- reactive({ 
        var1 <- currentvariable0()
        var2 <- currentvariable3()
        sqlInput <- paste("SELECT",var1,"as year, 'REG' as seasonType,'N' as dayNight,",var2,"as homeFinalRuns FROM games_wide LIMIT 30")
        dbGetQuery(bq_con, sqlInput, stringsAsFactors = T)})
    
    
      observe({
        if(currentvariable2()=="D"){
          output$myplot <- renderPlot({
    
            #Create the plot
            ggplot(data=dataset_sel(), aes(x=year, y=homeFinalRuns)) +
              geom_bar(stat="identity")
          })
        } else {
          #Create the plot
          ggplot(data=dataset_sel(), aes(x=year, y=homeFinalRuns)) +
            geom_bar(stat="identity")
    
        }
      }) #end of observe function.
    }
    shinyApp(ui, server)
    #
    

    【讨论】: