Search code examples
htmlrmarkdownasciiknitr

R markdown won't knit to html on Mac


I am trying to knit an Rmarkdown to html on both mac and windows. In this file there is a lot of text data that is being processed into text columns using the R package gt. In windows, the code runs fine, without any issue.

When I try to run the same code on a mac, I get this error:

Error in gsub("'", "'", html_tbl) : input string 1 is invalid

I know what the issue is: there is an apostrophe in a text string which comes off looking like a question mark in a black diamond in the raw data:

"...think a lot of us didn�t quite understand wh..."

What I don't understand is, why does the code fail on an ios and not on a windows; what can I do to fix this and prevent this issue going forward, and if possible how can I fix this issue so that the sentence passed to the html output maintains its' original meaning and composition.

Edit: This is how the file is read:

df_import <- read_csv("file_path")

Solution

  • It seems like encoding issue. So you can try to read the csv with encoding. What you should do is:

    • First detect encoding with the uchardet library uchardet::detect_file_enc("csv_file.csv")
    • Second read the file with encoding, for instance using one of these:

    readr::read_csv2("csv_file.csv", locale = locale(encoding = "WINDOWS-1252"))

    readr::read_csv2("csv_file.csv", locale = locale(encoding = "latin1"))

    readr::read_csv2("csv_file.csv", locale = locale(encoding = "UTF-8"))