我尝试在SQL Server R中使用 sp_execute_external_script。 命令,但它未能创建png图象。
DECLARE @stateName nvarchar(50) = 'Michigan'
EXEC sp_execute_external_script
@language = N'R',
@script = N'
covidWeeklyDataSet <- InputDataSet
# set up report file for chart
reportfile <- "C:\\temp\\Covid19-Weekly.png"
png(file = reportfile)
plot(x = covidWeeklyDataSet[, 1], y = covidWeeklyDataSet[, 2],
main = paste(state_name, "Weekly Covid 19 Counts", sep = ""),
col = 3, ylab = "Cases", xlab = "Dates", ylim = c(0, 35000))
par(new = TRUE)
plot(x = covidWeeklyDataSet[, 1], y = covidWeeklyDataSet[, 3],
col = 2, ylab = "Cases", xlab = "Dates", ylim = c(0, 35000))
dev.off()
',
@input_data_1 = N'SELECT [date], cases, deaths FROM #weekly',
@params = N'@state_name nvarchar(20)',
@state_name = @stateName
错误信息如下:Msg 39004, Level 16, State 20, Line 13
Msg 39004, Level 16, State 20, Line 13 在执行 “sp_execute_external_script “期间发生一个 “R “脚本错误,HRESULT 0x80004004。Msg 39019, Level 16, State 2, Line 13 发生一个外部脚本错误。 png(file = reportfile)中出错:无法启动png()设备调用:source -> withVisible -> eval -> eval -> png此外。警告信息。1: In png(file = reportfile) : 无法打开文件’C:\temp\Covid19-Weekly.png’进行写入 2: In png(file = reportfile) : 打开设备失败。
执行中出错。 检查输出以获取更多信息。eval(ei, envir)出错:执行中出错。 查看输出结果以了解更多信息。调用:runScriptFile -> source -> withVisible -> eval -> eval -> .Call 执行停止。
作为管理员也会失败。 请大家帮忙。
解决方案:
READ & WRITE permissions for c:\temp to “ALL APPLICATION PACKAGES”.
EXEC sp_execute_external_script
@language = N'R',
@script = N'
#file.create("c:\\temp\\mytest.png")
png(filename = "c:\\temp\\mytest.png",
width = 500, height = 500, units = "px", pointsize = 12,
bg = "white", res = NA)
x <- sample(c("A","B","C","D"), 20, replace=TRUE)
plot(table(x))
dev.off()'