-1

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

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.

请帮忙解决一下?

4

1 回答 1

0

问题解决了,没有错误了!!选择数据集也必须是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)
#
于 2021-10-08T17:56:03.780 回答