TarJae
TarJae

Reputation: 78927

How to use paste0 with input$ in shiny

This is a follow up question to this Avoid DRY with 13 sliderInputs and 13 textInputs:

How can I shorten this code:

  sliderValues <- reactive({
    
    data.frame(
      Name = c("A",
               "B",   
               "C"),
      Value = as.character(c(input$a,
                             input$b,
                             input$c
                            )),
      stringsAsFactors = FALSE)
  })

enter image description here

I have tried:

  # Reactive expression to create data frame of all input values ----
  sliderValues <- reactive({
    
    data.frame(
      Name = c(LETTERS[1:3]),
      Value = paste0("input$", letters[1:3]),
      stringsAsFactors = FALSE)
  })

enter image description here

Upvotes: 1

Views: 215

Answers (1)

stefan
stefan

Reputation: 124148

One option would be to use sapply or ...

# Reactive expression to create data frame of all input values ----
  sliderValues <- reactive({
    
    data.frame(
      Name = c("A",
               "B",
               "C"),
      Value = as.character(sapply(letters[1:3], function(x) input[[x]])),
      stringsAsFactors = FALSE)
    
  })

Upvotes: 3

Related Questions