使用RMarkdown或Shiny

时间:2017-09-21 16:31:17

标签: r shiny r-markdown formattable

我想

  1. 创建一个并排显示formattable个表格的报告
  2. 创建该报告的HTML,PDF或图像版本,
  3. 通过电子邮件发送给用户,以便他们查看或打印文档。
  4. 理想情况下,我可以生成HTML或PDF文档,但只会提供其中一个我可以设置分页符的内容,并且当没有R的人打印时,绿色检查和红色X将正确打印HTML或PDF版本的文档。

    尝试1 。我试过的一个选择是RMarkdown。在这种情况下,我不知道如何并排放置格式表。以下是示例代码:

    ---
    title: "test"
    author: "me"
    date: "September 21, 2017"
    output:
      html_document: default
      pdf_document: default
    ---
    
    ```{r,echo=F, warning=F}
    library(formattable)
    library(htmlwidgets)
    
    ## Create some data for the tables. I'll want 5 different tables, but for simplicity of this code, let's just repeat the same table 5 times.
    d1 = mtcars[5:8, c(1:4,4)]
    ```
    ### Three tables I would like to be side-by-side
    ```{r,echo=F}
    # create some formatting for the tables.
    hp.formatter = formatter("span", style = x ~ style(color = ifelse(x>=110, "green", ifelse(x<=93, "red", 'white'))), x ~ icontext(ifelse(x>110, "ok", ifelse(x<=93, "remove", ''))))
    
    # Create the tables. 
    t1 = as.htmlwidget(formattable(d1, list(hp.1=hp.formatter)), width='33%')
    t1
    t1
    t1
    ```
    
    ### Two tables I would like to be side-by-side
    ```{r,echo=F}
    t1
    t1
    ```
    

    尝试2 。或者,我可以使用闪亮的。在这种情况下,我知道如何将formattable表并排放置。但是,我不知道如何将输出下载为HTML或PDF,以便可以通过电子邮件发送给用户,以便他们可以打印。以下是一些示例代码:

    library(shiny)
    library(formattable)
    library(htmlwidgets)
    
    # Define UI 
    ui <- fluidPage(
    
      fluidRow(h3(paste0('Three formattable tables side-by-side'))), 
      fluidRow(column(3, div(tabPanel("Left"  , formattableOutput("d1")))), column(1),
               column(3, div(tabPanel("Center", formattableOutput("d2")))), column(1),
               column(3, div(tabPanel("Right" , formattableOutput("d3")))), column(1)
      ),
    
      fluidRow(h3(paste0('Two formattable tables side-by-side'))), 
      fluidRow(column(2),
               column(3, div(tabPanel("Left"  , formattableOutput("d4")))), column(2),
               column(3, div(tabPanel("Right" , formattableOutput("d5")))), column(2)
      )
    ) 
    
    server <- function(input, output) {
    
      ## Create some data for the table.
      ## I'll want 5 different tables, but for simplicity, let's just repeat the same data 5 times.
      d1 = mtcars[5:8, c(1:4,4)]
    
      ## Define some formatting for the formattable tables
      hp.formatter = formatter("span", style = x ~ style(color = ifelse(x>=110, "green", ifelse(x<=93, "red", 'white'))), x ~ icontext(ifelse(x>110, "ok", ifelse(x<=93, "remove", ''))))
    
      ## Create the table. 
      t1 = formattable(d1, list(hp.1=hp.formatter))
    
      ## Create the outputs. (Repeat same table 5 times)
      output$d1=renderFormattable(t1)
      output$d2=renderFormattable(t1)
      output$d3=renderFormattable(t1)
      output$d4=renderFormattable(t1)
      output$d5=renderFormattable(t1)
    
    }
    
    # Run the application 
    shinyApp(ui = ui, server = server)
    

    感谢您的帮助!

0 个答案:

没有答案