-3

Maybe related: Stack overflow: Windoes does not support UTF-8

I have a script which I can source from Rstudio, but when I try to source it from Rgui.exe or try to BATCH CMD run, I get the following error in my Rout file:

Error in easy_clean$Sv_Karakter: $ operator is invalid for atomic vectors

The reason is that the database table I am quering have a latin charachter 'ø' in its name (se third line below). So the result of my query is this (as per str(easy)):

"";"x"
"1";"42000 102 [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near '¸'."
"2";"[RODBC] ERROR: Could not SQLExecDirect 'select *
  from PrøveDeltager a
  left outer join Aftaler b
  ON b.Cpr = a.CPR 
  LEFT OUTER JOIN GODK c
  ON c.GODK_ID = b.GODK_ID
  where a.slut >= '20140808'
  AND a.slut <='20140818'
  AND a.Branche = 'vvs'
  AND a.SaleID is not null
  AND a.CPR in (select x.CPRNR from Statistik x)
  order by Sv_Karakter'"

In rstudio the query works.

Sys.getlocale('LC_CTYPE')returns Danish_Denmark.1252 in both R.gui and Rstudio - so I don't know how to fix this.

I did find this link to developer.r-project which discus windows locales (quite old though).

For now I have created a database view without the 'ø' - that view I can call without problems from R.


From sessionInfo I can say that:

Rstudio R is 64 bit, and R.exe is 32 bit.

Other than that, the only difference is this, for Rstudio:

loaded via a namespace (and not attached): 1 tools_3.1.0


Since I can't write my database credentials, I can't create a reproducible example. But here is the script. http://pastebin.com/XwdZPhL7

Andreas
  • 6,612
  • 14
  • 59
  • 69
  • Do you have Rstudio set up to automatically load certain packages for you? Perhaps compare the `sessionInfo` from both methods. Also, it would be best to give a fully [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) otherwise we're really just guessing what might be wrong. – MrFlick Aug 17 '14 at 19:29
  • Thanks @MrFlick - I thought this error might be common, so didn't bother with a reproducible example (because the script calls a SQL server, and I can't divulge the credentials for that). – Andreas Aug 17 '14 at 19:43
  • 3
    It doesn't have to be the actual script you run, you just need to create a minimal example that reproduces the problem. What if you take out the SQL stuff. Do you get the same error? – MrFlick Aug 17 '14 at 19:44
  • But if I take out the SQL stuff I will not know if data is the same as in the real script. I can create what I think is a tangential situation, but that runs with no problem in batchmode. - So I think its my ODBC call thats the problem. But I can't figure out which. – Andreas Aug 17 '14 at 20:10
  • Can we see `str(easy)`? – Roman Luštrik Aug 17 '14 at 21:00
  • Yes, from an interactive session where everything works... I'll update the question. – Andreas Aug 17 '14 at 21:02
  • HEre is a pastebin ofthe script http://pastebin.com/XwdZPhL7 – Andreas Aug 17 '14 at 21:10
  • 1
    I think the problem is with the database connection and encoding of the query string, as per updated question. – Andreas Aug 18 '14 at 12:43

1 Answers1

0

Two ways I can imagine that would yield an error with that code: one is if this results in a single column:

 easy[!is.na(easy$Sv_Karakter),]

In that case, the result would be a vector because of the default action of the "[" function to create atomic vectors from single column results. The attempts to extract that column would fail with that error. The other case of failure (but perhaps not that error) would be when there was no 'Sv_Karakter' column in the 'easy' dataframe.

Better efforts at documentation by offering str(easy) or dput(head(easy)) are needed.


Your query returned an error message and it was in the form of an R vector. So that explains the particular error. You now need to figure out how your db connection is getting messed up, as Andreas was saying.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • Post this as an edit to your question. It's impossible to read as it is. Then delte the comment. – IRTFM Aug 18 '14 at 21:32