nFrain
nFrain

Reputation: 149

How do I write to a bigquery table from shiny?

I'm trying to write a shiny app that takes a file as an input and uploads the data in that file to a bigquery table where some other stuff will go on. Everything appears to be working fine in terms of getting the data into my app, but when I try to upload the data to bigquery, nothing happens. No error messages, just nothing.

I can run the code on its own and it executes just fine. I'm having a little trouble figuring out how to create a reproducible example because you can't write to a public dataset, but I've included my code below.

Additional info:

Please let me know if there's something I can add to make this question easier to answer. Thanks.

############# UI ############
#

library(shiny)

shinyUI(fluidPage(

  # Application title
  titlePanel("Upload"),

  # Sidebar with a slider input for number of bins
  sidebarLayout(
    sidebarPanel(
      fileInput('list', 'Choose file to upload',
                accept = c(
                  'text/csv',
                  'text/comma-separated-values',
                  '.csv'
                )),
      tags$hr(),
      textInput('sql', 'Or give a query to get the customer_ids you want'),
      tags$hr(),
      actionButton('go', 'Go')
    ),

    # Show a plot of the generated distribution
    mainPanel(
      tableOutput('log')
    )
  )
))


############# server ##############

### setting up the environment
library(shiny)
library(data.table)
library(bigrquery)

### setting up the constants
project <- 'xxxxxxx'
dest_dataset <- 'temp'
dest_table <- 'custs_hash'
cd <- 'CREATE_IF_NEEDED'
wd <- 'WRITE_TRUNCATE'

options(shiny.maxRequestSize = 100*1024^2)


shinyServer(function(input, output) {

  logs <- eventReactive(input$go, {
    inFile <- input$list
    dat <- fread(inFile$datapath)
    dat <- head(dat)
    return(list(dat = dat))
  })

  upload <- eventReactive(input$go, {
    data <- dat()$dat
    ins <- insert_upload_job(project, dataset = dest_dataset, table = dest_table, values = data,
                            create_disposition = cd, write_disposition = wd)
    return(list(ins = ins))
  })

  output$log <- renderTable(logs()$dat)

})

Upvotes: 0

Views: 316

Answers (1)

Xiongbing Jin
Xiongbing Jin

Reputation: 12107

An eventReactive returns a reactive expression object. Like other reactive objects, you need to expressly call it like a function. Otherwise it won't run by itself.

So in your case, you have upload <- eventReactive(...), then you need to call it using upload().

Upvotes: 0

Related Questions