Combining Shiny app and Rmd file

时间:2016-04-04 17:34:39

标签: r shiny knitr r-markdown

I have a RMD file in R and I use shiny app within this file to read csv file and do some cleaning on the data. Then I need to have access to this data in the Rmd file and only after finishing the cleaning, the remaining of Rmd is processed!

My Rmd:

---
title: "Report_HTML"
author: "Mohammad"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output: html_document
runtime: shiny
---

## Read file
This report...... (some text)

```{r, echo=FALSE}
library(tcltk)
inputPanel(
  checkboxInput('header', 'Header', FALSE),
  radioButtons('sep', 'Separator',
               c(Comma=',',
                 Semicolon=';',
                 Tab='\t'),
               ';'),
  radioButtons('quote', 'Quote',
               c(None='',
                 'Double Quote'='"',
                 'Single Quote'="'"),
               '"'),
  fileInput('file1', 'Choose a file to upload',
            accept = c(
              'text/csv',
              'text/comma-separated-values',
              'text/tab-separated-values',
              'text/plain',
              '.csv',
              '.tsv'
            )
  )
)

remove.duplicates <- function(dt){
   #some data cleaning
   return(dt)
}

datasetInput <- reactive({

  inFile <- input$file1

  dt <- read.csv(inFile$datapath, header = input$header, sep = input$sep, quote = input$quote)

  #Remove duplicates
  dt2 <- remove.duplicates(dt)

  dt2

})

renderDataTable({
  datasetInput()
})
```

## Statistics
in this section I need to add some simple statistics like min, max, mean for each column of the `dt`.
```{r, echo=FALSE}
code for calculating min, max, and mean each column dt and show them in a table
```

I have two problems:

  1. I only want to run the code related to the Statistics section after reading and cleaning the data NOT the beginning of loading the Rmd file.
  2. I don't have access to the dt in the Statistics section, how can I have access?

Thanks for your help

1 个答案:

答案 0 :(得分:0)

我使用this question中解释的方法解决了问题。我希望它可以帮助那些有同样问题的人。