2016-11-22 15 views
2

パッケージを使用して、shinyアプリにデータテーブルを表示しています。私は異なるデータセットを提供するので、それらを選択するためのラジオボタンがあり、データテーブルは自動的に更新されます。光沢のあるDTデータテーブルでデータセットを変更するときに選択した行を保持します

私がしたいのは、df1からの利用可能な行をデータセットの切り替え時に事前に選択することです。現時点では、私の選択は常に消去されます。選択した行を保存しようとすると(2行のコメントを外してください)、私のテーブルは直接リセットされます。

library(shiny) 
library(DT) 

df1 <- data.frame(names=letters, 
        values=1:26) 
df2 <- data.frame(names=letters, 
        values=(1:26)*2)[seq(1,26,2),] 

ui <- shinyUI(
    fluidPage(
    sidebarLayout(
     sidebarPanel(
     radioButtons("dataset", label=h5("Select dataset"), 
        choices=list("df1"='df1', 
            "df2"='df2'), 
        selected='df1', inline=TRUE) 
    ), 
     mainPanel(
     DT::dataTableOutput("name_table") 
    ) 
    ) 
) 
) 

サーバー側...

server <- function(input, output, session) { 
    getDataset <- reactive({ 
    result <- list() 
    result[['dataset']] <- switch(input$dataset, 
        'df1'=df1, 
        'df2'=df2) 
    # result[['selection']] <- 
    # as.numeric(input$name_table_rows_selected) 
    return(result) 
    }) 
    output$name_table <- DT::renderDataTable({ 
    DT::datatable(getDataset()[['dataset']], 
        options=list(pageLength=5)) 

    }) 
    name_proxy = DT::dataTableProxy('name_table') 
} 

shinyApp(ui, server) 

私はプロキシとデータテーブルといくつかの相互作用を必要とするので、私は、DTテーブルを使用していました。

答えて

3

あなたは

server <- function(input, output, session) { 
    dd=reactiveValues(select=NULL) 

    observeEvent(input$dataset,{ 
    dd$select=as.numeric(isolate(input$name_table_rows_selected)) 
    }) 

    getDataset <- reactive({ 
    result <- list() 
    result[['dataset']] <- switch(input$dataset, 
            'df1'=df1, 
            'df2'=df2) 

    return(result) 
    }) 
    output$name_table <- DT::renderDataTable({ 
    DT::datatable(getDataset()[['dataset']], 
        options=list(pageLength=5), 
        selection = list(mode = 'multiple', selected =dd$select ) 
    ) 

    }) 
    name_proxy = DT::dataTableProxy('name_table') 
} 

shinyApp(ui, server) 

または@drmariod変異体のビットの変更のようにdfを変更しようとする場合にのみ、選択した行を保存することができます偉大に見える代わりにreactive

server <- function(input, output, session) { 
    getDataset <- eventReactive(input$dataset,{ 
    result <- list() 
    result[['dataset']] <- switch(input$dataset, 
            'df1'=df1, 
            'df2'=df2) 
    result[['selection']] <- testing() 
    return(result) 
    }) 
    testing <- function() { 
    list(selected=as.numeric(input$name_table_rows_selected)) 
    } 
    output$name_table <- DT::renderDataTable({ 
    DT::datatable(getDataset()[['dataset']], 
        options=list(pageLength=5), 
        selection=getDataset()[['selection']]) 

    }) 
    name_proxy = DT::dataTableProxy('name_table') 
} 
+0

eventReactiveを使用!両方の提案ありがとう! – drmariod

1

私は解決策を見つけたようですが、より良い解決策があるのだろうかと思います。

server <- function(input, output, session) { 
    getDataset <- reactive({ 
    result <- list() 
    result[['dataset']] <- switch(input$dataset, 
        'df1'=df1, 
        'df2'=df2) 
    result[['selection']] <- testing() 
    return(result) 
    }) 
    testing <- function() { 
    list(selected=as.numeric(input$name_table_rows_selected)) 
    } 
    output$name_table <- DT::renderDataTable({ 
    DT::datatable(getDataset()[['dataset']], 
        options=list(pageLength=5), 
        selection=getDataset()[['selection']]) 

    }) 
    name_proxy = DT::dataTableProxy('name_table') 
} 

私は、時々processingメッセージが来るのだろうか。クリックごとに表がまもなく "点滅"します...より良い回答を得ることは素晴らしいでしょう。

関連する問題