如何从已删除的Shiny输入小部件中清除“输入”列表

时间:2016-12-02 10:56:34

标签: shiny

My Shiny应用程序允许用户动态创建/删除输入。

然后循环遍历这些输入以获取值(例如,val1< - input [[“input_for_val1”]])。

我的问题是,已删除输入的值仍保留在闪亮的“输入”列表中。

我尝试使用removeUI或javascript(加上unbindAll / bindAll)删除html小部件,但它没有帮助,我想因为一旦值在“输入”列表中,它就会一直存在,直到UI上的某些内容更新它为止

我还尝试编辑'输入'列表本身(在服务器端)但是我收到错误,因为它是只读的。

如何清除已删除的闪亮输入小部件中的“输入”列表?

以下是一个小项目的代码来说明问题:

ui.R

-(IBAction)onBtnVibrateClicked:(id)sender {
    [self.view endEditing:YES];

    [myTimer invalidate];
    if(_txt_VibrationPerMinute.text.length == 0){
        _txt_VibrationPerMinute.text = @"10";
    }
    myTimer = [NSTimer scheduledTimerWithTimeInterval:60/[_txt_VibrationPerMinute.text intValue]
                                     target:self
                                   selector:@selector(targetMethod:)
                                   userInfo:nil
                                    repeats:YES];
}

- (IBAction)obBtnStopVibrationClicked:(id)sender {

    [myTimer invalidate];


}

-(void)targetMethod:(NSTimer *)timer {
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}

WWW /的script.js

library(shiny)

# Define UI for dataset viewer application
shinyUI(fluidPage(

  # Javascript to handle the add/remove action of inputs
  tags$head(tags$script(src="script.js")),

  # Container where the script adds the dynamic inputs
  tags$div(id = "container_for_dynamic_inputs"),

  # 'Add' button
  tags$button(onclick="addNewInputText()",
              class = "btn btn-default",
              "Add input"),

  # 'Remove' button
  tags$button(onclick="removeLastInputText()",
              class = "btn btn-default",
              "Remove input"),

  # Keeps tracks of the number of inputs (see server.R) 
  verbatimTextOutput("summary")

  )
)

server.R

var number_of_dynamic_inputs = 0;

var addNewInputText = function () {
  console.log("addNewInputText");

  var container_elt = document.getElementById("container_for_dynamic_inputs");

  Shiny.unbindAll(container_elt);

  var new_input = document.createElement("input");
  var id = "input_" + number_of_dynamic_inputs++;
  new_input.setAttribute("id", id);
  new_input.setAttribute("name", id);
  new_input.setAttribute("type", "text");
  new_input.setAttribute("value", id);

  container_elt.append(new_input);

  Shiny.bindAll(container_elt);

}


var removeLastInputText = function () {

  var container_elt = document.getElementById("container_for_dynamic_inputs");

  Shiny.unbindAll(container_elt);

  number_of_dynamic_inputs--;
  var id = "input_" + number_of_dynamic_inputs ;

  console.log("removeLastInputText: ", id);

  var elementToRemove = document.getElementById(id);

  container_elt.removeChild(elementToRemove);

  Shiny.bindAll(container_elt);

}

1 个答案:

答案 0 :(得分:0)

自上次检查以来,htmlwidgets上的文档已有所改进。这些特别是来自deanattali的非常有用的帖子。

http://www.htmlwidgets.org/develop_intro.html http://deanattali.com/blog/htmlwidgets-tips/#widget-to-r-data

所以我的解决方法是使用htmlwidgets使用自定义小部件。窗口小部件管理输入字段的创建/删除,并将输入作为一个json消息发送到服务器端。对于Shiny,这意味着只有一个输入......不再有问题。

相关问题