【问题标题】:need to input data into a textbox in shiny需要在闪亮的文本框中输入数据
【发布时间】: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


【解决方案1】:

如果要将if(sw==0{...} 的执行链接到'input$text2,那么isolate 是要走的路。您需要将if .. 包装在isolate 中,并将其放在观察者中以触发它。所以这应该工作

observe({
   fileNameData<-renderText({ input$text2 })
   fileName<<-paste(fileNameData(),sep=".","txt")

   isolate ({
      if (sw==0){
           #some operations here
           cat(someData,file=fileName,append=TRUE,sep=",") #save data
       }
    })
 })

现在isolate() 位于观察者内部,仅应在input$text2 更改时触发。

【讨论】:

  • 感谢@JohnPaul,但是不使用隔离就没有其他方法了吗?问题是一直在等待输入文本更改,我只需要捕获一次更改
猜你喜欢
  • 2018-11-14
  • 2015-12-26
  • 2014-01-05
  • 2016-03-23
  • 2021-10-31
  • 1970-01-01
  • 1970-01-01
  • 2021-08-10
相关资源
最近更新 更多