write.xlsx(openxlsx)的编码问题 [英] Encoding issue with write.xlsx (openxlsx)

查看:127
本文介绍了write.xlsx(openxlsx)的编码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 write.xlsx()函数(来自 openxlsx 包)将列表对象转换为excel电子表格,在该电子表格中转换列表的每个元素放入excel文件的工作表".过去,此功能非常有用,而且我从未遇到过任何问题.据我了解,此程序包和功能尤其不需要在计算机上进行任何特定的Java更新即可使其工作.

I use the write.xlsx() function (from the openxlsx package) to turn a list object into an excel spreadsheet, where each element of the list is converted into a "sheet" of the excel file. In the past, this function has been incredibly useful, and I have never encountered any issues. It is my understanding that this package and function in particular does not need any particular java update on the computer in order for it to work.

但是,最近我发现该函数产生了错误.这是我为列表运行write.xlsx()时在控制台中指出的内容:

However, recently I discovered that the function is producing error. This is what it states in the console when I run the write.xlsx() for the list:

Error in gsub("&", "&", v, fixed = TRUE) : 
  input string 5107 is invalid UTF-8

我已经确定了引起问题的数据框,但是我不确定如何确定数据框的哪一部分导致了错误.

I've identified the dataframes that are the cause of the issue, but I am not sure how to identify which part of the data frame is causing the error.

我什至继续使用了 enc2utf8()函数,尤其是该数据帧中的所有列,但仍然遇到错误.我已经在数据框本身上使用了 substr()函数,该函数向我显示了每一列的前 n 个字符,尽管我看不到任何明显的问题.输出.

I've even went ahead and used the enc2utf8() function for all of the columns in this data frame in particular but I still encounter the error. I've used the substr() function on the data frame itself, and it shows me the first n characters of each column, though I do not see any obvious issues from the output.

我什至还继续使用了 install.packages()函数,以便在有任何更新的情况下再次重新下载 openxlsx 软件包.

I've even went ahead and used the install.packages() function to re-download the openxlsx package again, in case of any updates.

有人知道我将如何确定错误原因吗?它是包装中所写的功能吗?如果问题出在数据本身的编码中,那么 enc2utf8()是否不足以解决问题?

Does anyone know how I would go about identifying the cause of the error? Is it the function as it is written in the package? If the problem is in the encoding of the data itself, does the enc2utf8() not suffice to resolve the issue?

谢谢!

推荐答案

我只是遇到了同样的问题.基于此问题,您可以替换数据框中的所有不良字符,其中之一:

I just had this same problem. Building on this question, you could replace all bad characters in the dataframe with either:

library(dplyr)
df %>%
  mutate_if(is.character, ~gsub('[^ -~]', '', .))

仅用于字符列,或:

df %>%
  mutate_all(~gsub('[^ -~]', '', .))  

对于所有列,然后使用 write.xlsx()导出到XLSX.

for all columns, and then export to XLSX with write.xlsx().

这篇关于write.xlsx(openxlsx)的编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆