3

I am trying to create a shiny application to help my coworkers sort, highlight, organize, etc, a load of qualitative data. Basically, I want it to display some narrative text, allow uses to copy/past text or take notes in a text area, and then print those notes to a physical printer. I have managed to create a minimal app which allows users to enter text in a text field, but I cannot figure out how to send the contents to a physical printer

This is as far as I can get...

library(shiny)
library(noteMD)

ui <- fluidPage(

  sidebarLayout(
  sidebarPanel(),

  mainPanel(
     tags$textarea("", id='input_notes', rows =20, style = 'width:100%;'),
     actionButton('input_notes', 'Print Notes'))))

server <- function(input, output) {
output$print_notes <- reactive({input$input_notes}) #????????????????
}

shinyApp(ui = ui, server = server)

I ran across the noteMD package, which seems promising, here: https://www.rdocumentation.org/packages/noteMD/versions/0.1.0 I cant seem to get this to work.

Ultimately, I would like to give a clean way for users to print their own notes with one or two clicks.

roberty boberty
  • 175
  • 2
  • 8
  • 1
    This might help: https://stackoverflow.com/questions/41459361/rshiny-print-current-page – Marian Minar Jun 15 '19 at 00:38
  • Yes, thanks, I have used this code before. However, I only want to print out notes the user has entered into the text area. This codes prints the entire page. – roberty boberty Jun 25 '19 at 18:14
  • I found the following which allows users to create a markdown report and is a workable solution for me: http://shiny.rstudio.com/articles/generating-reports.html But, while working on this, I also ran across the following issue with downloadHandler which was sending me in circles. downloadHandler drops the file extension on a windows machine and in different browsers... – roberty boberty Jun 25 '19 at 18:36

1 Answers1

4

Here is a simple solution with the library jQuery print:

library(shiny)

ui <- fluidPage(
  tags$head(
    tags$script(src = "https://cdnjs.cloudflare.com/ajax/libs/jQuery.print/1.6.0/jQuery.print.min.js")
  ),
  textAreaInput("textarea", "Type some text to be printed"), 
  actionButton("print", "Print", onclick = "$('#textarea').print();")
)

server <- function(input, output){}

shinyApp(ui, server)

However, this also prints the frame of the text area. The following solution only prints the text:

library(shiny)

js <- paste(
  "function print(){",
  "  var content = $('#textarea').val();",
  "  content = content.replace(/([^>\\r\\n]?)(\\r\\n|\\n\\r|\\r|\\n)/g, '$1' + '</br>' + '$2')",
  "  var $p = $('<p></p>');",
  "  $p.html(content);",
  "  $p.print();",
  "}", 
  sep = "\n"
)

ui <- fluidPage(
  tags$head(
    tags$script(HTML(js)),
    tags$script(src = "https://cdnjs.cloudflare.com/ajax/libs/jQuery.print/1.6.0/jQuery.print.min.js")
  ),
  textAreaInput("textarea", "Type some text to be printed"), 
  actionButton("print", "Print", onclick = "print();")
)

server <- function(input, output){}

shinyApp(ui, server)
Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225