2017-10-12 116 views
1

我希望我的Shiny應用程序的用戶使用行名和列名填寫2x2表的值。當然,我可以用4個輸入框來完成,但我認爲將所有東西整齊地排列起來會非常困難。儘管如此,我更喜歡桌面佈局,比如DT包提供的佈局。因此,我的問題是:是否有可能由用戶填充datatable(或類似的東西)?如何在由用戶填充的Shiny表中填寫表格?

回答

1

DT A液:

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) 
2

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

+0

在哪裏可以找到'shinysky'軟件包? 'install.packages(「shinysky」)'表示找不到。 – Joe

+1

'devtools :: install_github(「AnalytixWare/ShinySky」)' –