У меня есть хранимая процедура, где я выполняю сценарий R и возвращаю фрейм данных в качестве вывода.Процедура запрашивает связанный сервер, выполняет множество преобразований в результате исходного запроса и возвращает результирующий фрейм данных.
Вот подробности о моей среде:
Product: Microsoft SQL Server Enterprise (64-bit)
Operating System: Microsoft Windows NT 6.3 (9600)
Platform: NT x64
Version: 13.0.5216.0
Сценарий в хранимой процедуре выглядит следующим образом:
EXECUTE sp_execute_external_script
@language = N'R',
@script = N'
library(jsonlite)
library(purrr)
library(tidyr)
library(dplyr)
library(lubridate)
##store initial query as a data frame
goldInstrumentDF <- data.frame(InputDataSet)
##set load_ts as a timestamp
formattedDF <- transform(goldInstrumentDF, load_ts = ymd_hms(as.character(goldInstrumentDF$load_ts)))
##set all other column values as characters
i <- sapply(formattedDF, is.factor)
formattedDF[i] <- lapply(formattedDF[i], as.character)
##unpack json object
firstTransform <- formattedDF %>%
mutate(event = map(event, ~ fromJSON(.) %>% as.data.frame())) %>%
unnest(event)
##store load events in a data frame
loadEvents <- firstTransform[firstTransform$object.method == "IMM_Equipment_RePro_Load_Event", ]
##store equipment events in a data frame
equipmentEvents <- firstTransform[firstTransform$object.method != "IMM_Equipment_RePro_Load_Event", ]
##parse the initial characters from the non-standard data exchange format
equipmentSubstring <- equipmentEvents %>% mutate(object.object = substring(equipmentEvents$object.object, 19))
##remove the curly bracked from the end of the non-standard data exchange format
equipmentSubstring2 <- equipmentSubstring %>% mutate(object.object = gsub(''.$'', '''', equipmentSubstring$object.object))
##remove the single quotes from the non-standard data exchange format
equipmentSubstring3 <- equipmentSubstring2 %>% mutate(object.object = gsub("''", "", equipmentSubstring2$object.object))
##split the data from the non-standard data exchange format into a header and a value
namev<-function(x) {
a<-strsplit(x,"=")
setNames(sapply(a,''['',2), sapply(a,''['',1))
}
##turn each row into a named vector
secondTransform <- lapply(strsplit(equipmentSubstring3$object.object, ","), namev)
##find list of all column names
thirdTransform <- unique(unlist(sapply(secondTransform, names)))
##extract data from all rows for every column
fourthTransform <-do.call(rbind, lapply(secondTransform, ''['', thirdTransform))
##rejoin with original data
fifthTransform <-cbind(equipmentSubstring3[,-25], fourthTransform)
##remove exraneous columns
drops <- c(" error", "object.object", NA)
sixthTransform <- fifthTransform[ , !(names(fifthTransform) %in% drops)]
##output the data frame
OutputDataSet <- as.data.frame(sixthTransform)',
@input_data_1 = N'SELECT * FROM openquery(KMhivehttp, ''select * from dmfwk_gold.instrumentapps_event;'');
Есть ли способ для меня динамически определитьпредложение WITH RESULTS SET на основе данных в моем фрейме данных?