0

Shiny と DT パッケージを使用して、MySQL データベースからフィルター処理されたテーブルを表示しています。

つまり、ユーザーから入力値を取得し、SQL クエリを作成し、出力をキャプチャして DataTable として表示します。出力は、DataTable 列フィルターを使用してさらにフィルター処理でき、ユーザーはフィルター処理されたデータセットをダウンロードできるはずです。

DT docsによるとinput$table_rows_all、表示されたテーブルの行インデックスを含める必要があります。ただし、ダウンロード ボタンを押すと、列名のみでデータのないファイルが取得されます。

library(shiny)
library(DT)
library(RMySQL)

con <- dbConnect(MySQL(), user="myuser", host="myhost", dbname="mydb")

shinyServer(function(input, output) { 

                sqlOutput <- reactive({
                    sqlInput <- paste0("select * from mydb.mytable",
                           " where value < ", input$value,
                           ";")
                    dbGetQuery(con, sqlInput)
                })

                output$table <- DT::renderDataTable(sqlOutput(), server=TRUE, rownames=FALSE, filter="top", options=list(pageLength=10))

                output$download <- downloadHandler("filtered.data.txt", content = function(file) {
                                           rows <- input$table_rows_all
                                           write.table(sqlOutput()[rows, ], file, sep="\t", quote=FALSE, col.names=TRUE, row.names=FALSE)
                })

})

DBI::dbQuery()上記のコードでは、 からの出力をリアクティブ関数に保存して、と の両方にsqlOutput()渡すことができます。DT::renderDataTable()shiny::downloadHandler()

私は何を間違っていますか?よくわかりませんがinput$table_rows_all、何らかの理由で空のベクターを返しているようです。

どんな助けでも大歓迎です、ありがとう!

4

1 に答える 1

0

あなたの例は実際には再現可能ではありませんが、結果のテーブルを光沢のあるものからダウンロードできるようにしたいだけなら、 DT extensionsDTを見てください。そこでは、マニュアルを使用する代わりにダウンロードするオプションを使用できることがわかります。tableToolsdownloadHandler

機能を示す再現可能な例を次に示します。

www-ディレクトリが必要flashで、ブラウザにインストールされています。

library(shiny)
library(DT)

data(iris)

runApp(list(
  ui = fluidPage(
    sidebarLayout(
      sidebarPanel("DT Download Example"),
      mainPanel(
        DT::dataTableOutput("mytable")
        )
      )
    ),
  server = function(input, output) {
    output$mytable <- DT::renderDataTable({
      iris
    }, 
    extensions = 'TableTools',
    rownames=FALSE, 
    filter="top", 
    options=list(dom = 'T<"clear">lfrtip',
                 tableTools = list(
                   sSwfPath = copySWF('www'),
                   aButtons = list('copy', 'print',
                                   list(
                                     sExtends = 'collection',
                                     sButtonText = 'Save',
                                     aButtons = c('csv','xls')
                                     )
                                   )
                   )
                 )
    )
  }))

コード内の関連部分のテストされていないバージョンを次に示します。

output$table <- DT::renderDataTable({
  sqlOutput()
}, server=TRUE, 
extensions = 'TableTools',
rownames=FALSE, 
filter="top", 
options=list(pageLength=10,
             dom = 'T<"clear">lfrtip',
             tableTools = list(
               sSwfPath = copySWF('www'),
               aButtons = list('copy', 'print',
                               list(
                                 sExtends = 'collection',
                                 sButtonText = 'Save',
                                 aButtons = c('csv','xls'))))))
于 2015-09-16T12:48:57.303 に答える