2013-11-26 36 views
2

我有兩種不同的動作形式。第一個是上傳文件,第二個是舉例。當我單擊其中一個應用程序時,它會執行某些操作,但服務器會保存點擊信息並保持不變,直到我點擊另一個按鈕。例如,如果我不點擊上傳按鈕而沒有選擇文件,它什麼也不做,但是如果我選擇一個文件,服務器就會上傳文件並開始處理它,而不點擊上傳按鈕,因爲服務器已經保存了過去的點擊。我想知道是否可以重置每次點擊的價值。函數observe()和reactiveValues()如何工作?

的Index.html

<form class="span12 menu-med-upload"> 
<div class="row-fluid"> 
    <h3>Upload File .fasta</h3> 
    <div class="custom-input-file btn btn-inverse"> 
    <input type="file" size="1" name="fileFasta" id="fileFasta" class="input-file" /> 
    Select File 
    </div> 
    <img src="/static/img/check.png" class = "custom-input-check"> 
    <div class="span12"></div> 
    <textarea class = "span12" rows = "10" style="resize: none;" id="textAreaFasta"> 
    </textarea> 
</div> 
<button id="uploadFasta" type="button" class="btn btn-inverse action-button" >Upload File</button> 
<button id="exampleFasta" type="button" class="btn btn-inverse action-button" >Example</button> 
</form> 

Server.R

shinyServer(function(input, output, session) { 

# Create a reactiveValues object, to let us use settable reactive values 
values <- reactiveValues() 
# To start out, lastAction == NULL, meaning nothing clicked yet 
values$lastAction <- NULL 
# An observe block for each button, to record that the action happened 
observe({ 
    if (input$exampleFasta != 0) { 
    values$lastAction <- 'example' 
    } 
}) 
observe({ 
    if (input$uploadFasta != 0) { 
    values$lastAction <- 'upload' 
    }) 
}) 

# Then you can use values$lastAction in reactive expressions, outputs, etc. 
output$table <- renderText({ 
    if (is.null(values$lastAction)) 
    return(NULL) 
    if (identical(values$lastAction, 'upload')) 
    return(myRenderTable(matrixProtein(), "table", nameFile)) 
    if (identical(values$lastAction, 'example')) 
    return(myRenderTable(matrixProteinExample(), "table", "")) 
    stop("Unexpected value for lastAction: ", values$lastAction) 
}) 
}) 

注:鄭元暢發server.R的代碼,我抄在這個例子中一道shiny Change data input of buttons

+0

我不明白這個問題,有人可以編輯它更清楚嗎? –

回答

-4

你想隔離。因此,將此輸入到R控制檯中

?shiny::isolate 
+3

這個答案相當於一個鏈接;它只是指向R的幫助(實際上我可以給出一個具有相同信息的URL)。你應該擴大你的答案來解釋爲什麼使用孤立是對問題的回答。 –

4

如果沒有可重複的示例,此問題就更難回答。但是,我已經整合了一個獨立的示例並對其進行了修復,以便它能夠正常工作。代碼存在兩個問題。第一個,如@xiaodai所標識的(但沒有充分的解釋)是文件上傳元素不是來自被動環境的isolate d。這意味着,只要文件元素髮生更改,輸出也會改變。第二個問題是每個按鈕的代碼只在連續多次點擊時被調用一次,這意味着如果你沒有點擊例子,上傳不會發生(在修復isolate問題後)先按鈕。

現在它工作,因爲我相信OP想要的。

這裏的固定的index.html

<html> 
    <head> 
    <script src="shared/jquery.js" type="text/javascript"></script> 
    <script src="shared/shiny.js" type="text/javascript"></script> 
    <link rel="stylesheet" type="text/css" href="shared/shiny.css"/> 
</head> 
<body> 
<form class="span12 menu-med-upload"> 
<div class="row-fluid"> 
    <h3>Upload File .fasta</h3> 
    <div class="custom-input-file btn btn-inverse"> 
    <input type="file" size="1" name="fileFasta" id="fileFasta" class="input-file" /> 
    Select File 
    </div> 
</div> 
<button id="uploadFasta" type="button" class="btn btn-inverse action-button" >Upload File</button> 
<button id="exampleFasta" type="button" class="btn btn-inverse action-button" >Example</button> 
</form> 
    <div id="table" class="shiny-html-output"></div> 
</body> 
</html> 

而這裏的固定server.R

library("xtable") 
library("Biostrings") 

myRenderTable <- function(data, dataType, nameFile) { 
    print(xtable(data), type = "html", print.results = FALSE) 
} 

matrixProtein <- function(fastaFile) { 
    fasta <- readDNAStringSet(fastaFile) 
    alphabetFrequency(translate(fasta)) 
} 

matrixProteinExample <- function() { 
    matrixProtein(system.file("extdata", "someORF.fa", package="Biostrings")) 
} 

shinyServer(function(input, output, session) { 

    # Create a reactiveValues object, to let us use settable reactive values 
    values <- reactiveValues() 
    # To start out, lastAction == NULL, meaning nothing clicked yet 
    values$lastAction <- NULL 
    # An observe block for each button, to record that the action happened 
    # Note setting the lastAction to NULL and then to a string ensures the output 
    # is generated each time the button is clicked which is necessary for the upload button 
    observeEvent(input$exampleFasta, { 
    values$lastAction <- "example" 
    }) 
    observeEvent(input$uploadFasta, { 
    values$lastAction <- NULL 
    values$lastAction <- "upload" 
    }) 

    nameFile <- "Random" 

    # Then you can use values$lastAction in reactive expressions, outputs, etc. 
    output$table <- renderText({ 
    if (is.null(values$lastAction)) 
     return(NULL) 
    if (identical(values$lastAction, 'upload')) { 
     if (!is.null(isolate(input$fileFasta))) { 
      return(myRenderTable(isolate(matrixProtein(input$fileFasta$datapath)), "table", nameFile)) 
     } else { 
      stop("No file provided") 
     } 
    } else if (identical(values$lastAction, 'example')) 
     return(myRenderTable(matrixProteinExample(), "table", "")) 
    stop("Unexpected value for lastAction: ", values$lastAction) 
    }) 
}) 

注意,這依賴於Bioconductor的包Biostrings和CRAN包xtable。我不知道原始函數做了什麼,但是這個代碼需要一個FASTA格式的文件,讀入序列,翻譯成蛋白質序列,然後給出一個字母表頻率表。

另請注意我不知道myRenderTable的其他參數是什麼意思,所以我忽略了它們。

+0

我已經有點解決了我自己的問題,因爲我是那個把賞金的人,但是看起來你的回答最能解決問題。 +1。 – VermillionAzure