2017-10-16 7 views

答えて

0

解決策には2つの部分があります。最初にselectionパラメータのdatatable()は、形式のリストを取ることができますlist(mode='multiple', selected=c(1,3))

2番目の部分は、新しいテーブルに残っている選択行を決定することです。 2番目の部分に対する1つの解決策は、データテーブルのコピーをセッション変数として保存することです。新しいデータテーブルが生成されると、古いテーブルは新しいテーブルと比較されます。選択された行インデックスの新しいセットは、古いテーブルと新しいテーブルの共通の行に基づいて計算されます。新しいテーブルへの行インデックスは、which(newkeys %in% oldkeys)イディオムを使用して検索されます。ここで

は例です:

これも shiny::runGist("https://gist.github.com/dkulp2/7ebb1c936d08f3434127e58d7798af28")

でRStudioから実行することができます
library(shiny) 

ui <- fluidPage(
    checkboxInput('yellow.only', 'Yellow Only'), 
    uiOutput('fruit.selection'), 
    DT::dataTableOutput("dt.fruit.selection") 
) 

server <- function(input, output) { 

    fruit.options <- reactive({ 
    all.fruits <- c(grape='Grape', banana='Banana', papaya='Papaya', raspberry='Raspberry') 
    yellow.fruits <- c(FALSE, TRUE, TRUE, FALSE) 
    all.fruits[yellow.fruits | !input$yellow.only] 
    }) 

    fruit.options.df <- reactive({ 
    data.frame(fruits=fruit.options(), some.other.col=nchar(fruit.options())) 
    }) 

    output$fruit.selection <- renderUI({ selectInput('fruit', 'Fruit', choices=fruit.options(), selected=input$fruit, multiple=TRUE, selectize=FALSE, size=length(fruit.options())) }) 

    output$dt.fruit.selection <- DT::renderDataTable({ 
    if (!exists('fruit.options.cache') || identical(fruit.options.cache, fruit.options.df())) { 
     rows.selected <- isolate(input$dt.fruit.selection_rows_selected) 
    } else { 
     rows.selected <- which(fruit.options.df()$fruit %in% fruit.options.cache$fruits[isolate(input$dt.fruit.selection_rows_selected)]) 
    } 

    fruit.options.cache <<- fruit.options.df() 
    DT::datatable(fruit.options.cache, rownames=FALSE, selection=list(mode="multiple", selected=rows.selected)) 
    }) 
} 

# Run the application 
shinyApp(ui = ui, server = server) 

関連する問題