skip to Main Content

I try to make a shiny dashboard with translations. I got the example from the Appsilon i18n hp.

Here is the JSON:

"languages": ["en", "pl", "hr"],
"translation": [
    {
        "en": "MtCars Dataset Explorer",
        "pl": "Eksplorator danych: MtCars",
        "hr": "Istraživanje MtCars Dataseta"
    },
    {
        "en": "Change language",
        "pl": "Wybierz język",
        "hr": "Promijeni jezik"
    },
    {
        "en": "Select",
        "pl": "Wybierz",
        "hr": "Odaberi"
    },
    {
        "en": "This is description of the plot.",
        "pl": "To jest opis obrazka.",
        "hr": "Ovo je opis grafikona."
    },
    {
        "en": "Scatter plot",
        "pl": "Wykres punktowy",
        "hr": "Dijagram raspršenja"
    },
    {
        "en": "X-Axis",
        "pl": "Oś X",
        "hr": "X os"
    },
    {
        "en": "Y-Axis",
        "pl": "Oś Y",
        "hr": "Y os"
    }
]

Here is the R code:

## load libraries
library(ggplot2)
library(ggpubr)
library(extrafont)
library(shiny)
library(shinydashboard)
library(shiny.i18n)

## UI translation
i18n <- Translator$new(translation_json_path = "./trans1.json") # load translation file
i18n$set_translation_language("en") # set default language

ui <- fluidPage(
  shiny.i18n::usei18n(i18n),
  tags$div(
    style='float: right;',
    selectInput(
      inputId='selected_language',
      label=i18n$t('Change language'),
      choices = i18n$get_languages(),
      selected = i18n$get_key_translation()
    )
  ),
  titlePanel(i18n$t('MtCars Dataset Explorer'), windowTitle=NULL),
  sidebarLayout(
    sidebarPanel(
      width=3,
      tags$h4(i18n$t('Select')),
      varSelectInput(
        inputId='x_select', 
        label=i18n$t('X-Axis'), 
        data=mtcars
      ),
      varSelectInput(
        inputId='y_select', 
        label=i18n$t('Y-Axis'),
        data=mtcars
      )
    ),
    mainPanel(
      plotOutput(outputId='scatter'),
      tags$p(i18n$t('This is description of the plot.'))
    )
  )
)

server <- function(input, output, session) {
  observeEvent(input$selected_language, {
    update_lang(session, input$selected_language)
  })
  
  output$scatter <- renderPlot({
    col1 <- sym(input$x_select)
    col2 <- sym(input$y_select)
    
    ggplot(mtcars, aes(x= !! col1, y= !! col2)) + 
      geom_point(size=6) + 
      ggtitle(i18n$t('Scatter plot'))
  })
}

app <- shinyApp(ui=ui, server=server)

Start the app with runApp(app) and I get the error message

Warning: Error in $: $ operator is invalid for atomic vectors

I only use "$" on the "input" and "i18n" variables. If I ask R:

is.atomic(i18n)
[1] FALSE

If I launch the code without the 7 selectInput lines, I do not get the error message.

What did I do wrong? What do I not get? Have there been changes in packages that I missed?

Thank you for your help!

2

Answers


  1. not sure if you solved this already, but I ran into the same problem just now and found your post. There seems to be an update from version 0.2 to 0.3 that’s causing this error. So far I am able to install an older version of shiny.i18n to make it work, try:

    remotes::install_version("shiny.i18n", version = "0.2.0", repos = "http://cran.us.r-project.org")
    

    corresponding CRAN repo: https://cran.r-project.org/src/contrib/Archive/shiny.i18n/

    Not sure if they’ll be updating this in the future so v0.3 would work as well. But right now its the best workaround for now.

    Login or Signup to reply.
  2. I had the same issue. It seems that going from version 0.2.0 to 0.3.0 may create this error depending on the order of arguments to the function update_lang().

    With version 0.3.0 you should call the function update_lang() as follow :

    update_lang(input$selected_language, session)
    

    It seems that the order of the arguments called in your function is important with the version 0.3.0.

    It’s an issue on which they seem to be working on (see : https://github.com/Appsilon/shiny.i18n/issues/111).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search