2013-10-03 53 views
4

我使用R 3.0.1與Shiny 0.7.0創建一個帶有HTML UI的閃亮網頁。使用Javascript OnLoad限制HTML UI的閃亮?

看來我已經偶然發現了什麼似乎是Shiny有關body元素的OnLoad()事件的限制或錯誤。

由於Shiny沒有爲多個HTML頁面準備,如Joe Cheng所述here我試圖使用jQuery來代替在需要時顯示和隱藏divs

如果我在OnLoad事件中隱藏div,則此作品合理地運行良好,除了。在那種情況下,似乎reactive input components不再被Shiny檢測到。

下面你可以看到這個測試用例:

Server.R

library(shiny) 

shinyServer(function(input, output) { 

    output$caption <- renderText({input$myInput}) 

}) 

的index.html

<head> 
    <script src="shared/jquery.js" type="text/javascript"></script> 
    <script src="shared/shiny.js" type="text/javascript"></script> 
    <script type="text/javascript" src="js/library.js"></script>  
</head> 

<body onload="initForm();"> 


    <div id="dMenu"> 

     <a href="#" onclick="showFoo();">Show foo div</a> 
     <br> 
     <a href="#" onclick="showBar();">Show bar div</a> 
    </div> 

    <!-- some random div --> 
    <div id="foodiv">This is the Foo div</div> 

    <!-- div with reactive input -->     
    <div id="bardiv"> 
     This is the Bar div 
     <br> 
     <input type="text" id="myInput" class ="shiny-bound-input" name="myInput" autocomplete="off"> 
     <div id="caption" class="shiny-text-output shiny-bound-output"></div> 
    </div> 

</body> 

library.js

function showFoo(){ 
    $('#foodiv').show();  
    $('#bardiv').hide(); 
} 


function showBar(){ 
    $('#foodiv').hide(); 
    $('#bardiv').show();  
} 


function initForm(){ 
    /*showFoo(); */ 
} 

如果你作爲有光澤會以變化作出反應的myInput組件,並將其寫入到caption組件使用此代碼。另一方面,如果您註釋掉initForm()中的代碼,它將停止響應您寫入myInput的任何內容。

我用Firefox 23.0.1測試了這個。

有誰知道我是否做錯了什麼?

回答

6

看到溫斯頓發表的shiny mailing list

你可以寫

shinyServer(function(input, output) { 
    output$caption <- renderText({input$myInput}) 
    outputOptions(output, 'caption', suspendWhenHidden=FALSE) 
}) 

確保隱時現的元素仍然更新。

+0

+1,解決了這個問題,但我仍然在問自己,爲什麼只有在調用'OnLoad'事件時纔會發生這種情況。如果我不叫它,只是隱藏並再次顯示元素'閃亮'仍然對輸入作出反應。 –

+0

我同意。我希望您不介意,但我將此問題複製到https://groups.google.com/forum/#!topic/shiny-discuss/N68EFR_uh_c上的閃亮郵件列表。 –

+0

這完全沒問題。我會不時在Shiny郵件列表中檢查您的帖子,看看他們是否已經回答。 :-) –