title: Script with utf-8 text runs differently from RStudio and command line in Windows
body: <p>I'm working with files containing text in Hindi and parsing them. I wrote my code in Rstudio and executed it without many issues. But now, I need to execute the same script from command line using R.exe/Rscript.exe and it doesn't work the same way. I've run a simple script from both RStudio and the terminal:</p>
  
  <pre><code>n_p&lt;-'नाम'
  
  Encoding(n_p)
  
  gregexpr(n_p,c('adfdafc','नाम adsfdfa'))
  sessionInfo()
  </code></pre>
  
  <p>Output In RStudio:</p>
  
  <pre><code>&gt; n_p&lt;-'नाम'
  &gt; 
  &gt; Encoding(n_p)
  [1] "UTF-8"
  &gt; 
  &gt; gregexpr(n_p,c('adfdafc','नाम adsfdfa'))
  [[1]]
  [1] -1
  attr(,"match.length")
  [1] -1
  
  [[2]]
  [1] 1
  attr(,"match.length")
  [1] 3
  
  &gt; sessionInfo()
  R version 3.5.0 (2018-04-23)
  Platform: x86_64-w64-mingw32/x64 (64-bit)
  Running under: Windows 7 x64 (build 7600)
  
  Matrix products: default
  
  locale:
  [1] LC_COLLATE=English_India.1252  LC_CTYPE=English_India.1252   
  [3] LC_MONETARY=English_India.1252 LC_NUMERIC=C                  
  [5] LC_TIME=English_India.1252    
  
  attached base packages:
  [1] stats     graphics  grDevices utils     datasets  methods   base     
  
  other attached packages:
  [1] rJava_0.9-10
  
  loaded via a namespace (and not attached):
  [1] compiler_3.5.0 tools_3.5.0   
  </code></pre>
  
  <p>Output with R.exe in cmd (For debugging purposes. Rscript.exe gives a similar if not identical output)</p>
  
  <pre><code>&gt; n_p&lt;-'à☼"à☼_à☼r'
  &gt;
  &gt; Encoding(n_p)
  [1] "latin1"
  &gt;
  &gt; gregexpr(n_p,c('adfdafc','à☼"à☼_à☼r adsfdfa'))
  [[1]]
  [1] -1
  attr(,"match.length")
  [1] -1
  
  [[2]]
  [1] 1
  attr(,"match.length")
  [1] 9
  
  &gt; sessionInfo()
  R version 3.5.0 (2018-04-23)
  Platform: x86_64-w64-mingw32/x64 (64-bit)
  Running under: Windows 7 x64 (build 7600)
  
  Matrix products: default
  
  locale:
  [1] LC_COLLATE=English_India.1252  LC_CTYPE=English_India.1252
  [3] LC_MONETARY=English_India.1252 LC_NUMERIC=C
  [5] LC_TIME=English_India.1252
  
  attached base packages:
  [1] stats     graphics  grDevices utils     datasets  methods   base
  
  loaded via a namespace (and not attached):
  [1] compiler_3.5.0
  </code></pre>
  
  <p>I've tried changing locales but <code>Sys.setlocale</code> refuses to work properly. In some cases, <code>gregexpr</code> gives an error when it can't parse non ASCII code. And finally, when it does run without errors, it doesn't match regular expressions properly. I can't provide a reproducible example at the moment, but I will try to later.</p>
  
  <p>Help.</p>