1

I need a line in my server.r code of this type (basically in the server I would arrange all inputs from ui.r in a list):

x<- list(input$field1,input$field2,input$field3, etc... until n=100)
x<- x[!= is.null(x)]
h4(x) 

The code is used inside a renderUI({}). When I write it manually, it works fine. But surely there must be a way to use cat and paste (or other functions) to write it more concisely.

Using the following would not work and I don't get why:

x <- cat(paste("input$field", 1:100,",", sep = ""))
list(x)

Any help/advice would be highly appreciated

ps: I need this because my inputs are generated depending on a button, and so it may be that fields with large Id's such as field99 are not created and I need to test which ones have been created.

Cheers

user1431694
  • 715
  • 1
  • 11
  • 22
  • See: http://stackoverflow.com/questions/2679193/how-to-name-variables-on-the-fly-in-r – harkmug Mar 05 '14 at 19:06
  • Thanks rmk ! For some reason it does not work, but this works: res <- c() and then for(i in 1:100){res <- c(res, input[[paste("field",i,sep='')]])}. However I obtain warnings: if(! grepl(pattern, text)) return(text): the condition has length >1 and only the first element will be used. If anybody has an idea why, it would be highly appreciated. Cheers – user1431694 Mar 05 '14 at 19:55
  • If `input` is a list, you could also try `do.call("c", input)` to concatenate all variables in that list. – harkmug Mar 05 '14 at 19:58
  • what do you mean by "c" in this case ? When I use res <- do.call("c",input), I obtain the following message: cannot coerce type 'environment' to vector of type 'character'. (many thanks for your help so far :-) – user1431694 Mar 05 '14 at 20:04
  • Sorry, I think you can try `do.call("c", as.list(input))` instead. Here "c" is concatenate (all objects in the list `input`). It could be replaced by any other function, like `sum`, etc. – harkmug Mar 05 '14 at 20:26
  • thx! Unfortunately it does not work. I may stay with the warnings :-( I think that it may have to do with how it is generated. It is actually a snippet that I try to implement (http://bootsnipp.com/snippets/featured/dynamic-form-fields-add-amp-remove), and as far as I can see, it is in a form whose class is input-append, and it is in a control-group. I am new to jQuery and html, so there may be something obvious that I miss – user1431694 Mar 05 '14 at 21:00

1 Answers1

6

To get all the input variables as a list, try:

x <- reactiveValuesToList(input)

or,

x <- lapply(1:3, function(z) input[[paste0("field", z)]])

Simple demo: Sum up three user-provided reactive inputs as a list

server.R (EDITED to use second method)

library(shiny)

shinyServer(function(input, output) { 
  output$restable <- renderTable({
    mylist <- lapply(1:3, function(z) input[[paste0("slide", z)]])
    data.frame(Names=c("Slider 1", "Slider 2", "Slider 3", "Sum"),
               Values=c(do.call("c", mylist), do.call("sum", mylist)))
#Values=c(do.call("c", reactiveValuesToList(input)), do.call("sum", reactiveValuesToList(input))))
  })
})

ui.R

library(shiny)

# Define UI for application
shinyUI(pageWithSidebar(

  # Application title
  headerPanel("Sliders should sum to 100!"),

  # Sidebar with 3 slider inputs
  sidebarPanel(
   sliderInput("slide1", "Slider 1: ", min = 0, max = 100, value=40, step=1),
   sliderInput("slide2", "Slider 2: ", min = 0, max = 100, value = 30, step=1),
   sliderInput("slide3", "Slider 3: ", min = 0, max = 100, value = 30, step=1)
  ),

  # Create table output
  mainPanel(
    tableOutput("restable")
  )
))
harkmug
  • 2,725
  • 22
  • 26
  • see my comment above. Thanks anyway for your help ! – user1431694 Mar 05 '14 at 21:00
  • Yes it does ! thanks ! The only difference with my code is that you use 'session' in your server. I'll try to use if this resolves the matter ! Once again: many thanks. I'll tell here whether it works or not soon – user1431694 Mar 05 '14 at 21:36
  • Sorry, I copy-pasted and simplified another piece of code. The `session` is not needed in this code. Now deleted. – harkmug Mar 05 '14 at 21:47
  • Actually I may be interested in the whole code then if you don't mind sharing it, because I am trying to learn updateSliderInput() and this could help me to see how you link the three sliders. – user1431694 Mar 05 '14 at 21:55
  • regarding the initial question: I think I know why. The Input is a complicated animal because it includes both some fields (1 to n) and some buttons (1 to n too), and I don't know yet how the whole thing is structure (see my link to the snippet above if you're interested by any chance). That's possibly why I need to refer specifically to the fields, and it's just a question of writing it more concisely – user1431694 Mar 05 '14 at 21:59
  • It turns out that the calling the "fields" specifically works too. See edit in answer above. – harkmug Mar 05 '14 at 22:09
  • YEAH !!! This does now work !!! Many thanks for your persistence, and the solution is very elegant in terms of code !!! – user1431694 Mar 05 '14 at 23:03
  • Used this to get input values by string by input[["color"]] for ui element "color". Thanks! – sbanders Mar 06 '15 at 16:57