Quantcast
Channel: Power Query topics
Viewing all articles
Browse latest Browse all 31082

Problem Integrating Azure ML and Power BI with R script

$
0
0

Hey guys,

 

I've been trying to integrate my Azure ML web service with Power BI using an R script, but with no success.

I have already read Justyna's post and watched the Webinar on this topic, but I am pretty sure there is something missing on their codes. 

 

 

My R Script is as follows:

 

 

# 'dataset' contém os dados de entrada neste script
library("RCurl")
library("rjson")

createList <- function(dataset)
{
  temp <- apply(dataset, 1, function(x) as.vector(paste(x, sep = "")))
  colnames(temp) <- NULL
  temp <- apply(temp, 2, function(x) as.list(x))
  return(temp)
}

# Accept SSL certificates issued by public Certificate Authorities
options(RCurlOptions = list(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl")))

h = basicTextGatherer()
hdr = basicHeaderGatherer()

req = list(
  Inputs = list(
    "input1" = list(
      "ColumnNames" = list("ID", "Churn", "Estado", "LigacoesSuporte", "VoiceMail", "MensagensVoiceMail", "PlanoInternacional", "TotalLigacoesDia", "TotalCustoDia", "TotalLigacoesNoite", "TotalCustoNoite", "TotalLigacoesIntern", "TotalCustoIntern"),
      "Values" = createList(dataset)
    )                ),
  GlobalParameters = setNames(fromJSON('{}'), character(0))
)

body = enc2utf8(toJSON(req))
api_key = "XXXXXXX" # Replace this with the API key for the web service
authz_hdr = paste('Bearer', api_key, sep=' ')

h$reset()

curlPerform(url = "https://ussouthcentral.services.azureml.net/workspaces/XXXXX/services/XXXXXX/execute?api-version=2.0&details=true",
httpheader=c('Content-Type' = "application/json", 'Authorization' = authz_hdr),
postfields=body,
writefunction = h$update,
headerfunction = hdr$update,
verbose = TRUE
)

headers = hdr$value()
httpStatus = headers["status"]
if (httpStatus >= 400)
{
print(paste("The request failed with status code:", httpStatus, sep=" "))

# Print the headers - they include the requert ID and the timestamp, which are useful for debugging the failure
print(headers)
}

result = h$value()
finalResult <- fromJSON(result)
#Return results inter <- do.call("rbind", as.list(finalResult$Results$output1$value$Values)) MyFinalResults <- data.frame(inter) names(MyFinalResults) <- finalResult$Results$output1$value$ColumnNames rm(list=setdiff(ls(), "MyFinalResults"))

 

I tried changing a lot of things in the code, but I always get an empty Table for MyFinalResults.

Has anyone ever did this with R Script?

 

I bet there is something wrong with 

inter <- do.call("rbind", as.list(finalResult$Results$output1$value$Values))

 

I tried first without the funciont as.list() as they said, but it gives an error because the second argument must be a list.

And when I test this on RStudio, it returns a Null value. 


Anyway, would be glad if anyone help me.

Thanks

 


Viewing all articles
Browse latest Browse all 31082

Trending Articles