0

I develop my Shiny application in French and therefore I use special characters like "é" (e with an accent) "ê", "Œ", and so on. But these special characters do not work in renderUI. For example, the category Pancréas (with "é") doesn't work properly unless I remove the é and set the category as Pancreas. How can I make special characters work in renderUI?

library(shiny)
library(tidyverse)
TypeOfDisease<-c(rep("Infection",12),rep("Cancer",5),rep("Infection",14),
                 rep("Cancer",9),rep("Infection",8),rep("Cancer",7),rep("Infection",15),rep("Cancer",0),
                 rep("Infection",12),rep("Cancer",18))
Organ<-c(rep("Oesophage",17),rep("Stomach",23),rep("Lung",15),rep("Pancréas",15),rep("Liver",30))
data<-data.frame(TypeOfDisease,Organ)
addmargins(table(data$TypeOfDisease,dataF$Organ))

ui<-fluidPage(
  
  sidebarLayout(
    sidebarPanel(
      selectInput("Organ","Select the organ",
                  choices = c("Total",levels(data$Organ))),
      
      uiOutput("ui"),
    ),
    mainPanel(
      
      fluidRow(
        column(5,tableOutput("table")),
        column(7,
               
               fluidRow(verbatimTextOutput("matrix")),
               fluidRow(verbatimTextOutput("Nrow"))
        )
      )
      
      
      
    )
  )
  
)


server<-function(input,output){
  
  
  output$ui<-renderUI(
    
    switch (input$Organ,
            
            "Total" = selectInput("TypeOfDis","Type of disease",
                                  choices = c("Total","Infection","Cancer")),
            "Oesophage" = selectInput("TypeOfDis","Type of disease",
                                      choices = c("Total","Infection","Cancer")),
            "Stomach" = selectInput("TypeOfDis","Type of disease",
                                    choices = c("Total","Infection","Cancer")),
            "Lung" = selectInput("TypeOfDis","Type of disease",
                                 choices = c("Total","Infection","Cancer")),
            "Pancréas" = selectInput("TypeOfDis","Type of disease",
                                     choices = c("Total")),
            "Liver" = selectInput("TypeOfDis","Type of disease",
                                  choices = c("Total","Infection","Cancer")),
    )
  )
  
  dataFilter<-reactive({
    
    if(input$TypeOfDis=="Total"&input$Organ=="Total"){
      data
    }else {
      
      
      if(input$TypeOfDis=="Total"){
        data[data$Organ==input$Organ,]
      }else if (input$Organ=="Total"){
        data[data$TypeOfDisease==input$TypeOfDis,]
      }else
        data[data$TypeOfDisease==input$TypeOfDis & data$Organ==input$Organ ,]
    
      
      
      } 
    
  })
  
  output$table<-renderTable ({dataFilter()})
  output$matrix<-renderPrint({addmargins(table(data$TypeOfDisease,dataF$Organ))})
  output$Nrow<-renderPrint({nrow(dataFilter())})
  
}



shinyApp(ui,server)
Seydou GORO
  • 1,147
  • 7
  • 13
  • 1
    Your code runs fine for me too! – YBS Apr 23 '21 at 18:49
  • If you select `Pancreas`, the dataset is filtered but does the second `selectinput` window appear so that you can select the disease type? – Seydou GORO Apr 24 '21 at 10:52
  • Also, you need to use `req()` to eliminate the warnings in the console. – YBS Apr 24 '21 at 13:08
  • Yes, but the only choice is `Total` according to your data. Also, I use `unique()` in the first `selectInput` in `ui`, instead of `levels()`. Lastly, you have some typos... `dataF$`, instead of `data$` in a couple of places. – YBS Apr 24 '21 at 13:11

1 Answers1

2

Running your example above, the "é" displays properly for me. I would check to make sure the encoding is set to something like UTF-8 that allows for these characters. I'm not expert on encoding (and it can be a deep rabbit hole) but I would check first when parsing the code. For example, I copied your example into a blank file and ran source, this command appeared in the console source('~/.active-rstudio-document', encoding = 'UTF-8', echo=TRUE)

If that's set and still not displaying properly, launch the app into a browser and inspect the page source. Make sure UTF-8 is declared in the head element. Something such as:


<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

The last thing I can think of is then trying to either directly substitute the HTML codes or Unicode value (\U00E9) into the label definitions. This post discussing manually specifying Unicode in R strings maybe helpful.

Marcus
  • 3,478
  • 1
  • 7
  • 16