Search code examples
sql-serverrmicrosoft-r

SQL Server R Services error invalid input in 'utf8towcs'


Trying to run an R procedure in-database with Microsoft SQL Server R Services via sp_execute_external_script, but I receive this error even if I reduce my R script to do nothing but return an empty data frame.

A 'R' script error occurred during execution of '' 
Msg 39019, Level 16, State 1, Line 2
An external script error occurred: 
Error in sort.list(y) : invalid input 'BiototÆs spots' in 'utf8towcs'
Calls: as.data.frame ... as.data.frame -> as.data.frame.character -> factor -> sort.list

Error in ScaleR.  Check the output for more information.
Error in eval(expr, envir, enclos) : 
  Error in ScaleR.  Check the output for more information.
Calls: source -> withVisible -> eval -> eval -> .Call
Execution halted

The error appears when special characters are included in the results of the input query in a column of type varchar, but I cannot use to the typical R tricks to re-encode character data during the data frame creation because the transformation of the @input_data_1 query result to the InputDataSet data frame occurs outside of the control of the procedure.


Solution

  • Convert the string data to unicode before it is passed to R. You can do this by casting the varchar columns as nvarchar in the input query to match the expected encoding for input to the data frame conversion.

    SELECT CAST(ColA AS nvarchar(255)) FROM TableA