Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create snippet or extend addin to allow specification of LANGUAGE for the reprex #250

Closed
andresrcs opened this issue Apr 16, 2019 · 10 comments · Fixed by #274
Closed

Create snippet or extend addin to allow specification of LANGUAGE for the reprex #250

andresrcs opened this issue Apr 16, 2019 · 10 comments · Fixed by #274
Labels
feature tidy-dev-day 🤓

Comments

@andresrcs
Copy link

@andresrcs andresrcs commented Apr 16, 2019

My native language is spanish, so the locale on my PC is ES_us, but when posting a reprex to English forums that includes error messages I would like to use EN_us, that way I wouldn't have to manually translate the error messages, or remeber to manually change my locale beforehand, would you consider adding this funtionality? I think it would be pretty handy for non-English users.

@batpigandme batpigandme added the feature label Apr 16, 2019
@batpigandme
Copy link
Member

@batpigandme batpigandme commented Apr 16, 2019

Temporary workaround – you can use the withr package with_envvar() to set the language.
So, for example, if I run the following code as a reprex as I usually do, the message is in English.

dplyr::select(foo)
#> Error in dplyr::select(foo): object 'foo' not found

If I run:

withr::with_envvar(c("LANG" = "de_DE.UTF-8"), code = {reprex::reprex(dplyr::select(foo))})

I get

dplyr::select(foo)
#> Error in dplyr::select(foo): Objekt 'foo' nicht gefunden

Created on 2019-04-16 by the reprex package (v0.2.1)

@andresrcs
Copy link
Author

@andresrcs andresrcs commented Apr 16, 2019

Thanks Mara!, this is a nice walkaround solution although I still think it would be more convenient to have this functionality on the reprex() function itself.

@jennybc
Copy link
Member

@jennybc jennybc commented May 17, 2019

Assuming that one attaches reprex in .Rprofile (I think heavy users do) and that target code is on the clipboard, @batpigandme's suggestion gets very slick:

withr::with_envvar(
  c("LANG" = "de_DE.UTF-8"),
  reprex()
)

I can't see a built-in solution being that much easier/shorter and it would create more code to document and maintain. This looks like a good thing to create an RStudio snippet for? Or maybe even incorporate this as a feature in the current gadget/addin? I think all of those are better ways to make this withr trick more available than to add another argument to reprex() itself.

Do you @andresrcs have any interest in noodling on those alternatives?

@jennybc jennybc added the tidy-dev-day 🤓 label May 17, 2019
@jennybc
Copy link
Member

@jennybc jennybc commented May 17, 2019

Re: tidyverse dev day

An interesting project would be to explore exposing this method of setting the language in the existing addin/gadget.

reprex already imports withr. A minimum viable product, therefore, it's adding some sort of input for language and reworking the reprex() call if the user wants something other than their default. I don't have much experience with doing this in general, so I would anticipate some thought around ... Does the user have to provide this string or is there some sort of dropdown menu? Can we determine all of the valid choices and present them?

@andresrcs
Copy link
Author

@andresrcs andresrcs commented May 17, 2019

Do you @andresrcs have any interest in noodling on those alternatives?

I whish I had the skills needed, I just ended up creating my own reprex_en() variant in .Rprofile, this is more convenient for me since I mostly work on RStudio Server, so no clipboard available.

reprex_en <- function(...) {
  withr::with_envvar(
    c("LANG" = "en_US.UTF-8"),
    reprex::reprex(...)
  )
}

@jennybc
Copy link
Member

@jennybc jennybc commented May 17, 2019

I think that's a good solution! Esp. for an individual.

I will leave this open in case anyone want to tackle at our next tidyverse dev day.

@andresrcs
Copy link
Author

@andresrcs andresrcs commented May 17, 2019

Maybe something like this would be more generalizable

reprex_locale <- function(..., locale = "en_US.UTF-8") {
  withr::with_envvar(
    c("LANG" = locale),
    reprex::reprex(...)
  )
}

@jennybc jennybc changed the title Feature request: Allow to specify different locale for rendering Feature request: Allow to specify different language for rendering May 17, 2019
@jennybc
Copy link
Member

@jennybc jennybc commented May 17, 2019

This is related to locale in the Sys.getlocale() sense. But, from those docs, note that:

Note that the LANGUAGE environment variable has precedence over "LC_MESSAGES" in selecting the language for message translation on most R platforms.

And this thread appears to really be about this, more than true aspects of locale. Not sure what to make of this difference: "LANGUAGE" (from the docs) vs. "LANG" (from all the working examples here).

@hadley
Copy link
Member

@hadley hadley commented May 17, 2019

Details at https://cran.rstudio.com/doc/manuals/r-devel/R-admin.html#Localization-of-messages — I think setting LANGUAGE is most appropriate here

@andresrcs
Copy link
Author

@andresrcs andresrcs commented May 17, 2019

This make me think about this other use case, being English the "lingua franca" I also want my date labels in English when rendering a reprex.

reprex_locale <- function(..., locale = "en_US.UTF-8") {
    withr::with_envvar(
        c("LC_ALL" = locale),
        reprex::reprex(...)
    )
}

library(ggplot2)
df <- data.frame(stringsAsFactors = FALSE,
                 date = as.Date(c("2019-01-01", "2019-02-01")),
                 value = c(1, 2))
ggplot(df, aes(x = date, y = value)) +
    geom_col() +
    scale_x_date(date_breaks = "1 month", date_labels = "%b")

@jennybc jennybc changed the title Feature request: Allow to specify different language for rendering Create snippet or extend addin to allow specification of LANGUAGE for the reprex May 18, 2019
@jennybc jennybc closed this in #274 Jul 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature tidy-dev-day 🤓
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants