我想在我闪亮的应用程序中使用knitr获取报告

时间:2019-01-26 11:16:52

标签: r shiny knitr

我有一个闪亮的应用程序,可以从下载的数据集中提取一些表格,地块等。我想生成报告(knitr),其中将涵盖我在Serwer.R

中创建的所有表格和绘图仪

我用过downloadHandler

UI.R

library(shiny)


shinyUI(fluidPage(
sidebarLayout(

    sidebarPanel(
        # 2. Przegladarka do pobrania danych 
        fileInput("fileInPath", 
            label= h4("Import danych"), accept=("text/csv")),
        radioButtons('plott','Plot',c('ggplot2'='ggplot2', 'lattice'='lattice')
            ),
        radioButtons('format', 'Document format', c('PDF', 'HTML', 'Word'),
                     inline = TRUE),
        downloadButton('downloadReport',label="Wygeneruj raport"),
        uiOutput("ListOfColumns")

       ),


    mainPanel(
      tabsetPanel(type = "tabs",
                  tabPanel("Table", tableOutput("daneIn")),


                  tabPanel("Plot", plotOutput("plot"),
                             downloadButton("plotOutPath",label="Eksport wykresu")),

                  tabPanel("Macierz", tableOutput("macierz"),
                             downloadButton("fileOutPath",label="Eksport macierzy korelacji")),

                  tabPanel("Korelacja", plotOutput(outputId="korelacja"),
                             downloadButton("corOutPath",label="Eksport wykresu korelacji"))
        )
    )

)

))

Serwer.R

library(shiny)


library(ggplot2)
library(ggcorrplot)
library(reshape2)
library(lattice)
library(latticeExtra)
library(knitr)



    ShinyServer(function(input, output) {



# 3 wczytywanie danych 
dataIn <- reactive({
    inFile <- input$fileInPath

    if (is.null(inFile)) {
        return(NULL)
    }
    read.table(file=inFile$datapath,sep=";",dec=",",header=T,stringsAsFactors=FALSE)
})


output$ListOfColumns <- renderUI({
  req(dataIn())
  columns <- colnames(dataIn())

  checkboxGroupInput("Columns", "Choose columns",columns)
})     

# 4 wypisywanie danych 
output$daneIn <- renderTable({
    ret <- rbind(
        head(dataIn(),5),
        tail(dataIn(),5)
    )

    return(ret)

},include.rownames=FALSE)



output$plot <- renderPlot({ 


    d <- dataIn()

    d <- melt(d[,-c(2:4)],id.vars="Numer")

    if (input$plott == 'ggplot2'){

    wyk <- (
        ggplot(d,aes(x=variable,y=value)) 
        + geom_boxplot() 
        + xlab("") + ylab("Kula")
        ) 

    return(wyk) 
    }

    wyk2 <- (
      bwplot(value~variable,
             data=d,
             horizontal=F,
             ylab = "Kula")
    )
    return(wyk2)

}) 


output$macierz <- renderTable({
  d <- dataIn()
  cormat <- round(cor(d[,-c(1:4)]),2)
  return(cormat)
}, rownames=TRUE)


output$korelacja <- renderPlot({
   d <- dataIn()
   cormat <- round(cor(d[,-c(1:4)]),2)

   if (input$plott == 'ggplot2'){
   wyk <- (
     ggcorrplot(cormat, hc.order = TRUE, 
              type = "lower", 
              lab = TRUE,
              lab_size = 3, 
              method="circle", 
              colors = c("tomato2", "white", "springgreen3"),
              ggtheme=theme_bw)
   )

return(wyk)
   }
   rgb.palette <- colorRampPalette(c("tomato2", "springgreen3"), space = "rgb")
   wyk2 <- (

     levelplot(cormat, col.regions = rgb.palette(120), cuts=100)
   )
    return(wyk2) 
})




output$plotOutPath <- downloadHandler(
  filename = function() { 
    paste0("out.jpeg") 
  },
  content = function(file) {
    ggsave(file, device = "jpeg")
  }
)

output$fileOutPath <- downloadHandler(
  filename = function() { 
    paste0("out.csv") 
  },
  content = function(file) {
    write.table(cormat,
                file,sep=";",dec=".",row.names=T,col.names=T)
  }
)
output$corOutPath <- downloadHandler(
  filename=function(){
   paste0("out.jpeg")
    },
  content=function(file){
    ggsave(file, device = "jpeg")
    }
  )

output$report <- downloadHandler(
  # For PDF output, change this to "report.pdf"
  filename = "report.html",
  content = function(file) {

    tempReport <- file.path(tempdir(), "report.Rmd")
    file.copy("report.Rmd", tempReport, overwrite = FALSE)


    # Knit the document, passing in the `params` list, and eval it in a
    # child of the global environment (this isolates the code in the document
    # from the code in this app).
    rmarkdown::render(tempReport, output_file = file,
                      envir = new.env(parent = globalenv())
    )
  }
)
}
)

我希望得到一份报告。我想选择是PDF,Word还是HTML。应该涵盖我使用的所有图表,表格等。

0 个答案:

没有答案