问题描述
我有一个 R 脚本,它下载一个包含 Qualtrics 调查 ID 的表,然后尝试根据这些 ID 动态下载调查并编写 sql 查询以将数据写入 teradata。问题是我以 JSON 格式提供这些数据,所以我在 R 中使用了 qualtRics、jsonlite 和 RODBC 包。
survey_table:
survey_name | survey_id |
---|---|
调查一 | sid_abcdef |
调查二 | sid_ghijkl |
sample_survey:
response_id | 问题1 | 问题 2 |
---|---|---|
abcd1234 | 3 | 2 |
abcd5678 | 5 | 2 |
#pull in the survey data into a list of dataframes this works exactly as I want it to,#it pulls a list of surveys from our Qualtrics token,# and loads the surveys into dataframes based on the ID's:
library('dbplyr')
library('qualtRics')
library("RODBC")
library("jsonlite")
cnxn__ <- dbConnect(odbc::odbc(),'ODBC_DRIVER_NAME',database = 'teraDATA DATABASE NAME')
survey_table <- all_surveys()
for (i in 1:nrow(survey_table))
{assign(survey_table$name[i],value = fetch_survey(survey_table$id[i])) }
#This is where I'm having trouble:
#Create a function that takes the survey ID lists Metadata,and write code to process the
#the data in JSON format into teradata:
WRITE_FUNCTION <- function(id,name ){
for (i in 1:nrow( ls() )
{
dq <- '"'
#Format the dataframes into JSON to input into teradata
data_input <- escape_ansi(as.character(toJSON(id[i,],null = 'null',na = 'null')),parens = NA,collapse = "")
#Develop a dynamic query that inserts the data based on which dataframe in the environment it's touching:
query <- paste("INSERT INTO ",dq,name[i]," (id,",colnames(data_input),") VALUES (",i,data_input,")",sep = '')
#Push the sql to teradata and run
dbGetQuery(cnxn__,query)
}}
lapply(survey_table,WRITE_FUNCTION)
我知道我遇到的问题是上面粗体的“查询”别名,但也可能与我如何循环 JSON 和/或调用 LAPPLY() 函数有关。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)