用户如何在Shiny中填充表格? [英] How to have table in Shiny filled by user?

查看:251
本文介绍了用户如何在Shiny中填充表格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的Shiny应用程序的用户使用行名和列名填写2x2表的值。当然,我可以使用4个输入框来完成此操作,但是我认为将所有内容整齐地放置会很棘手。尽管如此,我还是希望使用一种表格布局,例如 DT 包提供的布局。因此,我的问题是:是否可以由用户填充数据表(或类似的东西)?

I want users of my Shiny app to fill in the values of a 2x2 table with row and column names. Of course, I could do it with 4 input boxes, but I assume that it will be tricky to position everything neatly. Despite that, I would prefer a table layout such as the one provided by the DT package. Thus, my question is: Is it possible to have a datatable (or something similar) filled by the user?

推荐答案

使用 DT 的解决方案:

library(DT)
library(shiny)

dat <- data.frame(
  V1 = c(as.character(numericInput("x11", "", 0)), as.character(numericInput("x21", "", 0))),
  V2 = c(as.character(numericInput("x21", "", 0)), as.character(numericInput("x22", "", 0)))
)

ui <- fluidPage(
  fluidRow(
    column(5, DT::dataTableOutput('my_table')),
    column(2),
    column(5, verbatimTextOutput("test"))
  )
)

server <- function(input, output, session) {

  output$my_table <- DT::renderDataTable(
    dat, selection = "none", 
    options = list(searching = FALSE, paging=FALSE, ordering=FALSE, dom="t"), 
    server = FALSE, escape = FALSE, rownames= FALSE, colnames=c("", ""), 
    callback = JS("table.rows().every(function(i, tab, row) {
                  var $this = $(this.node());
                  $this.attr('id', this.data()[0]);
                  $this.addClass('shiny-input-container');
                  });
                  Shiny.unbindAll(table.table().node());
                  Shiny.bindAll(table.table().node());")
  )

  output$test <- renderText({
    as.character(input$x11)
  })

}

shinyApp(ui, server)

这篇关于用户如何在Shiny中填充表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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