Call one element of an output vector

44 views
Skip to first unread message

Stephen Lauer

unread,
May 14, 2015, 10:27:24 AM5/14/15
to shiny-...@googlegroups.com
Hello,

I want to translate my app from English into other languages. I have a data frame that has both the English and the translations. Rather than make 20 output variables to display text differently around the app, I wanted to make a vector of outputs then for each spot that I need text, to call the correct element from that vector to display. Here is a toy example of what I am talking about.


## data frame with translations
translation_df <- data.frame(English = c("Hello", "Goodbye"),
                             Spanish = c("Hola", "Adios"))


server <- function(input, output, session) {
 output$distPlot <- renderPlot({
  hist(rnorm(input$obs), col = 'darkgray', border = 'white')
 })
 
 ## if English is selected return vector of English phrases, ditto for Spanish
 output$txt <- renderUI({
  if(input$language == "english")
   return(translation_df[,1])
  if(input$language == "spanish")
   return(translation_df[,2])
 })
}

ui <- fluidPage(

 ## I want it to say "Hello" here in the correct language
 titlePanel(uiOutput("txt")[1]),
 
 sidebarLayout(
  sidebarPanel(
   sliderInput("obs", "Number of observations:", min = 10, max = 500, value = 100),
   
   ## input to choose language
   selectInput("language", "Language", choices = c("English" = "english", "Spanish" = "spanish")),
   
   ## I want it to say "Goodbye" here in the correct language
   helpText(uiOutput("txt")[2])
  ),
  mainPanel(plotOutput("distPlot"))
 )
)

shinyApp(ui = ui, server = server)




What I have found is that I can return the entire vector or nothing. I have experimented with different outputs (ui, text, dataframe, etc) and with the placement of the [1] or [[1]] (within quotes, outside of quotes but within parentheses, and outside of parentheses), but nothing has worked so far. I'm pretty sure I need some HTML code to call from the vector, but I am not familiar with how to do so.

Please help!

Thanks,
Steve

Sigbert Klinke

unread,
May 21, 2015, 4:03:34 AM5/21/15
to shiny-...@googlegroups.com
Hi,

why do you not follow the gettext idea, e.g.

translation_df <- data.frame(English = c("Hello", "Goodbye"),
                             
Spanish = c("Hola", "Adios"))


input
<- list(language=1)

gettext
<- function (txt) {
  pos
<- which(translation_df$English==txt)    
  translation_df
[pos, input$language]
}

input
<- list(language=1)
print(gettext("Hello"))
input
<- list(language=2)
print(gettext("Hello"))


Best Sigbert

Stephen Lauer

unread,
May 28, 2015, 10:30:05 AM5/28/15
to shiny-...@googlegroups.com
I don't think that works in the Shiny environment (at least I couldn't get it to work). I need something that works using the ui and server. Thanks for your effort though.

Sigbert Klinke

unread,
May 29, 2015, 3:31:00 AM5/29/15
to shiny-...@googlegroups.com
Works (with some modifications), as can be seen here:

library("shiny")


translation_df
<- data.frame(English = c("Hello", "Goodbye"),

                             
Spanish = c("Hola", "Adios"),
                             stringsAsFactors
=F)

gettext
<- function (txt, lang=1) {
  pos
<- which(translation_df$English==txt)    
  translation_df
[pos, lang]

}

server
<- function(input, output, session) {

 
  getLanguage
<- reactive({
   
if (is.null(input$language)) lang <- 1 else lang <- as.numeric(input$language)
    updateRadioButtons
(session, inputId='language', label = gettext("Hello", lang))
    lang
 
})
 
  output$language
<- renderUI({
    radioButtons
(inputId='language', label=gettext("Hello"), choices=list("English"=1, "Spanish"=2))
 
})
 
  output$someText
<- renderPrint({
   
print(gettext("Goodbye", getLanguage()))
 
})
}

ui
<-  fluidPage(
  sidebarLayout
(
    sidebarPanel
(
      uiOutput
("language")
   
),
    mainPanel
(
      textOutput
("someText")
   
)
 
)
)

shinyApp
(ui = ui, server = server)


If something does not work for you then you should give some details what exactly did not work. Otherwise we can not help you.

Joe Cheng

unread,
May 29, 2015, 11:50:24 AM5/29/15
to Sigbert Klinke, shiny-...@googlegroups.com
I think you should just make the 20 variables. But you don't need to make them by hand:


translation_df <- data.frame(English = c("Hello", "Goodbye"),
  Spanish = c("Hola", "Adios"))

ui <- fluidPage(
  ...
  helpText(textOutput("txt1"))
)

server <- function(input, output, session) {
  # Make all the rows into variables
  for (rowname in rownames(translation_df) {
    local({
      row <- rowname  # need our own copy of rowname inside of local scope
      output[[paste0("txt", row)]] <- renderText(translation_df[row,input$language])
    })
  }
}

--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/1d893837-9b40-4fd5-8e2a-cc0c1ebbafee%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages