4

more a tip question that can save lots of time in many cases. I have a script.R file which I try to save and get the error:

Not all of the characters in ~/folder/script.R could be encoded using ASCII. To save using a different encoding, choose "File | Save with Encoding..." from the main menu.

I was working on this file for months and today I was editing like crazy my code and got this error for the first time, so obviously I inserted a character that can not be encoded while I was working today.

My question is, can I track and find this specific character and where exactly in the document is?

There are about 1000 lines in my code and it's almost impossible to manually search it.

Kwnwps
  • 353
  • 1
  • 4
  • 15
  • 1
    While it’s possible that you accidentally included a rogue character somewhere, *in general* you shouldn’t be using ASCII anyway. It’s an old encoding. There’s no good reason to ever use it today (in day to day life; there may be very specific edge cases). In particular, text/code filels should almost always be encoded as UTF-8. I strongly suggest doing this. – Konrad Rudolph Sep 16 '15 at 14:04
  • Thanks @KonradRudolph. I'll follow that advise. I was working in Rstudio with everything on default and never paid attention to that. – Kwnwps Sep 16 '15 at 14:07

3 Answers3

5

Use tools::showNonASCIIfile() to spot the non-ascii.

Marcin
  • 7,834
  • 8
  • 52
  • 99
  • Thanks, I tried tools::showNonASCIIfile('script.R') and I get nothing `character(0)` . Could it be possible with that worning that I get? – Kwnwps Sep 16 '15 at 14:10
  • 1
    I suggest you follow the instructions `To save using a different encoding, choose "File | Save with Encoding..." from the main menu.` and choose `UTF-8` encoding – Marcin Sep 16 '15 at 14:20
  • I did that, thanks. I'm just worried because obviously I have something in my code that it should not be there, so I need to locate it. – Kwnwps Sep 16 '15 at 14:25
  • 1
    Ok finally it worked perfectly. I found the character. Thanks @Marcin – Kwnwps Sep 16 '15 at 15:46
  • With RStudio on Windows, if I get the warning "Not all of the characters in ~/R/Projects/neuralnets.Rnw could be encoded using ISO8859-1. To save using a different encoding, choose "File | Save with Encoding..." from the main menu." is there a counterpart to the tools::showNonASCIIfile() function to locate the wayward character(s)? – lawyeR Oct 31 '18 at 13:01
2

Let me suggest two slight improvements this.

Process:

  1. Save your file using a different encoding (eg UTF-8)

  2. set a variable 'f' to the name of that file. something like this f <- yourpath\\yourfile.R

  3. Then use tools::showNonASCIIfile(f) to display the faulty characters.

Something to check:

I have a Markdown file which I run to output to Word document (not important). Some of the packages I used to initialise overload previous functions. I have found that the warning messages sometimes have nonASCII characters and this seems to have caused this message for me - some fault put all that output at the end of the file and I had to delete it anyway!

  1. Check where characters are coming back from Warnings!

Cheers

CJCoop
  • 31
  • 2
1

Expanding the accepted answer with this answer to another question, to check for offending characters in the script currently open in RStudio, you can use this:

tools::showNonASCIIfile(rstudioapi::getSourceEditorContext()$path)
PhiS
  • 4,540
  • 25
  • 35