switch() returns NULL in shiny on unicode characters coming from variable input only

208 views Asked by At

When I use switch inside my shiny-server function on unicode characters, I don't get the expected behavior :

library(shiny)
app<-shinyApp(
        ui=shinyUI(fluidPage(
                selectInput("variable",label = NULL,choices = c( "a","b","é"), selected = "é"),
                mainPanel(plotOutput("text"))
        )), 
        server=function(input, output) {
                output$text<-renderText({
                        print(switch(input$variable,
                                     "a"="A",
                                     "b"="B",
                                     "é"="E"))
                })
        })
runApp(app)

Will print "A" in the console for an input equals to "a" but NULL in the console for input equals to "é".

On the other hand :

x<-"é"
switch(x,
          "a"="A",
          "b"="B",
          "é"="E")

returns "E", just as I expected it.

I tried to replace the first argument of the switch call input$variable by "é" only, it does work like that. I guess input$variable is not exactly what I think it is, but coercing to a character string with as.character(input$variable) does not work either.

If I use print(input$variable) it comes up with [1] "é" which is all fine. This is beyond my current understanding...

I really need to display unicode characters and translate them in non-unicode. Any help would be greatly appreciated :-)

By the way sessionInfo() returns :

R version 3.1.3 (2015-03-09)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=French_France.1252  LC_CTYPE=French_France.1252    LC_MONETARY=French_France.1252
[4] LC_NUMERIC=C                   LC_TIME=French_France.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] shiny_0.12.0

loaded via a namespace (and not attached):
 [1] digest_0.6.8     htmltools_0.2.6  httpuv_1.3.2     jsonlite_0.9.16  mime_0.3        
 [6] R6_2.0.1         Rcpp_0.11.6      rsconnect_0.3.79 rstudioapi_0.3.1 tools_3.1.3     
[11] xtable_1.7-4   
0

There are 0 answers