Skip to content

Instantly share code, notes, and snippets.

@jrosell
Created November 23, 2021 20:05
Show Gist options
  • Save jrosell/329c4ec8e2ba118ce0a5bd277891bf09 to your computer and use it in GitHub Desktop.
Save jrosell/329c4ec8e2ba118ce0a5bd277891bf09 to your computer and use it in GitHub Desktop.
# Instead of a reactive chunk, have knitr convert a character vector "chunk" to HTML within a reactive context.
# Then feed that HTML to renderUI.
# Source: https://community.rstudio.com/t/feed-shiny-text-input-into-python-chunk/16098/3
---
title: "Untitled"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
runtime: shiny
---
```{r setup, include=FALSE}
library(shiny)
library(flexdashboard)
library(knitr)
library(reticulate)
```
Column {data-width=650}
-----------------------------------------------------------------------
### Chart A
```{r, e}
textAreaInput("text", label = h3("Paste Abstract Here"), width = '600px', height = '300px')
actionButton("submit", "Submit")
v <- reactiveValues()
renderUI(v[["py_chunk"]])
observeEvent(
input$submit,
{
var <- input$text
# Each chunk line is an element of the vector
py_code <- c(
"```{python}",
"test = str(r.var)",
"print(test)",
"```"
)
# Make sure knitr's converting to HTML
knitr::render_html()
# Then store the output in the reactive value
v[["py_chunk"]] <- HTML(knitr::knit(text = py_code))
}
)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment