闪亮的DT数据表到Rmarkdown [英] Shiny DT datatable to Rmarkdown
问题描述
我有一个闪亮的仪表板应用程序,可以显示DT数据表.在侧边栏上基于用户输入的功能中对原始数据进行操作.一切都很好,运作良好.
I have a shiny dashboard app that shows a DT datatable. The raw data is manipulated in a function based on user inputs on the sidebar. All is good and working well.
我的问题是如何获取数据表以在Rmarkdown html页面中呈现.我没有尝试过.
My question is how do I get the data table to render in an Rmarkdown html page. Nothing I've tried works.
以下是一些简单的代码示例.我希望当单击下载按钮时,该表能够在Rmarkdown页面中呈现.
Here is some simple code by way of an example. I wish the table to render in an Rmarkdown page when I click a download button.
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?
So in this example, how would I pass dataTableOutput("reactiveTable") to Rmarkdown?
谢谢
推荐答案
以下是适合您的代码,您首先需要在闪亮的应用程序中使用 downloadHandler()
参数:
Here is the code for you, you need to use downloadHandler()
argument in shiny app at first:
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文件与将要呈现 DT
的app.R文件放在同一目录中.
and additionally you need report_file.Rmd file in the same directory as app.R file where DT
is going to render:
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))
```
这篇关于闪亮的DT数据表到Rmarkdown的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!