私はselectizeInputウィジェットをコントロールにロードするためにリアクティブ値を使いたいと思います。私は単純な光沢のあるアプリでこれを行うことができますが、モジュールで再構成しようとすると、それを複製することができないようです。光沢のあるモジュールでupdateSelectizeInput
次のコードでは、入力ウィジェットとボタン付きの単純なアプリケーションがあります。ボタンを押した後、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)
ボタンを押した後、「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)
私は私が間違っているIDを使用している場合があります信じて。どんな提案も大歓迎です!ありがとう
なぜモジュールを使用していますか?あなたはちょうどサーバー/ uiの中にすべてを入れることはできません –
モジュールは、特にあなたが多くのidsで作業しているときに、非常に面白いことがあります –