Shiny DT datatable to rmarkdown

89 views
Skip to first unread message

Andrew Scotchmer

unread,
Aug 23, 2017, 12:04:09 PM8/23/17
to Shiny - Web Framework for R
Hi 

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. 

My question is how do I get the data table to render in an Rmarkdown html page.  Nothing I've tried works.

Here is a 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))
      ),
      
      # Show a plot of the generated distribution
      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)




Andrew

Joe Cheng

unread,
Aug 23, 2017, 5:01:15 PM8/23/17
to Andrew Scotchmer, Shiny - Web Framework for R
See the Parameterized Reports feature. If you define, say, a "data" parameter in the yaml header, then when calling rmarkdown::render you can pass params=list(data=reactiveFunction()), and then in your Rmd you can refer to params$data to fetch that data and wrap it in a DT or whatever.

--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/1b9aa146-5adb-4067-b2b9-0ec11f710962%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Message has been deleted

Andrew Scotchmer

unread,
Aug 24, 2017, 4:53:30 AM8/24/17
to Shiny - Web Framework for R
Well that has got me closer thank you.  But I now get a pandoc error 1 message.

I added this to app.R

output$download <- downloadHandler(
    filename <- "report_file.html",
    content <- function(file){
      rmarkdown::render(filename, output_file = file,
                        params = list(data = reactiveFunction())
                        )
    }
  )


This is my Rmd file:
---
title: "Untitled"
output: html_document
params:
  data: !r reactiveFunction()
---

```{r, echo=FALSE}
library(DT)
data(iris)
datatable(params$data)
```


The error I get in full is:

pandoc: Cannot decode byte '\xfe': Data.Text.Internal.Encoding.Fusion.streamUtf8: Invalid UTF-8 stream Warning: Error in : pandoc document conversion failed with error 1 Stack trace (innermost first): 53: pandoc_convert 52: convert 51: rmarkdown::render 50: download$func [/home/scotcan1/Webshot/app.R#45] 1: shiny::runApp Error : pandoc document conversion failed with error 1

Reply all
Reply to author
Forward
0 new messages