skip to Main Content

I’m working on an App that generates large amounts of text in response to user definitional questions. I’m coding text in using HTML in separate modules, and it is becoming very cumbersome to code in all this text mainly due to the HTML coding conventions. Is there a simplified way to pull in the text of a Word document, including formats, into the UI section of a Shiny App, instead of manually coding in all text in HTML? Below is a super-simple example code of my current approach, I wonder if there’s an easier approach than what I’m doing in the HTML() function in the server() section:

library(shiny)

ui <- fluidPage(uiOutput("coxModel"))

server <- function(input, output) {
  output$coxModel <- renderUI(
    tags$div(
      style="text-align:justify",
      HTML(
        "<b>Select</b> from <i>`Multiple-predictors for Cox model`</i> menu to run Cox model.",
      )
    )
  )
}

shinyApp(ui, server)

2

Answers


  1. Chosen as BEST ANSWER

    The cleanest solution for me is to follow limey's suggestion where I:

    1. Saved the formatted paragraph in a Word document
    2. Resaved the document as filtered HTML file
    3. Then accessed the saved html file in Shiny using the markdown package

    Here is the code for this solution:

    library(htmltools)
    library(markdown)
    library(shiny)
    
    text <- readLines("C:\Users\...\OneDrive\Desktop\Sample1.htm")
    html <- markdownToHTML(text)
    
    ui <- fluidPage(
      includeMarkdown(html)
    )
    
    server <- function(input, output) {}
    
    shinyApp(ui, server)
    

    Adapting ismirsehregal's comment, I did the following:

    1. Saved the Word doc as an html file
    2. Right-clicked on the saved html file to reveal the code (see image at the bottom)
    3. Copied the highlighted section of the code and pasted it into the R code, under the HTML() function

    And it renders correctly!

    Here's the revised code:

    library(shiny)
    
    ui <- fluidPage(uiOutput("coxModel"))
    
    server <- function(input, output) {
      output$coxModel <- renderUI(
        tags$div(
          style="text-align:justify",
          HTML("               
               <body lang=EN-GB style='word-wrap:break-word'>
               <div class=WordSection1>
               <p class=MsoNormal>Testing reading of a <b>Word</b> document into an <i><span
               style='color:#0070C0'>R Shiny App</span></i></p>
               </div>
               </body>
               "
          )
        )
      )
    }
    
    shinyApp(ui, server)
    

    Here's the code that was revealed when I right-clicked on the HTML file:

    enter image description here


  2. I wonder if there’s an easier approach than what I’m doing in the HTML() function…

    You can use shiny tags like so:

    library(shiny)
    
    ui <- fluidPage(uiOutput("coxModel"))
    
    server <- function(input, output) {
      output$coxModel <- renderUI(
        tags$div(
          style="text-align:justify",
    
          tags$p(
            tags$b("Select"),
            " from",
            tags$i(
              " `Multiple-predictors for Cox model`"
            ),
            " menu to run Cox model."
          )
        )
      )
    }
    
    shinyApp(ui, server)
    

    Also, if none of the elements being rendered has interactivity, I’d recommend you put it all in the UI. Like this:

    library(shiny)
    
    ui <- fluidPage(
      tags$div(
        style = "text-align:justify",
        tags$p(
          tags$b("Select"),
          " from",
          tags$i(
            " `Multiple-predictors for Cox model`"
          ),
          " menu to run Cox model."
        )
      )
    )
    
    server <- function(input, output) {
    
    }
    
    shinyApp(ui, server)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search