2017-08-07 4 views
2

ここで小さな質問:選択した行に戻ることができますか?input$dfname_rows_selected選択した行の数が返されます。私の場合は、後で使用する順序で生成されないため、内部で値を取得して動作させる必要があります。DTを使用して光沢のある数値ではなく選択された行の値を取得

編集:私はあなたができるとは思わない例

ui <- shinyUI(fluidPage(

    DT::dataTableOutput("table"), 
    actionButton("btn", "press me") 

)) 

server <- function(input, output) { 
    observeEvent(input$btn, { 
    print(input$table_rows_selected) 
    }) 

    output$table <- DT::renderDataTable({ 

    mtcars %>% 
     datatable(selection = "multiple") 

    }) 

} 
shinyApp(ui = ui, server = server) 
+0

小さな再現可能な例を示してくださいと予想される出力 – akrun

+1

[OK]を、私は奇妙だ瞬間 –

答えて

2

何か:

library(shiny) 
library(DT) 

ui <- basicPage(
    mainPanel(DT::dataTableOutput('mytable')), 
    textOutput("selected") 
) 

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

    mydata <- reactive({mtcars}) 

    output$mytable = DT::renderDataTable( 
    datatable(mydata()) 
) 

    selectedRow <- eventReactive(input$mytable_rows_selected,{ 
    row.names(mtcars)[c(input$mytable_rows_selected)] 
    }) 

    output$selected <- renderText({ 
    selectedRow() 
    }) 
} 
runApp(list(ui = ui, server = server)) 
1

を追加しました。あなたができることは、データテーブルを作成する前に、データフレームへのすべての変更が行われるリアクティブなものを作成することです。例:

library(shiny) 
library(DT) 

ui <- shinyUI(fluidPage(
    DT::dataTableOutput("table"), 
    textOutput("selectedcar") 
) 
) 

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

    # the reactive where we filter/sort/modify the data 
    reactive_df <- reactive({ 
    mtcars[order(mtcars$cyl),] 
    }) 

    # This datatable uses the reactive directly, so no more modifications 
    output$table <- DT::renderDataTable({ 
    DT::datatable(reactive_df()) 
    }) 

    # now we can get the row/rowname as follows: 
    output$selectedcar <- renderText({ 
    paste0(rownames(reactive_df())[input$table_rows_selected], collapse = ", ") 
    }) 
} 


shinyApp(ui, server) 

これが役立ちます。このような

+0

になります。それは私のためにうまくいく? – Florian

+0

私はネーミングが間違っていたと思っています。それは今働いています:) –

+0

偉大な、私はDTを追加しました:dataTableOutputの前にちょうど光沢のあるバージョンを使用していないことを確認してください。 – Florian

関連する問題