use*_*097 3 r shiny shiny-server
我有一个包含两个不同动作的表单。第一个是上传文件,第二个是示例。当我单击其中的一个时,我的应用程序会执行某些操作,但是服务器会保存单击的信息,并且直到我单击另一个按钮之前都没有更改。
例如,如果我单击上载按钮而不选择文件,则它什么都不做,但是如果我选择了文件,则服务器将上载文件并开始处理该文件而无需单击上载按钮,因为服务器已保存了过去的点击。我想知道是否可以重置每次点击的值。
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>
Run Code Online (Sandbox Code Playgroud)
服务器
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)
})
})
Run Code Online (Sandbox Code Playgroud)
注意:Joe Cheng编写了server.R的代码,在此示例中,我将其复制为闪亮的按钮更改数据输入
没有可重复的例子,这个问题就很难回答。但是,我整理了一个独立的示例,并对其进行了修复,使其可以正常工作。目前的代码存在两个问题。首先,由@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>
Run Code Online (Sandbox Code Playgroud)
这是固定的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)
})
})
Run Code Online (Sandbox Code Playgroud)
请注意,这取决于Bioconductor封装Biostrings和CRAN封装xtable。我不知道原始功能是做什么的,但是这段代码采用了FASTA格式的文件,读取了序列,翻译为蛋白质序列,然后给出了字母频率表。
另请注意,我不知道其他参数的myRenderTable含义,因此我忽略了它们。