R闪亮:在data.table中更新选择输入值 [英] R shiny: update select input values in data.table

查看:421
本文介绍了R闪亮:在data.table中更新选择输入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有renderDataTable,其中包含选择的输入和按钮。我想在datatable中更新selectInput,点击相应行中的保存按钮。我怎样才能做到这一点?在寻找解决方案时,我发现如果你重新安装了表,输入将不起作用,除非你添加一些额外的代码来取消绑定。然而,我是新的闪亮和使用js选项,所以我会感谢任何提示/解决方案。

 库(闪亮) 
库(DT)
runApp(list(
ui = basicPage(
h2('the mtcars data'))
DT :: dataTableOutput('mytable' ,
h2(Selected),
tableOutput(checked)
),

server = function(input,output){
#帮助函数使复选框
shinyInput = function(FUN,len,id,...){
inputs = character(len)
for(i in seq_len(len)){
输入[i] = as.character(FUN(paste0(id,i),...))
}
输入
}
#datatable with checkbox
输出$ mytable = DT :: renderDataTable({
data.frame(mtcars,Rating = shinyInput(selectInput,nrow(mtcars),selecter _,label = NULL,
choices = 1:5 ,width =60px),
Save = shinyInput(actionButton,nrow(mtcars),' button_',
label ='Save',onclick ='Shiny.onInputChange(\select_button\,this.id)'))
},selection ='none',server = FALSE ,escape = FALSE,options = list(
paging = TRUE,
preDrawCallback = JS('function(){
Shiny.unbindAll(this.api() )); }'),
drawCallback = JS('function(){
Shiny.bindAll(this.api()。table()。node());}')
))
#helper函数读取复选框
shinyValue = function(id,len){
unlist(lapply(seq_len(len)),function(i){
value = input [[paste0 (id,i)]]
if(is.null(value))NA else value
}))
}
#输出读取复选框
输出$ checked < - renderTable({
data.frame(selected = shinyValue(selecter _,nrow(mtcars)))
})
}
))


解决方案

嘿,所以我不完全明白你的问题,但希望这有帮助。这个应用程序不完美,但应该做你想要的:

 库(闪亮)
库(DT)
runApp(
ui = basicPage(
标签$脚本(
HTML(
Shiny.addCustomMessageHandler('unbind-DT',function(id)
Shiny.unbindAll($('#'+ id).find('table')。DataTable()。table()。node());
})

),
h2('数据'),
selectInput(myData,选择数据集,c(mtcars,iris),mtcars),
DT :: dataTableOutput('mytable'),
h2(Selected),
tableOutput(checked)
),

server = function (输入,输出,会话){
数据集< - reactive({
会话$ sendCustomMessage(unbind-DT,mytable)
get(input $ myData)
})

#帮助函数使复选框
shinyInput = function(FUN,len,id,...){
inputs = character(len)
for(i in seq_len(len)){
inputs [i] = as.character(FUN(paste0 (id,i),...))
}
输入
}
#datatable with checkbox
output $ mytable = DT :: renderDataTable({
data.frame(
dataset(),
Rating = shinyInput(
selectInput,
nrow(dataset()),
selecter_,
选择= 1:5,
width =60px,
label = NULL
),
Save = shinyInput(actionButton,nrow(dataset()),'button_' ,
label ='Save')

},selection ='none',server = FALSE,escape = FALSE,options = list(
dom =ti
paging = TRUE,
preDrawCallback = JS(
'function(){
Shiny.unbindAll(this.api()。 ''
),
drawCallback = JS('function(){
Shiny.bindAll(this.api()。table()。node());}')
$)
#helper function for reading checkbox
shinyValue = function(id,len){
unlist(lapply(seq_len(len)),function(i){
value =输入[[paste0(id,i)]]
if(is.null(value))
NA
else
value
}))

#输出读取复选框
输出$ checked< - renderTable({
data.frame(selected = shinyValue(selecter_,nrow(mtcars)))
})

lapply(1:150,function(i){
observeEvent(input [[paste0(button_,i)]],{
updateSelectInput(session,
paste0(selecter_,i),
selected = 5,
label = NULL)
})
})

}

))


I have renderDataTable with select Inputs and buttons inside. I want to update selectInput inside datatable after click on 'Save' button in appropriate row. How can I do that? During searching for a solution I found that "if you rerender the table, the inputs won't work unless you add some extra code to unbind". However I am new in shiny and using js options, so I would be grateful for any hints/solutions.

library(shiny)
library(DT) 
runApp(list(
  ui = basicPage(
    h2('The mtcars data'),
    DT::dataTableOutput('mytable'),
    h2("Selected"),
    tableOutput("checked")
  ),

  server = function(input, output) {
    # helper function for making checkbox
    shinyInput = function(FUN, len, id, ...) { 
      inputs = character(len) 
      for (i in seq_len(len)) { 
        inputs[i] = as.character(FUN(paste0(id, i), ...)) 
      } 
      inputs 
    } 
    # datatable with checkbox
    output$mytable = DT::renderDataTable({
      data.frame(mtcars,Rating=shinyInput(selectInput,nrow(mtcars),"selecter_",label=NULL,
                                          choices=1:5, width="60px"),
                 Save = shinyInput(actionButton, nrow(mtcars),'button_', 
                                         label = 'Save',onclick = 'Shiny.onInputChange(\"select_button\",  this.id)' ))
    }, selection='none',server = FALSE, escape = FALSE, options = list( 
      paging=TRUE,
      preDrawCallback = JS('function() { 
                           Shiny.unbindAll(this.api().table().node()); }'), 
      drawCallback = JS('function() { 
                        Shiny.bindAll(this.api().table().node()); } ') 
      ) )
    # helper function for reading checkbox
    shinyValue = function(id, len) { 
      unlist(lapply(seq_len(len), function(i) { 
        value = input[[paste0(id, i)]] 
        if (is.null(value)) NA else value 
      })) 
    } 
    # output read checkboxes
    output$checked <- renderTable({
      data.frame(selected=shinyValue("selecter_",nrow(mtcars)))
    })
    }
))

解决方案

Hey so I don't completely understand your question, but hopefully this helps. This app isn't perfect, but should do what you want:

library(shiny)
library(DT)
runApp(list(
  ui = basicPage(
    tags$script(
      HTML(
        "Shiny.addCustomMessageHandler('unbind-DT', function(id) {
        Shiny.unbindAll($('#'+id).find('table').DataTable().table().node());
        })"
)
    ),
h2('The data'),
selectInput("myData", "Choose dataset", c("mtcars", "iris"), "mtcars"),
DT::dataTableOutput('mytable'),
h2("Selected"),
tableOutput("checked")
      ),

server = function(input, output, session) {
  dataset <- reactive({
    session$sendCustomMessage("unbind-DT", "mytable")
    get(input$myData)
  })

  # helper function for making checkbox
  shinyInput = function(FUN, len, id, ...) {
    inputs = character(len)
    for (i in seq_len(len)) {
      inputs[i] = as.character(FUN(paste0(id, i), ...))
    }
    inputs
  }
  # datatable with checkbox
  output$mytable = DT::renderDataTable({
    data.frame(
      dataset(),
      Rating = shinyInput(
        selectInput,
        nrow(dataset()),
        "selecter_",
        choices = 1:5,
        width = "60px",
        label = NULL
      ),
      Save = shinyInput(actionButton, nrow(dataset()), 'button_',
                        label = 'Save')
    )
  }, selection = 'none', server = FALSE, escape = FALSE, options = list(
    dom = "ti",
    paging = TRUE,
    preDrawCallback = JS(
      'function() {
      Shiny.unbindAll(this.api().table().node()); }'
    ),
    drawCallback = JS('function() {
                      Shiny.bindAll(this.api().table().node()); } ')
    ))
  # helper function for reading checkbox
  shinyValue = function(id, len) {
    unlist(lapply(seq_len(len), function(i) {
      value = input[[paste0(id, i)]]
      if (is.null(value))
        NA
      else
        value
    }))
  }
  # output read checkboxes
  output$checked <- renderTable({
    data.frame(selected = shinyValue("selecter_", nrow(mtcars)))
  })

  lapply(1:150, function(i) {
    observeEvent(input[[paste0("button_", i)]], {
      updateSelectInput(session,
                        paste0("selecter_", i),
                        selected = 5,
                        label = NULL)
    })
  })

}

))

这篇关于R闪亮:在data.table中更新选择输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆