Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
562 views
in Technique[技术] by (71.8m points)

R Shiny, showModal, and rmarkdown-report

I've written an app which renders an r-markdown -report as html inside bsmodal, when user presses button.

It works, but it only works once. If the modal is closed and the button is entered again, the rmarkdown is not rendered anymore. If I place a renderText inside the modal instead of renderUI-part, it works OK. Does someone know why the rmarkdown gets rendered only the first time the button is pressed?

Thanks for any help,

-Kari

Minimized example code provided below.

Shiny code:

setwd("") #Set working directory here

ui <- function(id) {
  
  fluidPage(
    fluidRow(
      column(3, 
        actionButton("button", "Go!")
      )
    )
  )
}

server <- function(input, output) {

  observeEvent(input$button, {
    
   showModal(modalDialog(
     title = "Fancy report",
      tagList(
            renderUI(
              HTML(
                readLines(
                  rmarkdown::render("report.rmd",
                    encoding = "UTF-8",
                    envir = new.env()
                  ), encoding = "UTF-8"
                )
              )
            )
      ),
     easyClose = TRUE
   )
   )    
  })
}
shinyApp(ui = ui, server = server)

rmarkdown (please notice, stack interprets those 3 markdown ticks as code block start and end as well):

---
mainfont: Arial
output: html_document
---
  
```{r echo = FALSE, comment=NA, results='asis', warning=FALSE, message=FALSE}
library(knitr)
library(kableExtra)
library(tidyverse)
library(dplyr)

ds <- head(mtcars)

ds %>% kable("html", escape = F, col.names = NULL, table.attr = "style='width:90%;'")

question from:https://stackoverflow.com/questions/66047472/r-shiny-showmodal-and-rmarkdown-report

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I was able to fix this by using bsModal instead of showModal. In case someone needs this, here's the working Shiny-code (The original r-markdown code is not touched):

setwd("C:/set-rmarkdown-file-location-here")

ui <- function(id) {
  
  fluidPage(
    fluidRow(
      column(3, 
        actionButton("button", "Go!")
      )
    ),
    fluidRow(
      column(10,
        bsModal("modalExample", "fancy report", "button", size = "large",
          uiOutput("report_ui")
        )
      )
    )
  )
}

server <- function(input, output, session) {

  output$report_ui <- renderUI({
    
      HTML(
        readLines(
          rmarkdown::render("report.rmd",
                            encoding = "UTF-8",
                            envir = new.env()
          ), encoding = "UTF-8"
        )
      )
  })
  
}
shinyApp(ui = ui, server = server)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...