0

I am writing some functions on sublime text which I want to use on jupyter notebook or Rstudio. the function works fine on Sublime but the output from it in jupyter or Rstudio is not good.

the function:

normalize <- function(str, tobereplaced = c('à','â','ä'), replacements = c('a','a','a') ){
        for (i in 1:length(tobereplaced)){
            str <- gsub( tobereplaced[i], replacements[i], str)
        }
        return(str)
    }

When executing :

normalize("àâä")

output sublime :

Warning message:
Warning messages:
[1] "aaa"
[Finished in 0.6s]

output jupyter & Rstudio :

'àâä'

Can someone please help ? (I imported the R file containing the function with source())

Edit : The problem occurs only when importing the R file containing the function. When I define the function locally it does work fine.

Thami BNG
  • 1
  • 2

2 Answers2

0
> normalize <- function(str, tobereplaced = c('à','â','ä'), replacements = c('a','a','a') ){
+   for (i in 1:length(tobereplaced)){
+     str <- gsub( tobereplaced[i], replacements[i], str)
+   }
+   return(str)
+ }
> normalize("àâä")
[1] "aaa"
> 

I ran your code in Rstudio version 1.0.153 it working fine ...

kcm
  • 200
  • 3
  • 13
  • The problem occurs only when I import the function into Rstudio or Jupyter, if defined locally it does work fine for me as well – Thami BNG Sep 04 '17 at 10:04
0

Solved: The problem was due to the encoding from the jupyter notebook.

one way to solve this is to do the following:

source("file.R", encoding = "UTF-8" )

Thanks everyone for the help

Thami BNG
  • 1
  • 2