2017-11-28 3 views
0

Ich möchte einen reaktiven Wert verwenden, um ein selectizeInput-Widget zu laden. Ich kann das mit einer einfachen, glänzenden App machen, aber ich kann es anscheinend nicht replizieren, wenn ich versuche, es in einem Modul zu reorganisieren.updateSelectizeInput in einem glänzenden Modul

Im folgenden Code habe ich eine einfache App mit einem Eingabe-Widget und einer Schaltfläche. Nach dem Drücken der Taste möchte ich, dass das Widget selectizeInput() mit den in der reaktiven Variablen gespeicherten Werten aktualisiert wird.

nach dem Drücken der Taste wird das Widget korrekt mit "c" aktualisiert, und die Auswahlmöglichkeiten werden korrekt importiert. Wenn ich jedoch versuche, es als ein glänzendes Modul zu schreiben, funktioniert die Schaltfläche nicht.

Bibliothek (glänzend)

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

Ich glaube, ich könnte die falsche ID verwenden. Jeder Vorschlag ist sehr willkommen! Danke

+0

Warum verwenden Sie Module? Kannst du nicht einfach alles in den Server stellen? –

+0

Module können manchmal sehr hekful sein, besonders wenn du mit vielen IDs arbeitest –

Antwort

0

Diese Lösung ist näher an, was ich suchte. Es erlaubt immer noch, den Knopf und das Eingabe-Widget in verschiedenen Modulen zu halten (d. H. Den Knopf auf einer anderen Registerkarte zum Beispiel zu haben).

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

Sie möchten die selectizeInput und actionButton in der gleichen UI-Funktion haben. Andernfalls haben Sie für jede von ihnen unterschiedliche Namespaces. In diesem Fall brauchen Sie auch nicht die ns Funktion im Server Teil. Sie müssen nur, dass, wenn Sie dynamische UI-Rendering Hier

Objekte arbeitet Version des Codes

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

Danke für deinen Vorschlag! –

Verwandte Themen