从textInput搜索Shiny

时间:2017-09-18 22:51:57

标签: r shiny shiny-server rhandsontable

我已经和Handsontable in Shiny一起工作了几天,我陷入了一个非常愚蠢的问题,但我对如何解决这个问题并不是很了解。

我有一个Handsontable,它有一个自定义功能,允许搜索并且有效。它有效,但不够直观,因为您必须右键单击表格以弹出搜索选项。

因此,我决定让textInput具有相同的功能但是更漂亮。我知道它应该与输入变量的observeEvent相关(输入$ searchId),但由于我缺乏Shiny和Handsontable的经验,我不知道该怎么做。

这是来自server.R的代码,它打印表格并具有允许用户搜索的自定义功能。

output$hot <-renderRHandsontable({rhandsontable(Dataset(),height = 600)%>%   
hot_table( columnSorting = TRUE,highlightCol = TRUE, highlightRow = TRUE, search = TRUE) %>% 
hot_context_menu(
  customOpts = list(
    search = list(name = "Search",
                  callback = htmlwidgets::JS(
                    "function (key, options) {
                     var aux = document.getElementById('searchId').value;
                     var srch = prompt(Search);

                     this.search.query(srch);
                     this.render();
                   }")))) })

我想要归档相同的结果,但无需右键单击表格并创建提示。

非常感谢你,

1 个答案:

答案 0 :(得分:0)

好吧,我已经能够解决我的问题了。我受this post的启发 然后我得到了类似的东西:

js_search <- "
$(document).ready(setTimeout(function() {
  document.getElementById('searchId').onchange = function(e){
    var hot_instance = HTMLWidgets.getInstance(hot).hot
    console.log('hola')
    var aux = document.getElementById('searchId').value;
    hot_instance.search.query(aux);
    hot_instance.render();
  }
}))
"

必须包含在您的ui.R中tags$head(tags$script(HTML(js_search)))

我遇到的所有问题是我不知道如何从我之前在服务器端的自定义操作中获取“this”。一旦你知道那是hot_instance。我觉得很热闹是我桌子的名字。