我有兩種不同的動作形式。第一個是上傳文件,第二個是舉例。當我單擊其中一個應用程序時,它會執行某些操作,但服務器會保存點擊信息並保持不變,直到我點擊另一個按鈕。例如,如果我不點擊上傳按鈕而沒有選擇文件,它什麼也不做,但是如果我選擇一個文件,服務器就會上傳文件並開始處理它,而不點擊上傳按鈕,因爲服務器已經保存了過去的點擊。我想知道是否可以重置每次點擊的價值。函數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
我不明白這個問題,有人可以編輯它更清楚嗎? –