3

我有一个包含两种不同动作的表格。第一个是上传文件,第二个是示例。当我单击其中一个时,我的应用程序会执行某些操作,但服务器会保存单击的信息,并且在我单击另一个按钮之前不会更改。

例如,如果我点击上传按钮而不选择文件,它什么也不做,但如果我选择一个文件,服务器会上传文件并开始处理它而不点击上传按钮,因为服务器已经保存了过去的点击。我想知道是否可以重置每次点击的值。

索引.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>

服务器.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)
 })
})

注意:Joe Cheng 编写了 server.R 的代码,我复制到这个例子中的工作来闪亮更改按钮的数据输入

4

2 回答 2

5

如果没有可重复的例子,这个问题就更难回答了。但是,我整理了一个独立的示例并对其进行了修复以使其正常工作。代码存在两个问题。第一个,由@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

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意思,所以我忽略了它们。

于 2015-06-25T22:03:13.083 回答
-4

你要隔离。所以在 R 控制台中输入这个

?shiny::isolate
于 2013-11-29T17:01:00.953 回答