0

I am trying to run a simple command using gtrendsR package, but it's giving me an error saying Error in make.names(col.names, unique = TRUE) : invalid multibyte string 1

Here is the code:

res <- gtrends(c("nhl", "nba"), geo = c("CA", "US"))
user9532692
  • 584
  • 7
  • 28

2 Answers2

1
Sys.setlocale("LC_CTYPE", "English") 

If the error is due to different language setting, then it should work for you.

However, if you want to search keyword in other languages instead of English (e.g. Chinese for me), then another problem is the keywords in the retrieved data might be encoded. My trick is simply reset LC_CTYPE to the original setting.

Sys.setlocale("LC_CTYPE", "Chinese (Traditional)")
1

For someone who has difficulty like me.

Even if some keywords worked well, some keywords did not. I do not know

what makes differences.

Some keywords occured [Error in make.names(col.names, unique = TRUE) : invalid multibyte string] probelm.

I tried many things and nothing worked

Things I have tried and didn't work

  1. read.csv(~, fileEncoding = "UTF-8") and (~~ encoding = "UTF-8")
  2. re-save file in notepad
  3. Encoding()

The solution

At first, I use "Korean Language" and use Windows10, all of my CSV files are encoded as (ASCII)

If I re-encode original CSV files, problems occured at read-file step.

Conclusion

Above, Sys.setlocale() is the only solution in my case with some limitations.

You can find your own Sys.locale bySys.getlocale()`.

In my case,

["LC_COLLATE=Korean_Korea.949;LC_CTYPE=Korean_Korea.949;LC_MONETARY=Korean_Korea.949;LC_NUMERIC=C;LC_TIME=Korean_Korea.949"]

So I changed locale settings to Sys.setlocale("LC_CTYPE", "English")

Limitations

Even "geo" is correct, the result of "related_topics" is doubtable because the related_topics are translated.

Below is my code

google.trends = gtrends(keyword = key_final, geo = "KR", gprop = "web", time = "2018-01-01 2018-11-30")[[1]]
google.trends = dcast(google.trends, date ~ keyword + geo, value.var = "hits")
rownames(google.trends) = google.trends$date
google.trends$date = NULL
google.trends
plot(google.trends[[1]], type = 'l')

Screenshot 1

but the result is translated

Screenshot 2

Ali
  • 2,702
  • 3
  • 32
  • 54
YopenL
  • 11
  • 1