I would like to make an interactive rmarkdown using runtime:shiny in the yaml header.
I load my single-cell Rnaseq data into a sparse matrix and make it reactive using the reactive function. But there is a problem because the data seems to be not reactive.
Below is the code I used:
---
title: ""
author: ""
output:
html_document:
toc: true
runtime: shiny
---
```{r init}
library(shiny)
library(Seurat)
library(Matrix)
library(dplyr)
library(ggplot2)
library(here)
selectInput('Sample',
label = 'Old or Young',
choices = c("old","young"))
matrix_dir <- reactive({here(paste0(input$Sample,"/filtered_feature_bc_matrix/"))})
mat <- reactive({Read10X(data.dir = matrix_dir())})
renderPrint(dim(mat()))
```
When I run these commands, only the selection input is displayed. The renderPrint function no responds.
How to make it reactive?
Thank you in advance for any help
You would need to specify an output element to host that print, for example:
---
title: ""
author: ""
output:
html_document:
toc: true
runtime: shiny
---
```{r init}
library(shiny)
selectInput('Sample',
label = 'Old or Young',
choices = c("old","young"))
```
```{r print, echo=FALSE}
verbatimTextOutput("myprint")
output$myprint <- renderPrint(input$Sample)
```
Related
This question is similar, but not identical to this one.
Basically, I have a number of tables that I would like to show in tabsets using DT::datatable(). Unfortunately, I can't figure out how.
The following code works, but I need to manually type all the code:
---
title: "Untitled"
format: html
---
```{r}
library(DT)
```
::: {.panel-tabset}
### table no. 1
```{r}
#| results: asis
datatable(mtcars)
```
### table no. 2
```{r}
#| results: asis
datatable(mtcars)
```
:::
The following works, but instead of datatable() uses a simple markdown table from pander which does not give the desired effect.
---
title: "Untitled"
format: html
---
```{r}
library(pander)
```
::: {.panel-tabset}
```{r}
#| results: asis
for(i in 1:2) {
cat(sprintf("\n### table no. %d\n\n", i))
cat(pander(mtcars))
}
```
:::
The following code does not work, and I don't know how to make it work:
---
title: "Untitled"
format: html
---
```{r}
library(DT)
```
::: {.panel-tabset}
```{r}
#| results: asis
for(i in 1:2) {
cat(sprintf("\n### table no. %d\n\n", i))
print(datatable(mtcars))
}
```
:::
This is a known issue in the context of RMarkdown. But as it turns out the solution for RMarkdown also works for Quarto.
Following this answer related to the same issue in RMarkdown [Discalimer: The answer is from me] you could achieve your desired result by first making sure that the javascript dependencies needed for DT are included in your document and by wrapping the datatable call inside htmltools::tagList:
---
title: "Untitled"
format: html
---
```{r}
library(DT)
```
```{r, include=FALSE}
# Init Step to make sure that the dependencies are loaded
htmltools::tagList(datatable(mtcars))
```
::: {.panel-tabset}
```{r}
#| results: asis
for(i in 1:2) {
cat(sprintf("\n### table no. %d\n\n", i))
print(htmltools::tagList(datatable(mtcars)))
}
```
:::
I am making my code more modular and would like to run multiple RMarkdown files from one overall RMarkdown. I believe I could do this if I translated all my RMarkdown files to .R scripts and used source(), but I like the document-like nature of RMarkdown and I can describe what I'm doing as I'm doing it in plain text.
The goal is to wrangle data and export a usable .sav file. I want to run clean.rmd from run.rmd, but I don't want any HTML/pdf/etc. output. Removing the output line in the YAML header doesn't prevent output. If there is a way to do this without translating everything to .R scripts, I would be very appreciative. Thank you.
clean.rmd: Script that does the cleaning
---
title: "clean"
author: "jrcalabrese"
date: "12/30/2021"
output: html_document
---
```{r}
library(tidyverse)
library(haven)
```
```{r}
data(cars)
cars <- cars %>%
mutate(newvar = speed + dist)
```
```{r}
write_spss(cars, "~/Documents/cars_new.sav", compress = FALSE)
```
run.rmd: Script that runs clean.rmd
---
title: "run"
author: "jrcalabrese"
date: "12/30/2021"
output: html_document
---
```{r}
rmarkdown::render("~/Documents/clean.rmd")
```
Thank you for your help! This function works:
---
title: "run"
author: "jrcalabrese"
date: "12/30/2021"
#output: html_document
---
```{r}
source_rmd = function(file, ...) {
tmp_file = tempfile(fileext=".R")
on.exit(unlink(tmp_file), add = TRUE)
knitr::purl(file, output=tmp_file)
source(file = tmp_file, ...)
}
```
```{r}
source_rmd("~/Documents/clean.rmd")
```
I'm trying to incorporate an Rmd I have been using into a flexdashboard. I'm curious if it is possible to isolate an uploaded file and use it as-is rather than writing a bunch of reactive functions. If this is my template, is it possible to get a static object named df that the child document can go ahead and run with?
---
title: "help"
runtime: shiny
output:
flexdashboard::flex_dashboard:
orientation: columns
---
```{r}
fileInput("data", "select data")
df <- isolate(input$data)
```
```{r, child="some_code.Rmd"}
```
My real example does something completely different but let's say some_code.Rmd looks like this:
---
title: "some code"
output: html_document
---
```{r packages, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, message = FALSE)
library(tidyverse)
```
The data looks like this:
```{r}
as_tibble(df)
```
The numeric data can be summarized with the following means
```{r}
df |>
summarise(across(where(is.numeric), mean)) |>
gather()
```
This ended up working:
knitr::knit() + markdown::markdownToHTML() + HTML() ---> renderUI()
---
title: "help"
runtime: shiny
output:
flexdashboard::flex_dashboard:
orientation: rows
---
Sidebar {.sidebar}
==============================================
```{r file-input}
fileInput("data", "select data")
```
Row
==============================================
```{r knit-child}
observeEvent(input$data, {
df <- isolate(read.csv(input$data$datapath))
new_env <- list2env(list(df = df))
output$res <- renderUI({
knitr::knit("some_code.Rmd", quiet = TRUE, envir = new_env) |>
markdown::markdownToHTML() |>
HTML()
})
})
uiOutput("res")
```
Is is possible to hide some comments in code when kniting using knitr / R markdown? Example:
---
title: "SOSO"
author: "SO"
date: '2017-06-06'
output: pdf_document
---
```{r}
# Generate some data
rnorm(2)
## But keep this comment
```
When kniting I would like the first comment to disapear, but keep the second one somehow.
Here is a quick example of modifying the hook to change knitr behavior.
---
title: "SOSO"
author: "SO"
date: 2017-06-06
output: pdf_document
---
```{r setup-hook, echo=FALSE}
hook_in <- function(x, options) {
x <- x[!grepl("^#\\s+", x)]
paste0("```r\n",
paste0(x, collapse="\n"),
"\n```")
}
knitr::knit_hooks$set(source = hook_in)
```
```{r}
# Generate some data
# Lines that starts with `# ` will be removed from the rendered documents
rnorm(2)
## But keep this comment
## But lines that starts with `## ` will be kept
```
produces this
In fact, you can choose to show any lines of R code by passing numeric indices to the chunk option echo, e.g.
---
title: "SOSO"
author: "SO"
date: '2017-06-06'
output: pdf_document
---
```{r echo=4:7}
# Generate some data
rnorm(2)
## But keep this comment
```
See knitr documentation for more information.
I prepared very simple rmarkdown document with shiny widget. I want to add table of content but although I use toc: yes it doesn't work at all, why?
---
title: "Test document"
author: "XXX"
date: "XXX"
output:
html_document:
toc: yes
runtime: shiny
---
```{r, echo = FALSE}
h2('R package')
br()
h3('Ggplot2')
br()
h4('Mtcars')
library(ggplot2)
data(mtcars)
selectInput('name', 'Choose a cylinder:',
choices = sort(unique(mtcars$cyl)),
selected = sort(unique(mtcars$cyl))[1])
data <- reactive(subset(mtcars,cyl == input$name))
number <- reactive(which(sort(unique(mtcars$cyl)) == input$name))
renderPlot(ggplot(data(),aes(qsec, mpg))+
geom_point(size = 6))
```
Your headers need to be outside the code chunk for the table of content to catch them.
You should then use the appropriate numbers of # instead of the html style to visualize the headers size.
Note also that the toc depth default to three, so in your case mtcars won't appear in the toc, unless you adjust the toc_depth to 4.
This should do what you want :
---
title: "Test document"
author: "XXX"
date: "XXX"
output:
html_document:
toc: TRUE
toc_depth: 4
runtime: shiny
---
## R package
### ggplot2
#### mtcars
```{r, echo = FALSE}
library(ggplot2)
data(mtcars)
selectInput('name', 'Choose a cylinder:',
choices = sort(unique(mtcars$cyl)),
selected = sort(unique(mtcars$cyl))[1])
data <- reactive(subset(mtcars,cyl == input$name))
number <- reactive(which(sort(unique(mtcars$cyl)) == input$name))
renderPlot(ggplot(data(),aes(qsec, mpg))+
geom_point(size = 6))
```
Side note : even if it's working it's more common to use toc: TRUE rather than toc: yes