闪亮的DT数据到Rmarkdown

时间:2017-08-23 16:09:38

标签: r shiny r-markdown dt

我有一个闪亮的仪表板应用程序,显示DT数据表。原始数据基于侧栏上的用户输入在功能中进行操作。一切都很好,运作良好。

我的问题是如何在Rmarkdown html页面中显示数据表。我没有尝试过任何工作。

以下是一些简单的代码示例。我希望当我点击下载按钮时,表格会在Rmarkdown页面中呈现。

library(shiny)
library(DT)
data(iris)

ui <- fluidPage(

   titlePanel("|Species Table"),

   sidebarLayout(
      sidebarPanel(
         selectInput("specs",
                     "Number of bins:",
                     unique(iris$Species))
      ),

      mainPanel(
         tableOutput("specTable")
      )
   )
)


server <- function(input, output) {

  subSpec <- function(x){
    iris[iris$Species == x, -5]
    iris[1:10,]
  }

  reactiveFunction <- reactive({ subSpec(input$specs) })

  output$reactiveTable <- renderDataTable({ reactiveFunction() }, rownames = FALSE)

  output$specTable <- renderUI({
    dataTableOutput("reactiveTable")
  })

}


shinyApp(ui = ui, server = server)

所以在这个例子中,我如何将dataTableOutput(“reactiveTable”)传递给Rmarkdown?

由于

1 个答案:

答案 0 :(得分:1)

以下是您的代码,您需要首先在闪亮的应用中使用downloadHandler()参数:

<强> app.R

library(shiny)
library(DT)
data(iris)

ui <- fluidPage(

  titlePanel("|Species Table"),

  sidebarLayout(
    sidebarPanel(
      selectInput("specs",
                  "Number of bins:",
                  unique(iris$Species)),
      downloadButton('downloadReport')
    ),

    mainPanel(
      tableOutput("specTable")
    )
  )
)


server <- function(input, output) {

  subSpec <- function(x){
    iris[iris$Species == x, -5]
    iris[1:10,]
  }

  reactiveFunction <- reactive({ subSpec(input$specs) })

  output$reactiveTable <- renderDataTable({ reactiveFunction() }, rownames = FALSE)

  output$specTable <- renderUI({
    dataTableOutput("reactiveTable")
  })


  output$downloadReport <- downloadHandler(
    filename = function() {
      paste("test",".html",sep="")
    },
    content = function(file) {
      src <- normalizePath('report_file.Rmd')

      # temporarily switch to the temp dir, in case you do not have write
      # permission to the current working directory
      owd <- setwd(tempdir())
      on.exit(setwd(owd))
      file.copy(src, 'report_file.Rmd', overwrite = TRUE) 

      out <- rmarkdown::render('report_file.Rmd')
      file.rename(out, file)
    }
  )

}




shinyApp(ui = ui, server = server)

此外,您需要report.file.Rmd文件与app.R文件在同一目录中,其中DT将呈现:

<强> report_file.Rmd

```{r, fig.align='center', fig.pos='htb!', echo=FALSE, cache=FALSE, warning = FALSE, message = FALSE, tidy=TRUE}
library(DT)
datatable(reactiveFunction(),rownames=FALSE, options = list(dom='t',ordering=F))
```
相关问题