如何在用户填写Shiny表?

时间:2017-10-12 10:35:01

标签: r datatable shiny

我希望我的Shiny应用程序的用户使用行名和列名填写2x2表的值。当然,我可以使用4个输入框来完成它,但我认为将所有东西整齐地定位会很棘手。尽管如此,我更喜欢表格布局,例如DT包提供的表布局。因此,我的问题是:是否可以让用户填充datatable(或类似的东西)?

2 个答案:

答案 0 :(得分:4)

您可以使用shinysky

devtools::install_github("AnalytixWare/ShinySky")

rhandsontable做你想做的事:

rm(list = ls())
library(shiny)
library(shinysky)

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

  # Initiate your table
  previous <- reactive({mtcars[1:10,]})

  MyChanges <- reactive({
    if(is.null(input$hotable1)){return(previous())}
    else if(!identical(previous(),input$hotable1)){
      # hot.to.df function will convert your updated table into the dataframe
      as.data.frame(hot.to.df(input$hotable1))
    }
  })
  output$hotable1 <- renderHotable({MyChanges()}, readOnly = F)
  output$tbl = DT::renderDataTable(MyChanges())
})

ui <- basicPage(mainPanel(column(6,hotable("hotable1")),column(6,DT::dataTableOutput('tbl'))))
shinyApp(ui, server)

enter image description here

答案 1 :(得分:2)

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)