2017-07-18 4 views
0

名前空間の外にdivを表示/非表示するには、Shinyモジュールが必要です。 div idをモジュールサーバー関数に渡してみましたが、shinyjsを使って表示/非表示にしていましたが、動作しません。私はエラーを取得していない、それは表示されない/ divを非表示にします。光沢のあるモジュールへのアクセスがネームスペースの外側にある

Shinyモジュールのドキュメントによれば、モジュールは名前空間の外にある出力にアクセスできません。ただし、ドキュメントは、モジュールが反応空間を使用して名前空間外の入力にアクセスする方法を提供します。

Shinyモジュールが名前空間外の出力にアクセスする方法があるかどうか知りませんか?ここで

は私がやろうとしているものです:

### ui.R ### 
header <- dashboardHeader(
    title = a(href = 'http://google.com') 
) 

dashboardPage(
    skin = 'black', 
    header, 

    dashboardSidebar(
    sidebarMenu(id='tabs', 
      menuItem('Edit Existing Client', tabName = 'client-info') 
    )), 

    dashboardBody(
    useShinyjs(), 
    fluidRow(
     tabItems(
     tabItem(tabName = "client-info", 
       div(selectClientModuleUI("clientinfons")), 
       div(id='editclientinfo', uiOutput('editclientstuff')) 
     ) 
    ) 
    ) 
) 
) 

### server.R ### 
shinyServer(function(session,input, output) { 

    output$editclientstuff <- renderUI({ 
    div(
     fluidRow(
     column(6, 
      textInput('editname', "Display name", value ='Testing name') 
     ), 
     column(6, 
       numericInput('editastart','Start', value ='3') 
     ) 
    ) 
    ) 
    }) 


    callModule(selectClientModule, 'clientinfons', 'editclientinfo') 
    shinyjs::hide(id='editclientstuff') 
}) 

### in global.R ### 
selectClientModuleUI <- function(id){ 
    ns <- NS(id) 

    clientlist = c(0, 1, 2) 
    names(clientlist) = c('Choose client', 'Fred', 'Kim') 

    div( 
    selectInput(ns('selectclient'), 'Select client to edit', choices = clientlist, selected = NULL, multiple = FALSE) 
) 
} 

selectClientModule <- function(input, output, session, divtoshow = ''){ 
    observeEvent(input$selectclient, { 
    if (!is.null(input$selectclient) && input$selectclient > 0){ 
     print(paste0("showing ", divtoshow)) 
     shinyjs::show(divtoshow) 
    } 
    }) 

} 

答えて

0

モジュールへの反応として(ない反応性の値として)値を与えることで可能であること。モジュールのリアクティブバリューを変更して、モジュールからのリアクティブをアプリケーションに戻すことができます(メモ、リセット自体は値ではなく戻ります)。次のアプリは、メインアプリ内の 'divtoshow'をモジュール内から切り替えるものです。何も選択されていない場合は、それ以外の場合は示されています、隠されたのです(注、私はそれは、スタンドアローンのアプリとして働いているので、あなたは少しのコードを調整する):

library(shinydashboard) 
library(shinyjs) 


# Module 
selectClientModuleUI <- function(id){ 
    ns <- NS(id) 

    clientlist = c(0, 1, 2) 
    names(clientlist) = c('Choose client', 'Fred', 'Kim') 

    div( 
    selectInput(ns('selectclient'), 'Select client to edit', choices = clientlist, selected = NULL, multiple = FALSE) 
) 
} 

selectClientModule <- function(input, output, session, divtoshow){ 

    observeEvent(input$selectclient, { 
    if (input$selectclient > 0){ 
     print(paste0("showing editclientinfo")) 

     divtoshow("editclientinfo") # set the div to show to "editclientinfo", this will be visible outside the module 
    }else{ 
     divtoshow("") # set the div to show to "", if nothing was chosen 
    } 
    }) 

    # return the div to show as reactive to the main app 
    return(divtoshow) 
} 


# Main App 
ui <- shinyUI(
    dashboardPage(
    skin = 'black', 
    dashboardHeader(
     title = a(href = 'http://google.com') 
    ), 
    dashboardSidebar(
     sidebarMenu(id='tabs', 
        menuItem('Edit Existing Client', tabName = 'client-info') 
    )), 

    dashboardBody(
     useShinyjs(), 
     fluidRow(
     tabItems(
      tabItem(tabName = "client-info", 
        div(selectClientModuleUI("clientinfons")), 
        div(id='editclientinfo', uiOutput('editclientstuff')) 
     ) 
     ) 
    ) 
    ) 
)) 

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

    output$editclientstuff <- renderUI({ 
    div(
     fluidRow(
     column(6, 
       textInput('editname', "Display name", value ='Testing name') 
     ), 
     column(6, 
       numericInput('editastart','Start', value ='3') 
     ) 
    ) 
    ) 
    }) 

    # store the div to show in a reactive 
    divtoshow <- reactiveVal('') 

    # divtoshow can be changed in side this module, so it's a return value 
    divtoshow <- callModule(selectClientModule, 'clientinfons', divtoshow) 

    # observe the value of divtoshow and toggle the corresponding div 
    observeEvent(divtoshow(), { 
     if(divtoshow() == "editclientinfo"){ 
     shinyjs::show("editclientinfo") 
     }else{ 
     shinyjs::hide("editclientinfo") 
     } 

    }) 
}) 

shinyApp(ui, server) 
+0

素敵な答えを。私は大きなコードをモジュールに分割しています。ワンダーリングサイドバーパネルのドロップダウン入力と条件付きパネルとダッシュボード本体のアウトプットプットを持つことは可能ですか?私が遭遇したすべての例には、入力とプロット/テーブル出力の両方が本体にあります。あなたが私に指示できる例はありますか? – user5249203

+0

すべての必要な要素をサイドバーに入れるだけです。 – shosaco

関連する問題