Problem with encoding when publishing to shinyapps.io

Hello! I am trying to create an application (for my wedding :smiley: ) that reads in poetry (of different genres and languages, which you can choose from) and then generates new "poems" using Markov Chain sequences. It works great on my local machine, but the encoding breaks when printing out the generated poem; a problem when Finnish is littered with "ä" and "ö". The English language poems show nicely but I'd like to show the Finnish ones as well.

Curiously, the "ä" shows correctly on UI title panel. It only breaks down on textOutput.

Can I change deployment specifications or what can I do to have my printed poem show correctly? This is really annoying.

Here is the application hosted on shinyapps: https://lassewinter.shinyapps.io/talvipelto18/

And this is the code. Both ui.R and server.R files are saved with UTF-8 encoding on my local computer.

ui.R:



library(shiny)

ui <- fluidPage(
  

  titlePanel("#Talvipelto18 Virallinen hääapplikaatio!"),
  

  sidebarLayout(
    
    sidebarPanel(
      
     
      selectInput("tyylilaji", "Valitse runosi tyylilaji",
                  c("Moderni suomenkielinen"=1,
                            "Moderni englanninkielinen"=2,
                            "Klassinen englanninkielinen"=3,
                            "Klassinen suomenkielinen"=4,
                            "Shakespeare"=5))
      ),
    
    
    mainPanel(
      
      
      textOutput("runo")
      
    )
  )
)

server.R

#the official wedding App!

library(shiny)
library(gutenbergr)
library(dplyr)
library(markovchain)
library(RCurl)

#collect books from Gutenberg

kirjat <- gutenberg_download(c(27899,23543,43521,27898,20883,47985), meta_fields = "author", strip = TRUE)

books <- gutenberg_download(c(45470,1141,5125,4009,37649,15311,2491), meta_fields = "author", strip = TRUE)

shakespeare <- gutenberg_download(1041, meta_fields = "author", strip = TRUE)

#read more poems from my dropbox account

modernitRunot <- readLines("newuserwasallowedtopostonly2links", skipNul = TRUE,
encoding = "latin1")

modernitRunot <- gsub("ÿþ","",modernitRunot)
modernitRunot <- modernitRunot[nchar(modernitRunot) > 0]

poems <- readLines("https://www.dropbox.com/s/qdd3cngd4d5hz1u/lovePoems.txt?raw=1", skipNul = TRUE,
encoding = "latin1")

poems <- poems[nchar(poems) > 0]

#set encoding

Encoding(kirjat$text) <- "latin1"
Encoding(books$text) <- "latin1"
Encoding(shakespeare$text) <- "latin1"

#clear empty rows

kirjat <- filter(kirjat, nchar(text)>0)
books <- filter(books, nchar(text) > 0)
shakespeare <- filter(shakespeare, nchar(text) > 0)

server part starts

shinyServer(function(input, output){

output$runo <- renderText({

#choose which genre your generated poem reflects

if(input$tyylilaji==1){
  teksti <-modernitRunot

  
}else if(input$tyylilaji==2){
  
  teksti <- poems
}else if(input$tyylilaji==3){
  teksti <- paste(books$text, collapse = " ")
 
}else if(input$tyylilaji==4){
  teksti <- paste(kirjat$text, collapse = " ")
  
}else{
  
  teksti <- paste(shakespeare$text, collapse = " ")
}



#split text to terms

terms <- unlist(strsplit(teksti, ' '))

terms <- terms[nchar(terms) > 0]

#limit to max 2000 terms for quicker model fits

if(length(terms) > 2000){
  
  alku <- round(runif(1,1,length(terms)-2000),0)
  
  loppu <- alku + 2000
  
  terms2 <- terms[alku:loppu]
}else{
  terms2 <- terms
}



#fit markov chain

fit <- markovchainFit(data = terms2)

#generate poem 

runo <- paste(markovchainSequence(n=20, markovchain=fit$estimate), collapse=' ')

Encoding(runo) <- "latin1"

runo

})

})

Encoding problems, the bane of non-English coders...

shinyapps.io linux servers have a 'UTF-8' locale so switching your encoding from latin1 to utf-8 might do the trick? Try the enc2utf8() function.

This did the trick, thanks! In hindsight, the solution was obvious :stuck_out_tongue:

nice one. enjoy the wedding!

A post was merged into an existing topic: Using iconv code map in shinyapps.io