Asked  4 Months ago    Answers:  5   Viewed   33 times

I would like to create many multiple selectize inputs which are connected with each other. In other words : if an item is selected in one of the selectizeinputs i would like that it disappears from the other selectizeinputs' choices. In addition, i would like that the number of selectize inputs corresponds to the number selected in a numericinput.

The example below is working. The only question I have left is on the following line :

  X = 1:100, ####### QUESTION HERE

Instead of 1:100, i would like to put something like 1:input$ui_number but I have the following error in R :

 Error in .getReactiveEnvironment()$currentContext() :  Operation not allowed without an active reactive context.

And if I put a "reactive" or an "observe" function around the lapply, the observeEvent does not work anymore. Any trick for me ?

Thank you for your help !

 modalities <- LETTERS[1:10]

 library(shiny)

 app <- shinyApp(
   ui = tabPanel("Change modalities",
            numericInput("ui_number", label="Number of modalities",min = 1, max = 4, value=3),
            uiOutput("renderui")
   ),


   server = function(input, output, session) {


     output$renderui <- renderUI({
       output = tagList()
       for(i in 1:input$ui_number){
         output[[i]] = tagList()
         output[[i]][[1]] = selectizeInput(paste0("ui_mod_choose",i), label=paste0("Modality ",i),choices=modalities, multiple = TRUE)
  }
  return(output)
})


 lapply(
  X = 1:100, ####### QUESTION HERE
  FUN = function(j){
    observeEvent({
      input[[paste0("ui_mod_choose",j)]]
    },
    {
      sapply(1:input$ui_number,function(i){
        vecteur <- do.call(c,lapply((1:input$ui_number)[-i],function(i){input[[paste0("ui_mod_choose",i)]]}))
        updateSelectizeInput(session,paste0("ui_mod_choose",i),choices=  modalities[!modalities %in% vecteur],selected = input[[paste0("ui_mod_choose",i)]])
      })
    },
    ignoreNULL = FALSE)

  }
)


observeEvent({
  input$ui_num
},
{
  sapply(1:nput$ui_num,function(i){ 
    updateSelectizeInput(session,paste0("ui_mod_choose",i),choice= modalities,selected=NULL)
       })
     }
     )

   }

 )
 runApp(app)

 Answers

67

You could have a single observe() instead of multiple observeEvent():

library(shiny)

modalities <- LETTERS[1:10]

ui = tabPanel("Change modalities",
              numericInput("ui_number", label = "Number of modalities",
                           min = 1, max = 4, value = 3),
              uiOutput("renderui"))

server = function(input, output, session) {

  # Generate modalities select lists
  output$renderui <- renderUI({
    output = tagList()
    for (i in seq_len(input$ui_number)) {
      output[[i]] = selectizeInput(paste0("ui_mod_choose", i), 
                                   label = paste0("Modality ", i),
                                   choices = modalities, multiple = TRUE)
    }
    return(output)
  })

  # Remove selected modalities from other select lists
  observe({
    n <- isolate(input$ui_number)
    for (i in seq_len(n)) {
      vecteur <- unlist(lapply((1:n)[-i], function(i) 
        input[[paste0("ui_mod_choose",i)]]))
      updateSelectizeInput(session, paste0("ui_mod_choose",i),
                           choices = setdiff(modalities, vecteur),
                           selected = input[[paste0("ui_mod_choose",i)]])
    }
  })

}

runApp(shinyApp(ui, server))
Saturday, August 21, 2021
 
ala
answered 4 Months ago
ala
82

This is documented in ?lapply, in the "Note" section (emphasis mine):

For historical reasons, the calls created by lapply are unevaluated, and code has been written (e.g. bquote) that relies on this. This means that the recorded call is always of the form FUN(X[[0L]], ...), with 0L replaced by the current integer index. This is not normally a problem, but it can be if FUN uses sys.call or match.call or if it is a primitive function that makes use of the call. This means that it is often safer to call primitive functions with a wrapper, so that e.g. lapply(ll, function(x) is.numeric(x)) is required in R 2.7.1 to ensure that method dispatch for is.numeric occurs correctly.

Saturday, June 19, 2021
 
ClmentM
answered 6 Months ago
17

Use this line:

startDate TIMESTAMP := endDate - ($3 || ' MONTH')::INTERVAL;

and note the space before MONTH. Basically: You construct a string with like 4 MONTH and cast it with ::type into a proper interval.

Edit: I' have found another solution: You can calculate with interval like this:

startDate TIMESTAMP := endDate - $3 * INTERVAL '1 MONTH';

This looks a little bit nicer to me.

Thursday, June 24, 2021
 
makadev
answered 6 Months ago
69

It will work with the following lapply call:

lapply(names(mylist), function(x) NewVar(mylist[[x]], "y", x))
Tuesday, June 29, 2021
 
EurekA
answered 6 Months ago
59

Same problem here... Solved.

The shiny-server and rstudio server has a pandoc embedded on installation directory.

Just remove the bad pandoc binary file of shiny server and do a logical link in the same location to a new pandoc version or, in my case, I used the pandoc version of a local rstudio server that is working.

Thursday, November 25, 2021
 
Hauke Ingmar Schmidt
answered 1 Week ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share