2013-11-26 3 views
2

두 가지 다른 동작이있는 양식이 있습니다. 첫 번째는 파일을 업로드하는 것이고 두 번째는 예제를위한 것입니다. 이 중 하나를 클릭하면 앱에서 어떤 작업을 수행하지만 서버는 클릭 정보를 저장하고 다른 버튼을 클릭 할 때까지 변경 사항을 저장하지 않습니다.함수 observe() 및 reactValues ​​()는 어떻게 작동합니까?

예를 들어 파일을 선택하지 않고 업로드 버튼을 클릭하면 아무 것도 실행되지 않지만 파일을 선택하면 서버는 파일을 업로드하고 서버가 저장 했으므로 업로드 버튼을 클릭하지 않고 파일을 처리하기 시작합니다. 과거의 클릭. 각 클릭에 대해 값을 재설정 할 수 있는지 알고 싶습니다.

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이 아니었다는 것입니다. 이는 파일 요소가 변경 될 때마다 출력도 변경되었음을 의미합니다. 두 번째 문제는 각 버튼의 코드가 여러 번 연속 클릭 될 때 한 번만 호출된다는 것이 었습니다. 이는 예를 클릭하지 않은 경우 업로드가 발생하지 않았 음을 의미합니다 (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이이 Bioconductor 패키지 Biostrings과 CRAN 패키지에 따라 달라

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

xtable. 원래 함수가 무엇인지는 모르지만이 코드는 FASTA 형식의 파일을 가져 와서 시퀀스를 읽고, 단백질 시퀀스로 변환 한 다음 알파벳 빈도 테이블을 제공합니다.

또한 유의하십시오. myRenderTable에 대한 다른 매개 변수의 의미는 무엇인지 모르기 때문에 무시했습니다.

+0

나는 현상금을 넣었던 것처럼 내 자신의 문제를 이미 다소 해결했지만, 당신의 대답은 문제를 가장 잘 해결 한 것처럼 보입니다. +1. – VermillionAzure