Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

with_shiny

Turn a googleAuthR data fetch function into a Shiny compatible one


Description

Turn a googleAuthR data fetch function into a Shiny compatible one

Usage

with_shiny(f, shiny_access_token = NULL, ...)

Arguments

f

A function generated by googleAuth_fetch_generator.

shiny_access_token

A reactive object that resolves to a token.

...

Other arguments passed to f.

Value

the function f with an extra parameter, shiny_access_token=NULL.

Examples

## Not run: 
## in global.R

## create the API call function, example with goo.gl URL shortner
library(googleAuthR)
options("googleAuthR.scopes.selected" = c("https://www.googleapis.com/auth/urlshortener"))

shorten_url <- function(url){

  body = list(
    longUrl = url
 )
 
 f <- gar_api_generator("https://www.googleapis.com/urlshortener/v1/url",
                        "POST",
                        data_parse_function = function(x) x$id)
                        
 f(the_body = body)
 
 }


## in server.R
library(shiny)
library(googleAuthR)
source('global.R')

shinyServer(function(input, output, session)){
  
  ## Get auth code from return URL
  access_token  <- reactiveAccessToken(session)

  ## Make a loginButton to display using loginOutput
  output$loginButton <- renderLogin(session, access_token())

  short_url_output <- eventReactive(input$submit, {
  ## wrap existing function with_shiny
  ## pass the reactive token in shiny_access_token
  ## pass other named arguments
    short_url <- with_shiny(f = shorten_url, 
                           shiny_access_token = access_token(),
                           url=input$url)
                           
   })
   
   output$short_url <- renderText({
   
     short_url_output()
     
   })
 }

## in ui.R
library(shiny)
library(googleAuthR)

shinyUI(
  fluidPage(
    loginOutput("loginButton"),
    textInput("url", "Enter URL"),
    actionButton("submit", "Shorten URL"),
    textOutput("short_url")
    ))

## End(Not run)

googleAuthR

Authenticate and Create Google APIs

v1.4.0
MIT + file LICENSE
Authors
Mark Edmondson [aut, cre] (<https://orcid.org/0000-0002-8434-3881>), Jennifer Bryan [ctb], Johann deBoer [ctb], Neal Richardson [ctb], David Kulp [ctb], Joe Cheng [ctb]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.