2017-11-28 5 views
0

私は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を使用している場合があります信じて。どんな提案も大歓迎です!ありがとう

+0

なぜモジュールを使用していますか?あなたはちょうどサーバー/ uiの中にすべてを入れることはできません –

+0

モジュールは、特にあなたが多くのidsで作業しているときに、非常に面白いことがあります –

答えて

0

を進めています。ボタンと入力ウィジェットを別々のモジュールに保つことができます(つまり、別のタブにボタンを置くなど)。

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) 
0

同じUI機能でselectizeInput actionButtonを使いたいと思っています。それ以外の場合は、それぞれの名前空間が異なります。この場合、サーバ部分にはns機能は必要ありません。あなただけが動的なUIをレンダリングしているとき、ここで

オブジェクトことを必要とするこのソリューションは、私が探していたものに近いあなたのコードのバージョン

STORED <- reactiveValues(choices = c("a", "b", "c") 
         , selected = c("c")) 

# MODULE SELECTIZEINPUT 
# ------------------------------------------------------------------------------ 
input_widgetUI <- function(id) { 
    ns <- NS(id) 
    tagList(
    selectizeInput(ns("input_widget") 
       , label = "label" 
       , choices = c("WRONG A","WRONG B","WRONG C") 
       , selected = "WRONG A" 
), 
    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 = getDefaultReactiveDomain() 
         , inputId = "input_widget" 
         , choices = STORED$choices 
         , selected= STORED$selected 
    ) 

    }) 
} 


# ############################################################################# 
# SHINY APP 
# ############################################################################# 
ui <- fixedPage(
    wellPanel(
    input_widgetUI("widget1") 
) 
) 
server <- function(input, output, session) { 
    callModule(plotBtn, "widget1") 
} 
shinyApp(ui, server) 
+0

あなたの提案をありがとう! –

関連する問題