如何在闪亮的模块内将值添加到反应性元素(矢量)

时间:2019-11-17 01:48:04

标签: module shiny reactive

我正在尝试修改模块的输入参数中的selectInput选择如果值不存在

如果module参数不是反应性的,我可以使它工作(请参见下面的示例1)。 如果我省略了尝试将值添加到反应模块参数的行,则在传递反应参数时也可以使用。 (!any(xvar_choices=="none")){xvar_choices = c("none", xvar_choices)}

如何将元素(在本例中为“ none”)添加到反应值?

请注意,在output $ out下面的所有示例均不包括添加的“ none”,而selectInput选项则包括相同的值(ex#1中的xvar_choices),尽管它们都基于同一个值-这似乎很奇怪。

示例1-模块参数不起作用

 # module UI
   plotOpt_xvar_UI <- function(id){
      ns <- NS(id)
      tagList(
        uiOutput(ns("ui_opt_xvar")),
        h4("xvar_choices:"),
        verbatimTextOutput(ns("out"))
      )
   }

   #module SERVER - passing as not reactive
   plotOpt_xvar <- function(input, output, session, xvar_choices){

        output$ui_opt_xvar <- renderUI({
         ns <- session$ns

         #to add none option if not available...
         if(!any(xvar_choices=="none")){xvar_choices = c("none", xvar_choices)}

         selectInput(inputId = ns("opt_xvar"), label = "X-variable:",
          choices =  xvar_choices,
          selected = "none",
          multiple = FALSE, selectize = FALSE)
        })

        #this output doesn't include the "none"???
        output$out <- renderPrint({
          glimpse(xvar_choices)
        })

        return(reactive(input$opt_xvar))
   }

  # # Example in Shiny app ----------------------------------------------------

library(shiny)

ui <- fluidPage(

  plotOpt_xvar_UI("test1"),
  h4("value returned from xvar selectInput"),
  verbatimTextOutput("returned")
)

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

 test_out <- callModule(module=plotOpt_xvar, id="test1", xvar_choices=c("1", "2", "3"))

  output$returned <- renderPrint({
    glimpse(test_out())
  })

}

shinyApp(ui, server)

**示例2-将响应值传递给模块

  # mODulE UI
   plotOpt_xvar_UI <- function(id){
      ns <- NS(id)
      tagList(
        uiOutput(ns("ui_opt_xvar")),
        h4("xvar_choices:"),
        verbatimTextOutput(ns("out"))
      )
   }

   #module SERVER - passing as not reactive
   plotOpt_xvar <- function(input, output, session, xvar_choices){

        output$ui_opt_xvar <- renderUI({
         ns <- session$ns

         #WORKS WITHOUT THIS LINE - how can add "none" when it is a reactive variables?
         #if(!any(xvar_choices()=="none")){xvar_choices() <- c("none", xvar_choices())}

         #tried with  newVals$xsel in choices - nothing shows up for output...
          # newVals <- reactiveValues()
          # observe({
          #   newVals$xsel <- ifelse(!any(xvar_choices()=="none"), c("none", xvar_choices()), xvar_choices())
          # })

         selectInput(inputId = ns("opt_xvar"), label = "X-variable:",
          choices =  xvar_choices(),
          #choices = newVals$xsel,
          selected = "none",
          multiple = FALSE, selectize = FALSE)
        })

        #this output doesn't include the "none"???
        output$out <- renderPrint({
          #glimpse(xvar_choices())
          glimpse(newVals$xsel)
        })

        return(reactive(input$opt_xvar))
   }

  # # Example in Shiny app ----------------------------------------------------

library(shiny)

ui <- fluidPage(

  #note: this isn't how I actually select the variables...in reality it is based on a reactive value based on an input dataset
  selectInput(inputId = "var", label = "varin",
          choices = c("none", "a", "b"),
          selected = "none",
          multiple = TRUE, selectize = FALSE),

  plotOpt_xvar_UI("test1"),
  h4("value returned from xvar selectInput"),
  verbatimTextOutput("returned")
)

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

  vars <- reactive({
    input$var
  })

 test_out <- callModule(module=plotOpt_xvar, id="test1", xvar_choices=vars)

  output$returned <- renderPrint({
    glimpse(test_out())
  })

}

shinyApp(ui, server)

0 个答案:

没有答案