从R

时间:2018-01-25 21:18:27

标签: r shiny knitr r-markdown

您好我想创建一个闪亮的应用程序,它将输入一个csv文件,并将创建一个数据表。出于本示例的目的,我使用mtcars数据集。生成表格时,我希望能够将其作为pdf下载,但增强了某些特性。

  • 标题字体 - Calibri(或类似),10磅,粗体
  • 标题行着色-10%灰色

  • 表格字体 - Calibri(或类似),10分

  • 超链接 - 蓝色文字,带下划线

我的示例代码如下:

#ui.r
library(shiny)
library(DT)
library(rmarkdown)

fluidPage(

  # App title ----
  titlePanel(div("CLINICAL TABLE",style = "color:blue")),

  # Sidebar layout with input and output definitions ----
  sidebarLayout(

    # Sidebar panel for inputs ----
    sidebarPanel(

      # Input: Select a file ----
      fileInput("file1", "Input CSV-File",
                multiple = TRUE,
                accept = c("text/csv",
                           "text/comma-separated-values,text/plain",
                           ".csv")),

      # Horizontal line ----
      tags$hr(),

      # Input: Checkbox if file has header ----
      checkboxInput("header", "Header", TRUE),

      # Input: Select separator ----
      radioButtons("sep", "Separator",
                   choices = c(Comma = ",",
                               Semicolon = ";",
                               Tab = "\t"),
                   selected = ","),


      # Horizontal line ----
      tags$hr(),

      # Input: Select number of rows to display ----
      radioButtons("disp", "Display",
                   choices = c(Head = "head",
                               All = "all"),
                   selected = "head"),
      radioButtons('format', 'Document format', c('PDF', 'CSV'),
                   inline = TRUE),
      downloadButton('downloadReport')


    ),
    # Main panel for displaying outputs ----
    mainPanel(

      tabsetPanel(type = "tabs",
                  tabPanel("Table",
                           shiny::dataTableOutput("contents"))
      ))
  ))

#server.r
function(input, output) {

  output$contents <- shiny::renderDataTable({
    mtcars
  })


output$downloadReport <- downloadHandler(
  filename = function() {
    paste('my-report', sep = '.', switch(
      input$format, PDF = 'pdf', CSV = 'csv'
    ))
  },
  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 <- render('report.Rmd', switch(
      input$format,
      PDF = pdf_document(), HTML = html_document()
    ))
    file.rename(out, file)
  }
)
}

0 个答案:

没有答案