首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Google Big Query中的闪亮反应对象中存在问题

Google Big Query中的闪亮反应对象中存在问题
EN

Stack Overflow用户
提问于 2021-10-28 15:23:10
回答 2查看 118关注 0票数 0

我只想下载并绘制selectInput在Shiny中选择的state几何图形。但是,当我尝试在来自大查询表(stands_sel())的反应对象中使用st_as_sf时,它不起作用,输出是:

代码语言:javascript
复制
Listening on http://127.0.0.1:5221
Warning: Error in $: $ operator is invalid for atomic vectors
  186: unique
  185: <reactive:stands_sel> [C:/Users/fores/Downloads/teste_sf.R#60]
  183: .func
  180: contextFunc
  179: env$runWith
  172: ctx$run
  171: self$.updateValue
  169: stands_sel
  167: renderPlot [C:/Users/fores/Downloads/teste_sf.R#71]
  165: func
  125: drawPlot
  111: <reactive:plotObj>
   95: drawReactive
   82: renderFunc
   81: output$myplot
    1: runApp

在我的示例中,我这样做:

代码语言:javascript
复制
library(shinythemes)
library(dplyr)
library(ggplot2)
library(bigrquery)
library(DBI)
library(sf)
library(glue)

# Open a public BigQuery dataset eg. "geo_us_boundaries"
bq_con <- dbConnect(
  bigrquery::bigquery(),
  project = "bigquery-public-data",
  dataset = "geo_us_boundaries",
  billing = "my-project"
)
bigrquery::dbListTables(bq_con) # List all the tables in BigQuery data set


# Take the table
dataset <- dplyr::tbl(bq_con, 
                      "states") # connects to a table


# Enumerate the states
dataset_vars <- dataset %>% dplyr::distinct(geo_id, state, state_name)%>% 
  collect() 
str(dataset_vars)


# Create the shiny dash
ui <- fluidPage(
  theme = shinytheme("cosmo"),
  titlePanel(title="States Dashboard"),  
  sidebarLayout(
    sidebarPanel(
      selectInput(inputId = "selectedvariable0",
                  label = "Geo ID", 
                  choices = c(unique(dataset_vars$geo_id)),selected = TRUE ), 
      selectInput(inputId = "selectedvariable1",
                  label = "State", 
                  choices = c(unique(dataset_vars$state)),selected = TRUE ), 
      selectInput(inputId = "selectedvariable2",
                  label = "State name", 
                  choices = c(unique(dataset_vars$state_name)),selected = TRUE )
    ),
    mainPanel(
      textOutput("idSaida"),
      fluidRow(
        splitLayout(plotOutput("myplot")))
    )
  )
)
server <- function(input, output){
  
  
  # # Selection of variables for plots constructions
      currentvariable1 <- reactive({input$selectedvariable1})
  
    stands_sel <- reactive({ 
      var0 <- unique(currentvariable1()$state)
      sqlInput <- glue::glue_sql("SELECT * FROM states WHERE state = {var0}", .con=bq_con)
      dbGetQuery(bq_con, as.character(sqlInput), stringsAsFactors = T)
      })
  
   
  observe({

       output$myplot <- renderPlot({
         
         #Create the plot
         stands_sel <- st_as_sf(stands_sel(), wkt = "state_geom", crs = 4326)
         ggplot() +
           geom_sf(data=stands_sel) 
       })
   }) #end of observe function.
}
shinyApp(ui, server)
#

请问,有没有帮助解决这个问题?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-11-04 17:46:23

函数glue_sql不接受使用glue::glue_sql("SELECT * FROM states WHERE state = {var0}", .con=bq_con)的反应对象,但需要将glue_sql调用移到反应表达式(glue::glue_sql("SELECT * FROM states WHERE state = {x}", x = input$selectedvariable1, .con=bq_con))中。

解决方案代码:

代码语言:javascript
复制
library(shinythemes)
library(dplyr)
library(ggplot2)
library(bigrquery)
library(DBI)
library(sf)
library(glue)

# Open a public BigQuery dataset eg. "geo_us_boundaries"
bq_con <- dbConnect(
  bigrquery::bigquery(),
  project = "bigquery-public-data",
  dataset = "geo_us_boundaries",
  billing = "my-project"
)
bigrquery::dbListTables(bq_con) # List all the tables in BigQuery data set


# Take the table
dataset <- dplyr::tbl(bq_con, 
                      "states") # connects to a table


# Enumerate the states
dataset_vars <- dataset %>% dplyr::distinct(geo_id, state, state_name)%>% 
  collect() 
str(dataset_vars)


# Create the shiny dash
ui <- fluidPage(
  theme = shinytheme("cosmo"),
  titlePanel(title="States Dashboard"),  
  sidebarLayout(
    sidebarPanel(
      selectInput(inputId = "selectedvariable0",
                  label = "Geo ID", 
                  choices = c(unique(dataset_vars$geo_id)),selected = TRUE ), 
      selectInput(inputId = "selectedvariable1",
                  label = "State", 
                  choices = c(unique(dataset_vars$state)),selected = TRUE ), 
      selectInput(inputId = "selectedvariable2",
                  label = "State name", 
                  choices = c(unique(dataset_vars$state_name)),selected = TRUE )
    ),
    mainPanel(
        fluidRow(
          splitLayout(plotOutput("myplot")))
      
    )
  )
)
server <- function(input, output){
  
  # # Selection of variables for plots constructions
  
  sqlInput <- reactive({
    glue::glue_sql("SELECT * FROM states WHERE state = {x}", x = input$selectedvariable1, .con=bq_con)
  })
  stands_sel <- function() dbGetQuery(bq_con, as.character(sqlInput()), stringsAsFactors = T)

  print(sqlInput)
  
  observe({

  output$myplot <- renderPlot({

  #Create the plot 
    stands_sel_sf <- st_as_sf(stands_sel(), wkt = "state_geom", crs = 4326) 
    ggplot() + geom_sf(data=stands_sel_sf) }) 
  }) #end of observe function.
}
shinyApp(ui, server)
#

票数 0
EN

Stack Overflow用户

发布于 2021-10-29 15:39:10

出现此错误的原因是“$”运算符未设计为访问向量元素。如果我们使用“$”运算符来访问向量元素,则R不理解它并认为它是无效的;因此,我们必须非常小心地在哪里使用“$”运算符。当我们为元素命名,并开始认为我们可以将它们视为数据框列时,就会发生这种情况,这是一个错误的方法。要访问向量元素,我们应该使用单方括号。

原子向量的各个元素可以被顺序访问。例如,position 1,position 2等。它的符号是vect1,vect2,vect3。

您还可以将其以不同的格式表示为变量,以便包含在迭代循环中。但是,在访问原子向量的各个部分时,您必须小心。如果向量的某些部分被错误地访问或以不正确的顺序访问,您可能会看到“$ operator is invalid for atomic”错误消息。

您可以通过将“$”运算符转换为括号引用来修复这些错误。也可以使用getElement()函数。

你可以签入你的代码,在其中你使用了“$”符号。

代码语言:javascript
复制
> testlist <- c(1,2,3) 
> testlist$s
Error in testlist$s : $ operator is invalid for atomic vectors
> testlist[1]
[1] 1
> getElement(testlist,1)
[1] 1

正如您所看到的,我们通过使用“$”操作符尝试不正确地访问原子向量(我们的数组),成功地触发了本例中的错误。通过使用方括号或getElement(),我们可以获得所需的结果。

你可以看到更多的documentation

你可以在这个向量中看到这个例子。

代码语言:javascript
复制
> set.seed(1)
> x1<-sample(1:10,20,replace=TRUE)
> x1
[1] 9 4 7 1 2 7 2 3 1 5 5 10 6 10 7 9 5 5 9 9
> names(x1)<-LETTERS[1:20]
> x1
A B C D E F G H I J K L M N O P Q R S T
9 4 7 1 2 7 2 3 1 5 5 10 6 10 7 9 5 5 9 9
> x1$K
Error in x1$K : $ operator is invalid for atomic vectors

在这里,我们得到的错误是“$ operator对于原子向量无效”。现在,我们应该使用单方括号访问向量x1的元素,如下面的−所示

代码语言:javascript
复制
> x1["K"]
K
5
> x1["T"]
T
9
> x1["A"]
A
9
> x1[1]
A
9

你可以在this link上看到更多信息。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69756818

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档