2

I am trying to add connection snippet to the bigrquery package based on this documentation:

https://db.rstudio.com/advanced/snippets/

I would like to default project name to a env var like this:

library(bigrquery)
con <-  dbConnect(
  bigquery(),
  project = "${1:Project=Sys.getenv('BIGQUERY_PROJECT')}"
)

Unfortunately this does not work, as Sys.getenv('BIGQUERY_PROJECT') is not evaluated.

UPDATE e.g. for @Jozef's example I get the following code generated:

library(bigrquery)
con <-  dbConnect(
  bigquery(),
  project = "`r eval({Sys.getenv('BIGQUERY_PROJECT'))`"
)

UPDDATE 2

This sort of works, but does not look good:

library(bigrquery)
con <-  dbConnect(
  bigquery(),
  project = paste0("${1:Project=", Sys.getenv("BIGQUERY_PROJECT"), "}")
)
Bulat
  • 6,869
  • 1
  • 29
  • 52

1 Answers1

1

Not sure if this is exactly what you want, but you can use r eval({ }) in back-ticks if I understand your purpose correctly.

Example with the 'USER' environment variable:

snippet envvar
    library(bigrquery)
    con <-  dbConnect(
        bigquery(),
        project = "${1:Project=`r eval({Sys.getenv('USER')})`}"
    )

Will do this for my user (jozef) when triggered:

enter image description here

Also, I wrote a post with more examples of advanced snippet use.

Jozef
  • 2,617
  • 14
  • 19