Pablo Boswell
Pablo Boswell

Reputation: 845

Microsoft R Server Parsing errors

It is really lame that you can't just copy/paste code from RStudio to a SQL Server stored procedure seamlessly, but alas, it wouldn't be Microsoft otherwise, right?

Basically, I get really strange errors in parsing:

Error in source(revoScriptConnection) : 
  revoScriptConnection:116:111: unexpected INCOMPLETE_STRING
116: dtCumulativeResults[,c("State_Name_RAW","City_Name_MDM","County_Name_MDM","State_Name_MDM","City_Address_MDM","City_

Here is the actual code line (with a few lines prior included):

    }


 }

##save dud results if no geocoding occurred
dtCumulativeResults <- data.table(City_Name_RAW=NA)
dtCumulativeResults[,c("State_Name_RAW","City_Name_MDM","County_Name_MDM","State_Name_MDM","City_Address_MDM","City_Latitude","City_Longitude","index"):=character()]

The only lines preceding this are the ending brackets for a for loop and if statement.

This code runs just fine in RStudio. Unfortunately, if I just isolate these few lines, the SPROC runs. So it has to do with some lines above, but I couldn't possibly know where to start. One thing I do know: it's having problems parsing out braces { or }. If I don't indent the braces it throws errors. But not always.

Anyway, I was hoping someone had realized what the silver bullet is. Perhaps keep braces on one line. Etc. I am adding the full SPROC below, but again, this is some weird formatting issue.

USE master
GO

/****** Object:  StoredProcedure [dbo].[Rscript_geocodeUSACities]    Script Date: 8/8/2017 10:31:15 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



























CREATE PROCEDURE [dbo].[Rscript_geocodeUSACities]
    @usrOutputFilePath varchar(150)
    ,@usrOutputFileName varchar(150)

AS
BEGIN

    SET NOCOUNT ON;

DECLARE @rScript nvarchar(max) = N'

#### USER INPUTS ####

usrOutputFile <- "' + @usrOutputFilePath + @usrOutputFileName + '"


#### ESTABLISH ENVIRONMENT ####

library(data.table)
library(foreach)
library(XML)
library(RCurl)
library(RJSONIO)

##turn off scientific notation
options(scipen=999)


#### GEOCODE ####

##assign input data set
dtInputData <- data.table(InputDataSet)

##initialize cumulative results data table
dtCumulativeResults <- dtInputData[0,]

##find out where to start
startindex <- 1

##if a temp file exists - load it up and count the rows
if (file.exists(usrOutputFile)) {

    print("Found temp file - resuming from index")
    dtCumulativeResults <- fread(usrOutputFile,colClasses="character")
    startindex <- ifelse(is.na(max(as.numeric(dtCumulativeResults$index),na.rm=T)),1,max(as.numeric(dtCumulativeResults$index),na.rm=T)+1)
    print(startindex)


} else { print("No temp file") }



if (startindex<nrow(dtInputData) | startindex==nrow(dtInputData)) {

    for (ii in seq(startindex,nrow(dtInputData))) {

    ##initialize temporary results data table
    dtTempResults <- dtInputData[ii,]

    ##geocode address
    vcSearchInput <- paste0(gsub("[[:punct:]]","",unlist(strsplit(paste(dtInputData$City_Name_RAW[ii],dtInputData$State_Name_RAW[ii],"USA")," "))),collapse="+")
    vcSearchURL <- URLencode(paste("http://maps.google.com/maps/api/geocode/json?address=",vcSearchInput,"&sensor=false",sep=""))
    vcSearchOutput <- getURL(vcSearchURL)
    vcSearchOutput <- fromJSON(vcSearchOutput,simplify=F)

    if (vcSearchOutput$status=="OVER_QUERY_LIMIT") {

    dtTempResults[1,c("City_Name_MDM","County_Name_MDM","State_Name_MDM","City_Address_MDM","City_Latitude","City_Longitude","index"):=NA]

    #save temporary results as we are going along
    dtCumulativeResults <- rbind(dtCumulativeResults,dtTempResults[0,],fill=T)
    write.csv(dtCumulativeResults,usrOutputFile,row.names=F)
    break

     }

    if (vcSearchOutput$status=="OK" & !(tolower(dtTempResults$City_Name_RAW) %in% tolower(state.name)) & !(tolower(dtTempResults$City_Name_RAW) %in% tolower(state.abb)) & !(tolower(vcSearchOutput$results[[1]]$address_components[[1]]$long_name) %in% tolower(state.name))) {

    dtTempResults[1,City_Name_MDM:=vcSearchOutput$results[[1]]$address_components[[1]]$long_name]

    if (regexpr("County|Parish|Borough",vcSearchOutput$results[[1]]$address_components[[2]]$long_name)>0) {

    dtTempResults[1,County_Name_MDM:=gsub(" County","",vcSearchOutput$results[[1]]$address_components[[2]]$long_name)]
    dtTempResults[1,State_Name_MDM:=vcSearchOutput$results[[1]]$address_components[[3]]$long_name]

    } else if (regexpr("United States",vcSearchOutput$results[[1]]$address_components[[3]]$long_name)>0) {

    dtTempResults[1,County_Name_MDM:=vcSearchOutput$results[[1]]$address_components[[1]]$long_name]
    dtTempResults[1,State_Name_MDM:=vcSearchOutput$results[[1]]$address_components[[2]]$long_name]


    } else {

    dtTempResults[1,County_Name_MDM:=gsub(" County","",vcSearchOutput$results[[1]]$address_components[[3]]$long_name)]
    dtTempResults[1,State_Name_MDM:=vcSearchOutput$results[[1]]$address_components[[4]]$long_name]

     }

    dtTempResults[1,City_Address_MDM:=vcSearchOutput$results[[1]]$formatted_address]
    dtTempResults[1,City_Latitude:=vcSearchOutput$results[[1]]$geometry$location$lat]
    dtTempResults[1,City_Longitude:=vcSearchOutput$results[[1]]$geometry$location$lng]
    dtTempResults[1,index:=ii]

     }

    ##save temporary results as we are going along
    dtCumulativeResults <- rbind(dtCumulativeResults,dtTempResults,fill=T)
    write.csv(dtCumulativeResults,usrOutputFile,row.names=F)

    }

} else {

    ##save dud results if no geocoding occurred
    dtCumulativeResults <- data.table(City_Name_RAW=NA)
    dtCumulativeResults[,c("State_Name_RAW","City_Name_MDM","County_Name_MDM","State_Name_MDM","City_Address_MDM","City_Latitude","City_Longitude","index"):=character()]
    write.csv(dtCumulativeResults[0,],usrOutputFile,row.names=F)

    }

'

EXECUTE  sp_execute_external_script
                @language = N'R'
              , @script = @rScript
              ,@input_data_1 =N'select distinct [City_Name_RAW]
                                    ,[State_Name_RAW]
                                from [External].[mdm].[dim_USA_Cities_Map]
                                where Mod_DT is null'
              ;

END


























GO

Upvotes: 1

Views: 173

Answers (1)

David Browne - Microsoft
David Browne - Microsoft

Reputation: 89051

Note that the bulk of your script is specified as a non-Unicode string literal. Everyting after

usrOutputFile <- "' + @usrOutputFilePath + @usrOutputFileName + '"  . . .

Because after the '+' you start the next string literal without N' . Should be

usrOutputFile <- "' + @usrOutputFilePath + @usrOutputFileName + N'"  . . .

To insert the results to a table, in TSQL use INSERT ... EXEC . See https://learn.microsoft.com/en-us/sql/t-sql/statements/insert-transact-sql

Upvotes: 2

Related Questions