Sion Lee
Sion Lee

Reputation: 21

Mixed up column order, DBI::dbWriteTable used in R for BigQuery

When executing the below code in R to create new table, order of columns is mixed up for the table created within BigQuery.

Code:

library(DBI)
    
con <- dbConnect(
      bigrquery::bigquery(),
      project = "project_name",
      dataset = "dataset_name",
      billing = billing
    )


DBI::dbWriteTable(conn = con,
                  name = "table_name",
                  value = dataframe
                  )

Expected Output:

model Seg1 Seg2 Seg3 max
MDE12, 1, 7, 2, 7
KDV02, 3, 8, 3, 8
LKD21, 0, 9, 1, 9 

Actual Output:

max Seg3 Seg1 model Seg2
7, 2, 1, MDE12, 7
8, 3, 2, KDV02, 8
9, 1, 0, LKD21, 9

Please help me wise man of this planet earth.

Thanks ahead :)

Upvotes: 2

Views: 415

Answers (1)

abalter
abalter

Reputation: 10383

The function bigrquery::dbWriteTable does not have an argument to specify column order. However, the comment on this post

Maintain column order when uploading an R data frame to Big Query

gives a solution for using bigrquery methods. I've tested it and it "works."

The comment:

Have you tried providing a list of fields to use? For example, bq_table_upload("project.dataset.table_name", df_name, fields=df_name). When you provide fields argument as a dataframe, the table schema is generated from it and used in the upload request.

Upvotes: 1

Related Questions