How can I save R Shiny HTML Output to an external file?

Below are 3 files that make up my shiny app. I would like to save htmlOutput("element_list") to an external file. Is this possible, and, if so, how?

My packages are kept in the global.r file

Global

# In this script include packages, functions, datasets and anything that will be
# used both by UI and server

############################.
##Packages ----
############################.
library(shiny)

My input boxes can be added her and their inputs are saved.

Server

## Define a server for the Shiny app
function(input, output, session) {

  # Track the number of element input boxes to render
  counterElement <- reactiveValues(n = 0)

  # Empty list to store parameters(ie elements, authors,..) text input vaules
  parameters <- reactiveValues()

  #Track the number of input boxes previously
  prevcountElement <-reactiveValues(n = 0)

  #On click: add element input box
  observeEvent(input$ele_add_btn, {
    counterElement$n <- counterElement$n + 1
    prevcountElement$n <- counterElement$n - 1})

  #On click: remove last element input box
  observeEvent(input$ele_rm_btn, {
    if (counterElement$n > 0) {
      counterElement$n <- counterElement$n - 1
      prevcountElement$n <- counterElement$n + 1
    }

  })

  #On click: store values from element input boxes in a list within parameters list
  observeEvent(input$ele_save_btn, {
    if (counterElement$n > 0) {
      parameters$elementName <- NA
      parameters$elementType <- NA
      parameters$elementRole <- NA
      for(i in 1:counterElement$n) {
        parameters$elementName[i] <- input[[paste0("elementName",i)]]
        parameters$elementType[i] <- input[[paste0("elementType",i)]]
        parameters$elementRole[i] <- input[[paste0("elementRole",i)]]
      }
        output$element_list <- renderText(
            paste("<b>CREATE (:Element {elementName: '",parameters$elementName,"', elementType: '",parameters$elementType,"', elementRole: ",parameters$elementRole,"});<b/>","<br/><br/>")
        )
      }
    })

  #Store number of element input boxes
  output$counterElement <- renderPrint(print(counterElement$n))

  #code to generate element input boxes
  elementboxes <- reactive({

    n <- counterElement$n

    if (n > 0) {
      # If the no. of element boxes previously where more than zero, then
      #save the text inputs in those text boxes
      if(prevcountElement$n > 0){

        vals = c()
        typ = c()
        role = c()
        if(prevcountElement$n > n){
          lesscnt <- n
          isInc <- FALSE
        }else{
          lesscnt <- prevcountElement$n
          isInc <- TRUE
        }
        for(i in 1:lesscnt){
          inpid = paste0("elementName",i)
          vals[i] = input[[inpid]]
          inptyp = paste0("elementType",i)
          typ[i] = input[[inptyp]]
          inprole = paste0("elementRole",i)
          role[i] = input[[inprole]]
        }
        if(isInc){
          vals <- c(vals, "New element")
          typ <- c(typ, "Construct")
          role <- c(role, "Mediate")
        }

        lapply(seq_len(n), function(i) {
          elementInputs<- tagList(
            textInput(inputId = paste0("elementName", i),
                      label = paste0("Element ", i), value = vals[i]),
            selectInput(inputId = paste0("elementType",i), label = paste0("What type of element is element ",i, "?"),
                        c("Construct" = "Construct","Concept" = "Concept","Process"="Process"), selected = typ[i]),
            selectInput(inputId = paste0("elementRole",i), label = paste0("What role does element ",i, " have?"),
                        c("No Role" = "NULL","Moderator" = "'Moderator'","Mediator" = "'Mediator'"), selected = role[i]))
        })

      }else{
        lapply(seq_len(n), function(i) {
          elementInputs<- tagList(
            textInput(inputId = paste0("elementName", i),
                      label = paste0("Element ", i), value = "New element"),
            selectInput(inputId = paste0("elementType",i), label = paste0("What type of element is element ",i, "?"),
                        c("Construct" = "Construct","Concept" = "Concept","Process"="Process")),
            selectInput(inputId = paste0("elementRole",i), label = paste0("What role does element ",i, " have?"),
                        c("No Role" = "NULL","Moderator" = "'Moderator'","Mediator" = "'Mediator'")))
        })
      }

    }

  })



  #display element input boxes
  output$element_ui <- renderUI({ elementboxes() })

###################################################################################################


#output$downloadData <- downloadHandler("cypherScript.txt",
#content = function(file){
#write_file(c(output$element_list), file)
#}
#)

#output$downloadImage <- downloadHandler(
#  filename = "cypherScript.txt",
#  content = function(file) {
#    ## copy the file from the updated image location to the final download location
#    file.copy(output$element_list, file)
#  }
#)


}

ui.r makes the app display its sections in an intuitive way

UI

# This script includes the user-interface definition of the app.

fluidPage(

  ui <- shinyUI(fluidPage(

    sidebarLayout(
    sidebarPanel(
      ######################.
      # Element Buttons----
      ######################.
      h4("Elements"),
      #adds a new element textbox & selection
      actionButton("ele_add_btn", "Add Element"),
      #removes last element textbox & selection
      actionButton("ele_rm_btn", "Remove Element"),
      #save all element textbox & selection inputs into a list
      actionButton("ele_save_btn", "Save Element List"),
      #prints the number of element input boxes in ui sidebar
      textOutput("counterElement")

 ),
 # displays the input elements
 mainPanel(
   fluidRow(
     ####################.
     # Element Input ----
     ####################.
     column(7,
            h4("Element Boxes"),
            uiOutput("element_ui")),
     ####################.
     # Element Output----
     ####################.
     column(4,
            h4("Element Output"),
            htmlOutput("element_list"))
   ))
 ),

 sidebarLayout(
   sidebarPanel(
     ######################.
     # Download Buttons----
     ######################.
     h4("Save Code"),
     #adds a new textbox & selection
     downloadButton("downloadData","Download"),
   ),
   mainPanel(
     fluidRow(
     ))
 ),


  )#Close fluidpage()
  )#Close shinyUI()


)#CLOSES fluidPage

Any help is appreciated

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.