0

I am new to Rmarkdown and shiny and forgive me for some naive questions. I have build a code in two parts first where I do all the processing and second where I call the Rmarkdown to knit it.

The first code example.R is as follows and works fine independently (with only glitch of plots being trimmed from sides):

# Create a label for the knitr code chunk name 
## @knitr ExternalCodeChunk020 
library(Seurat)
library(tidyverse)
library(sleepwalk)
library(gridExtra)
library(plotly)
library(DT)
# Set up some sample data 
data(mtcars)

# Display the xvars 
# Note that I don't really want to display the xvars, but this line is included 
# to demonstrate that text output won't show up in the RMarkdown in this example. 

a <- ggplotly(ggplot(mtcars, aes(cyl,mpg)) + geom_boxplot())
b <- ggplotly(ggplot(mtcars, aes(wt,mpg)) + geom_point())

subplot(a, b, nrows=1)

DT::datatable(mtcars, class = "cell-border stripe", rownames = FALSE, filter ="top", 
              editable =TRUE, extension = "Buttons", options = list(dom="Bfrtip", 
                                                                    buttons =c("copy", "csv", "excel", "pdf","print")))


ggplotly(ggplot(mtcars,aes(x=mpg)) + geom_histogram(binwidth=5))

    
# Display the date and time 
# Similar to xvars above, this line is intended to demonstrate that text output 
# won't be displayed in this RMarkdown example. 
Sys.Date() 

The second part of the code (mrkdwn.Rmd) is where I try to knit and generate Rmarkdown report:

--- 
title: "Code Chunks" 
author: "Author" 
date: "November 13, 2020" 
output: html_document 
--- 

```{r setup, include=FALSE} 
  knitr::opts_chunk$set(echo = TRUE) 
  knitr::read_chunk("example.R") 
``` 

This first code chunk prints the externally located code, 
but it does not execute the code. The next code chunk 
executes the externally located code, but it does not print code 
itself. Text output is suppressed, and figures are plotted, 
but only after all of the code is executed. 

```{r DisplayCodeChunk, eval = FALSE, echo = FALSE} 
  <<ExternalCodeChunk020>> 
``` 

```{r RunCodeChunk, echo = FALSE, eval = TRUE, results = 'hide'} 
  <<ExternalCodeChunk020>>
```   

the output doesn't contain plots. I am not sure what is going wrong, could anyone of you help me in fixing this.

I know that an easy fix is to put both parts of the code together inside the Rmarkdown like this:

---
title: "test3"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r}

library(Seurat)
library(tidyverse)
library(sleepwalk)
library(gridExtra)
library(plotly)
library(DT)
# Set up some sample data 
data(mtcars)

# Display the xvars 
# Note that I don't really want to display the xvars, but this line is included 
# to demonstrate that text output won't show up in the RMarkdown in this example. 


a <- ggplotly(ggplot(mtcars, aes(cyl,mpg)) + geom_boxplot())
b <- ggplotly(ggplot(mtcars, aes(wt,mpg)) + geom_point())

subplot(a, b, nrows=1)

DT::datatable(mtcars, class = "cell-border stripe", rownames = FALSE, filter ="top", 
              editable =TRUE, extension = "Buttons", options = list(dom="Bfrtip", 
                                                                    buttons =c("copy", "csv", "excel", "pdf","print")))


ggplotly(ggplot(mtcars,aes(x=mpg)) + geom_histogram(binwidth=5))



# Display the date and time 
# Similar to xvars above, this line is intended to demonstrate that text output 
# won't be displayed in this RMarkdown example. 
Sys.Date() 
```

## Including Plots

You can also embed plots, for example:

```{r pressure, echo=FALSE}
plot(pressure)
```

Since I need to process large datasets and generate graphs/plots and table I would prefer to keep them separately, so that my Rmarkdown doesn't crash. May be this is wrong and there could be a better approach, please suggest.

Many thanks for your time and help.

Angelo
  • 4,829
  • 7
  • 35
  • 56
  • 1
    Remove `results = 'hide'`? After doing so I get a nice report with a datatable and plotly charts. – stefan Nov 13 '20 at 09:43
  • Thanks a lot. I have an additional question is my approach best to handle large datasets that require a lot of processing. I mean, that this would not lead to crashing of Rmarkdown if the example.R file has to process like 5-7gb of dataset. – Angelo Nov 13 '20 at 11:39
  • 1
    Hm. One option would be to do the data processing in a script, save the processed data and in the end call the rmd which prepares the report. – stefan Nov 13 '20 at 12:49
  • A small example would be very kind of you. I mean you can use the above scripts to exemplify. It will really help me :) – Angelo Nov 13 '20 at 14:40
  • 1
    Wouldn't you want to add `cache=FALSE` to your R chunks or to the `opts_chunk$set(echo = TRUE cache=FALSE)`. To help cache large processing for the next run. Does you r large dataset crash Rmarkdown when you run it? you might need to use Shiny for the large dataset if Rmarkdown crashes? – Daniel_j_iii Nov 13 '20 at 18:27
  • You could also sample your large dataset. – Daniel_j_iii Nov 13 '20 at 18:34

0 Answers0