【问题标题】:How to have table in Shiny filled by user?如何让用户填写闪亮的表格?
【发布时间】:2018-03-24 06:18:41
【问题描述】:

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

【问题讨论】:

    标签: r datatable shiny


    【解决方案1】:

    你可以使用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)
    

    【讨论】:

    • 在哪里可以找到shinysky 包? install.packages("shinysky") 说没找到。
    • devtools::install_github("AnalytixWare/ShinySky")
    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-04
      • 2016-03-04
      • 1970-01-01
      • 1970-01-01
      • 2018-08-04
      相关资源
      最近更新 更多