2016년 8월 10일 수요일

Download knitr Reports

This app uses the rmarkdown package to compile a report template to PDF/HTML/Word, which we can download by clicking a button. Note we switched the working dir to a temporary dir, to avoid the possibility that we do not have write permission on the server.

http://shiny.rstudio.com/gallery/download-knitr-reports.html

pandoc document conversion failed with error 41

Solution:
Step 1: Download and Install MiKTeX from http://miktex.org/2.9/setup
Step 2: Run Sys.getenv("PATH") in R studio 
This command returns the path where Rstudio is trying to find pdflatex.exe
In windows (64-bit) it should return C:\Program Files\MiKTeX 2.9\miktex\bin\x64\pdflatex.exe

If pdflatex.exe is not located in this location Rstudio gives this error code 41.
Step 3: To set this path variable run:
Sys.setenv(PATH=paste(Sys.getenv("PATH"),"C:/Program Files/MiKTeX 2.9/miktex/bin/x64/",sep=";"))

https://github.com/rstudio/shiny-examples/issues/34

generate report with shiny app and

I would like to create a shiny app that allows you to download a report. Right now i'm trying to keep things super simple and the only input on the shiny app is some text that the user can input using textarea:
library(shiny)
server <- function(input, output) {
  output$downloadReport <- downloadHandler(
    filename = function() {
      paste('my-report', sep = '.', switch(
        input$format, PDF = 'pdf', HTML = 'html', Word = 'docx'
      ))
    },
    content = function(file) {
      src <- normalizePath('report.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.Rmd', overwrite = TRUE)

      out <- rmarkdown::render('report.Rmd',
                               params = list(text = input$text),
                               switch(input$format,
                                      PDF = pdf_document(), 
                                      HTML = html_document(), 
                                      Word = word_document()
                               ))
      file.rename(out, file)
    }
  )
}

ui <- fluidPage(
  tags$textarea(id="text", rows=20, cols=155, 
                placeholder="Some placeholder text"),

  flowLayout(radioButtons('format', 'Document format', c('HTML', 'Word'),
                          inline = TRUE),
             downloadButton('downloadReport'))

)

shinyApp(ui = ui, server = server)
and report.Rmd
---
title: "Parameterized Report for Shiny"
output: html_document
params:
  text: 'NULL'
---

# Some title

`r params[["text"]]`
http://stackoverflow.com/questions/37347463/generate-report-with-shiny-app-and

2016년 8월 9일 화요일

R Shiny Demo - how to embed pdf into shiny app

library(shiny)
shinyServer(function(input, output,session){
})
 ui.r
# R Shiny app demo - display PDF in app as reference document
library(shiny)
# Simple shiny layout for demo sake
shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
h5("use case - embed a pdf user guide in the app - embed as a local pdf or from web URL")
),
mainPanel(
tabsetPanel(
# using iframe along with tags() within tab to display pdf with scroll, height and width could be adjusted
tabPanel("Reference",
tags$iframe(style="height:400px; width:100%; scrolling=yes",
src="https://cran.r-project.org/doc/manuals/r-release/R-intro.pdf")),
tabPanel("Summary"),
tabPanel("Plot")
)
))
))
# Example web url used in our demo app
# https://cran.r-project.org/doc/manuals/r-release/R-intro.pdf
# Please note that the code might not work with all https:// links such as
# this drop box link might not work
# https://www.dropbox.com/s/clzf4cd92nqd706/Get%20Started%20with%20Dropbox.pdf?dl=0
# Replace dl=0 with raw=1 to fix if drop box link does not work
# https://www.dropbox.com/s/clzf4cd92nqd706/Get%20Started%20with%20Dropbox.pdf?raw=1
# If problems with drop box link not showing pdf, refer to the below stackoverflow
# http://stackoverflow.com/questions/29763759/how-to-open-a-dropbox-file-in-the-application-webpage
# if using the local copy of pdf, ensure that the pdf is in www folder
# and specify the relative path accordingly against src
# Example I am in my working directory and "shiny-cheatsheet.pdf" is in www folder withn working directory
# tabPanel("Reference",
# tags$iframe(style="height:400px; width:100%; scrolling=yes",
# src="shiny-cheatsheet.pdf"))
https://gist.github.com/aagarw30/d5aa49864674aaf74951

Outputting Shiny (non-ggplot) plot to PDF

Is there a method to output (UI end) Shiny plots to PDF for the app user to download? I've tried various methods similar to those involving ggplot, but it seems downloadHandler can't operate in this way. For example the following just produces broken PDF's that don't open.
Solved. The plot should be saved locally with pdf(), not the screen device (as with dev.copy2pdf). Here's a working example: shiny::runGist('d8d4a14542c0b9d32786'). For a nice basic model try:
server.R
library(shiny)
shinyServer(
    function(input, output) {

        plotInput <- reactive({
            if(input$returnpdf){
                pdf("plot.pdf", width=as.numeric(input$w), height=as.numeric(input$h))
                plot(rnorm(sample(100:1000,1)))
                dev.off()
            }
            plot(rnorm(sample(100:1000,1)))
        })

        output$myplot <- renderPlot({ plotInput() })
        output$pdflink <- downloadHandler(
            filename <- "myplot.pdf",
            content <- function(file) {
                file.copy("plot.pdf", file)
            }
        )
    }
)
ui.R
require(shiny)
pageWithSidebar(
    headerPanel("Output to PDF"),
    sidebarPanel(
        checkboxInput('returnpdf', 'output pdf?', FALSE),
        conditionalPanel(
            condition = "input.returnpdf == true",
            strong("PDF size (inches):"),
            sliderInput(inputId="w", label = "width:", min=3, max=20, value=8, width=100, ticks=F),
            sliderInput(inputId="h", label = "height:", min=3, max=20, value=6, width=100, ticks=F),
            br(),
            downloadLink('pdflink')
        )
    ),
    mainPanel({ mainPanel(plotOutput("myplot")) })
)

http://stackoverflow.com/questions/27276994/outputting-shiny-non-ggplot-plot-to-pdf