Last active
December 31, 2015 14:51
-
-
Save talegari/165a689f8b6f8b9dd35f to your computer and use it in GitHub Desktop.
Page ranges to print multiple pages on both sides of a sheet -- an R script using shiny
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Page ranges to print on both sides of a sheet ---- | |
# author : Srikanth KS (talegari) | |
# contact : gmail me at sri dot teach | |
# version : 1 | |
# last updated : 31st Dec 2015 | |
# purpose : If your printer prints on one side(you manually | |
# turn and feed the papers) and you need to print | |
# multiple pages per sheet | |
# load libraries ---- | |
library('shiny') | |
# ui ---- | |
ui <- | |
pageWithSidebar( | |
headerPanel("Page ranges to print on both sides of a sheet") | |
,sidebarPanel( | |
numericInput(inputId = 'number_of_pages' | |
, label = 'Number of Pages' | |
, value = 50 | |
, min = 2 | |
, max = 10000), | |
numericInput(inputId = 'number_of_pages_per_sheet' | |
, label = 'Number of Pages per sheet' | |
, value = 2 | |
, min = 1 | |
, max = 100), | |
p('----'), | |
a('code on github gist',href = 'https://gist.github.com/talegari/165a689f8b6f8b9dd35f') | |
) | |
,mainPanel( | |
p('If your printer prints on one side(you manually turn and feed the papers) and you need to print multiple pages per sheet') | |
,h4('First print these,') | |
,verbatimTextOutput('odd_pages') | |
,h4('Turn and insert the printed pages and print these') | |
,verbatimTextOutput('even_pages') | |
) | |
) # end of pageWithSidebar | |
# define print_both_sides ---- | |
print_both_sides <- function(n_pages | |
,n_pages_per_sheet){ | |
stopifnot(n_pages_per_sheet < n_pages) | |
# left/right ends of pages that go into one sheet | |
left <- seq(from = 1 | |
,to = n_pages | |
,by = n_pages_per_sheet) | |
right <- seq(from = n_pages_per_sheet | |
,to = n_pages | |
,by = n_pages_per_sheet) | |
# handle oddity | |
ell <- length(left) | |
if (ell > length(right)) | |
right[ell] <- tail(left,1) | |
# combine left right ends | |
print_vector <- vapply(1:ell | |
,function(x){ | |
paste(left[x],right[x],sep = '-') | |
} | |
,character(1) | |
) | |
# split into front/back of sheet | |
ell2 <- length(print_vector) | |
odd_pages_vector <- print_vector[seq(from = 1,to = ell2,by = 2)] | |
even_pages_vector <- print_vector[seq(from = 2,to = ell2,by = 2)] | |
odd_pages <- do.call('paste' | |
,as.list(c(odd_pages_vector,sep = ','))) | |
even_pages <- do.call('paste' | |
,as.list(c(even_pages_vector,sep = ','))) | |
# screen printing | |
list(odd_pages = odd_pages | |
,even_pages = even_pages) | |
} | |
# server ---- | |
server <- | |
function(input,output){ | |
np <- reactive({ | |
input$number_of_pages | |
}) | |
nps <- reactive({ | |
input$number_of_pages_per_sheet | |
}) | |
os <- reactive({ | |
print_both_sides(np(),nps()) | |
}) | |
output$odd_pages <- renderText({ | |
os()$odd_pages | |
}) | |
output$even_pages <- renderText({ | |
os()$even_pages | |
}) | |
} | |
# shinyall call ---- | |
shinyApp(ui = ui, server = server) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment