Joni Hoppen
Joni Hoppen

Reputation: 688

How to render a text field in a radio radio button in shiny?

I need to translate the option that in a list, the current code does not run. The function tr() does the translation from one term to another in a .csv dictionary.

radioButtons(
              "enc",
              renderText({tr("Select encoding in case your data presents strange characters:")}),
              choices = c(
                renderText({tr("Automatic")}) = "unknown",
                "UTF-8" = "UTF-8",
                "Windows" = "Latin-1"
              ),
              selected = "unknown",
              inline = TRUE
            )

The current result:

Error in source("server/body.R", local = TRUE) : 
server/body.R:86:48: unexpected '='
                 choices = c(
                 renderText({tr("Browse")}) =
                                               ^

Upvotes: 0

Views: 678

Answers (1)

Tonio Liebrand
Tonio Liebrand

Reputation: 17699

A reproducible example would be very helpful. For us to get started quick and for you to ensure the answer fits your request.

If you want to use dynamic ui elements, you should use renderUI().

Also you should double check some basics,... renderText() produces and output and should not be used within another render() function.

I simulated the tr() function with: tr <- function(name) return(paste("TR:", name)).

A full working example is below, from there on you should be able to integrate it in your code. Good luck!:

library(shiny)

tr <- function(name) return(paste("TR:", name))

ui <- fluidPage(
  uiOutput("radio"),
  plotOutput("distPlot")
)

server <- function(input, output) {

  output$radio <- renderUI({
    opt <- c("Normal" = "norm",
             "Uniform" = "unif",
             "Log-normal" = "lnorm",
             "Exponential" = "exp")
    names(opt)[1] <- tr("Normal")
    label <-  tr("Distribution type:")

    radioButtons("dist", label, opt)
  })  

  output$distPlot <- renderPlot({
    dist <- switch(input$dist,
                   norm = rnorm,
                   unif = runif,
                   lnorm = rlnorm,
                   exp = rexp,
                   rnorm)

    hist(dist(500))
  })
}

shinyApp(ui, server)

Upvotes: 1

Related Questions