R Shiny download data encoding issue

2019-07-12 04:33发布

Maybe it is a silly question but I am really confused. We have Shiny server running on Linux system and set language = UTF-8 to ensure it shows special characters correctly (e.g. 谭盾, いきも,Acatlán de Pérez Figueroa). However, when I tried to download the data (I have a download button utilizing write.csv function), those special characters got mess up.

When I specify the encoding, it works well for both Windows and Mac systems, meaning both Windows and Mac users are able to download correct data from the Shiny page.

It first allows users to upload data and then manipulate and download it eventually. Below is the simplified example:

#upload data

filedata <- reactive({
infile <- input$file
if (is.null(infile)) {
  # User has not uploaded a file yet
  return(NULL)
}
fread(infile$datapath, encoding = "UTF-8")
}) 

#output data

output$im = downloadHandler(
filename = function(){
  paste("Request ",Sys.time(),".csv",sep="")
},
content = function(file){
  write.csv(filedata(),file,row.names = F,fileEncoding = "latin1")
}
)

I understand why Windows users could download correct data. But I don't know why Mac users can also do that, because I know Mac and Linux are both using UTF-8. If I set fileEncoding = "latin1", it should mess up when users are using Mac.

Any ideas why it happens?

Thanks in advance.

0条回答
登录 后发表回答