【发布时间】:2015-05-15 01:35:52
【问题描述】:
我在 Shiny 中制作了一个简单的系统,它包含一个文本框和它下面的许多复选框。我希望用户在文本框中输入他的 ID 并自动生成一个文本文件,该文件的名称与用户 ID 相同。我的意思是如果用户输入“1234Q”作为他的ID,那么系统将生成一个名为“1234Q”的文件。到目前为止,我已经完成了以下工作:
#part of the ui.R file
library(shiny)
shinyUI(fluidPage(
titlePanel(h2("Title")),
###textbox for entering person data
textInput("text2", label = h3("Personal ID"), value = ""),
verbatimTextOutput("textBoxvalue"),
hr(),
mainPanel(
textOutput("text1"),
#some checkboxes here
actionButton("action", label = "Next")
)
))
而我的server.R大致如下:
sw=0
shinyServer(function(input, output, session) {
output$textBoxvalue <- renderPrint({ input$text2 })
observe({
fileNameData<-renderText({ input$text2 })
fileName<<-paste(fileNameData(),sep=".","txt")
})
if (sw==0){
#some operations here
cat(someData,file=fileName,append=TRUE,sep=",") #save data
我遇到的问题是,在用户输入他的 ID 后,我将该数据放入 fileName 变量中,我打算用它来保存我将在 if(sw==0) 部分之后获得的数据,但什么也没发生。对于我所看到的程序绕过观察部分并直接评估 if(sw==0) 部分。我尝试过使用隔离,但一点运气都没有。我该怎么做才能解决这个问题?
更新:
我做了以下类似的事情:
shinyServer(function(input, output, session) {
###data received from the textbox, we need to save a file with this name only once
####observe
observe({
fileNameData<-renderText({ input$text2 })
#print(fileNameData())
fileName<<-paste(fileNameData(),sep=".","txt")
})
isolate({
###end of data received from the textbox
if (sw==0){
这大致可以正常工作,但我缺少要输入的第一个数据,因为我的文件名是空的。
PD。总之,我只需要用户输入到文本字段中的文本值,然后创建一个具有相同名称的文件。该过程应该只进行一次。
【问题讨论】:
-
if 语句不应该在一些反应表达式中还是在观察中?
-
不,我的程序的所有其他部分都运行良好
标签: r shiny shiny-server