updateSelectizeInput 在一个闪亮的模块中

Ale*_*ida 5 r shiny selectize.js

我想使用反应值来加载控制 selectizeInput 小部件。我可以使用一个简单的闪亮应用程序来做到这一点,但是当我尝试在模块中重新组织它时,我似乎无法复制它。

在下面的代码中,我有一个带有输入小部件和按钮的简单应用程序。按下按钮后,我希望使用selectizeInput()反应变量中存储的值更新小部件。

library(shiny)

# GLOBAL VAR TO BE LOADED after pressing the button
# -------------
STORED <- reactiveValues(choices = c("a", "b", "c")
                         , selected = c("c"))

# UI
# -------------
ui <- fixedPage(
  wellPanel(
    # input widget
    selectInput("widget1"
                , "label"
                ,choices = c("WRONG A","WRONG B","WRONG C")
                ,selected="WRONG A"
                )

    # update button
    , actionButton("plotBtn"
                   , "update"
                   , class = "btn-primary")
  )
)

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

  observeEvent(input$plotBtn,{
    message("update button was pressed")

    updateSelectInput(session
                      ,"widget1"
                      ,choices = STORED$choices
                      , selected = STORED$selected
    )
  })
}

# APP
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)

按下按钮后,小部件将正确更新并选择“c”,并且选项将正确导入。但是,当我尝试将其编写为闪亮模块时,该按钮不起作用。

图书馆(闪亮)

# INIT VARS TO BE LOADED AFTER PRESSING THE BUTTON
# ------------------------------------------------------------------------------
STORED <- reactiveValues(choices = c("a", "b", "c")
                         , selected = c("c"))

# MODULE SELECTIZEINPUT
# ------------------------------------------------------------------------------
input_widgetUI <- function(id) {
  ns <- NS(id)

  selectizeInput(ns("input_widget")
                 , label = "label"
                 , choices = c("WRONG A","WRONG B","WRONG C")
                 , selected = "WRONG A"
  )

}

# MODULE BUTTON
# ------------------------------------------------------------------------------
plotBtnUI <- function(id){

  ns <- NS(id)

  fluidPage(actionButton(ns("plotBtn"), "update", class = "btn-primary"))
}

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

  ns <- session$ns

  print("Loading plotBtn()")

  observeEvent(input$plotBtn, {

    message("update button was pressed")

    updateSelectizeInput(session
                         , ns("widget1")
                         , choices = STORED$choices
                         , selected= STORED$selected
                         )

  })
}


# #############################################################################
# SHINY APP
# #############################################################################
ui <- fixedPage(
  wellPanel(
    input_widgetUI("widget1")
    , plotBtnUI("button")
  )
)

server <- function(input, output, session) {
  callModule(plotBtn, "button")
}


shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)

我相信我可能使用了错误的 ID。任何建议都非常受欢迎!谢谢

Ale*_*ida 2

这个解决方案更接近我所寻找的。它仍然允许将按钮和输入小部件保留在不同的模块中(例如,将按钮放在不同的选项卡上)。

library(shiny)
options(shiny.reactlog=TRUE)

# INIT GLOBAL VARS
# --------------------------------------------------------------------
STORED <- reactiveValues(choices = c("WRONG A","WRONG B","WRONG C")
                         , selected = "WRONG A")

# MODULE SELECTIZEINPUT
#----------------------------------------------------------------------
input_widgetUI <- function(id) {

  ns <- NS(id)

  tagList(
    selectizeInput(ns("input_widget")
                   , label = "label"
                   , choices = NULL
                   , selected = NULL
    )
  )
}

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

  #ns <- session$ns

  observe({

    updateSelectizeInput(session
                        , "input_widget"
                        , choices = STORED$choices
                        , selected= STORED$selected
                        , server=TRUE
                        )
  })

}

# MODULE BUTTON
# ---------------------------------------------------------------------
plotBtnUI <- function(id){

  ns <- NS(id)

  fluidPage(actionButton(ns("plotBtn"), "update", class = "btn-primary"))
}

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

   #ns <- session$ns

   print("Loading plotBtn()")

   observeEvent(input$plotBtn, {

     message("update button was pressed")

     # VARS TO BE LOADED AFTER PRESSING THE BUTTON
     STORED$choices <- c("a", "b", "c")
     STORED$selected <- c("c")

   })
}


# ######################################################################
# SHINY APP
# ######################################################################
ui <- fixedPage(
  tagList(
      input_widgetUI("widget1")
    , plotBtnUI("button")
  )
)

server <- function(input, output, session) {
  callModule(input_widgetSERVER, "widget1")
  callModule(plotBtnSERVER, "button")
}
# launch the app
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)