Skip to content

Commit

Permalink
Allow to save and restore session
Browse files Browse the repository at this point in the history
  • Loading branch information
nfrerebeau committed Dec 15, 2023
1 parent 2b0c97a commit 5a6bed3
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 54 deletions.
29 changes: 22 additions & 7 deletions R/janus-internal.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ is_set <- function(x) {
!is.null(x) && x != ""
}

assert_csv <- function(x) {
validate(need(x, message = "Import a CSV file first."))
}

url_tesselle <- function(package = NULL, campaign = TRUE) {
mtm <- if (campaign) "?mtm_campaign=shiny" else ""
if (is.null(package)) {
Expand All @@ -17,22 +21,33 @@ url_tesselle <- function(package = NULL, campaign = TRUE) {
}
}

assert_csv <- function(x) {
validate(need(x, message = "Import a CSV file first."))
}

cite_markdown <- function(x = NULL) {
cite_package <- function(x = NULL) {
x <- c("janus", x)
lapply(
X = x,
FUN = function(x) {
bib <- format(utils::citation(x), style = "text")
markdown(bib)
txt <- paste0(vapply(X = bib, FUN = markdown, FUN.VALUE = character(1)))
HTML(txt)
}
)
}

info_markdown <- function() {
cite_article <- function(author, year, doi, text = FALSE) {
url <- sprintf("https://doi.org/%s", doi)
link <- tags$a(year, href = url, target = "_blank", .noWS = "outside")

if (text) {
tags$span(author, "(", link, ")")
} else {
tags$span(
paste0("(", author, ", "), link, ")",
.noWS = c("after-begin", "before-end")
)
}
}

info_session <- function() {
info <- paste0(utils::capture.output(utils::sessionInfo()), collapse = "\n")
markdown(sprintf("```\n%s\n```", info))
}
6 changes: 3 additions & 3 deletions R/module_footer.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ module_footer_ui <- function(id) {
actionLink(inputId = ns("session"), label = "Session info"),
HTML(" &middot; "),
tags$a(href = "https://github.com/tesselle/janus",
rel = "external", title = "Code", "Source code"),
target = "_blank", rel = "external", "Source code"),
HTML(" &middot; "),
tags$a(href = "https://github.com/tesselle/janus/issues",
rel = "external", title = "Issue", "Report a bug or request")
target = "_blank", rel = "external", "Report a bug or request")
)
)
}
Expand All @@ -41,7 +41,7 @@ module_footer_server <- function(id) {
showModal(
modalDialog(
title = "Session Info",
info_markdown(),
info_session(),
size = "xl",
easyClose = TRUE,
footer = modalButton("Close")
Expand Down
99 changes: 60 additions & 39 deletions R/module_home.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module_home_ui <- function(id) {
"This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY."
),
h3("Citation"),
h3("How to cite"),
tags$p(
"If you use this application in your research, you must report
and cite it properly to ensure transparency of your results.
Expand All @@ -38,7 +38,7 @@ module_home_ui <- function(id) {
by the research community."
),
tags$p("To cite in your publications, please use:"),
cite_markdown(),
cite_package(),
tags$p(
class = "logo",
tags$a(href = "https://www.archeosciences-bordeaux.fr", rel = "external",
Expand All @@ -49,8 +49,9 @@ module_home_ui <- function(id) {
),
tabPanel(
title = "About",
h3("What is", tags$i("tesselle"), "?"),
tags$img(src="static/tesselle.png", alt="Smiley face", style="float:right;width:150px;margin:0 10px;"),
h3("What is", tags$i("tesselle", .noWS = "after"), "?"),
tags$img(src="static/tesselle.png", alt="tesselle logo",
style="float:right;width:150px;margin:0 10px;"),
tags$p(
"This app is a part of the", tags$strong("tesselle"), "project,",
"a collection of packages for research and teaching in archaeology.
Expand All @@ -61,20 +62,40 @@ module_home_ui <- function(id) {
),
tags$p(
"For more information and relevant links see:",
tags$a(href = url_tesselle(), rel = "external", "tesselle.org.")
tags$a("tesselle.org", href = url_tesselle(),
target = "_blank", rel = "external", .noWS = "after"), "."
),
# h3("Who is", tags$i("tesselle"), "for?"),
h3("License"),
tags$p(
"This app is distributed as a free and open source",
tags$a(href = url_tesselle("janus"), rel = "external", "R package.")
tags$a("R package", href = url_tesselle("janus"),
target = "_blank", rel = "external", .noWS = "after"), "."
),
tags$p(
"You can redistribute it and/or modify it under the terms of the
GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version."
)
),
tabPanel(
title = "Save and restore",
fileInput(
inputId = ns("session_restore"),
label = "Restore session",
multiple = FALSE,
accept = ".rds"
),
textInput(
inputID = ns("session_name"),
label = "Please enter a session name (optional):"
),
actionButton(
inputId = ns("session_save"),
label = "Save session",
icon = icon("download")
)
)
)
) # mainPanel
Expand All @@ -93,38 +114,38 @@ module_home_ui <- function(id) {
#' @export
module_home_server <- function(id) {
moduleServer(id, function(input, output, session) {
## Render ------------------------------------------------------------------
output$session <- renderPrint({ utils::sessionInfo() })

## Bookmark ----------------------------------------------------------------
# onBookmark(function(state) {
# saved_time <- Sys.time()
#
# msg <- sprintf("Last saved at %s.", saved_time)
# showNotification(
# ui = msg,
# duration = 5,
# closeButton = TRUE,
# type = "message",
# session = session
# )
# cat(msg, "\n")
#
# # state is a mutable reference object,
# # we can add arbitrary values to it.
# state$values$time <- saved_time
# })
## Bookmark ----------------------------------------------------------------
# onRestore(function(state) {
# msg <- sprintf("Restoring from state bookmarked at %s.", state$values$time)
# showNotification(
# ui = msg,
# duration = 5,
# closeButton = TRUE,
# type = "message",
# session = session
# )
# cat(msg, sep = "\n")
# })
## Bookmark -----
latest_bookmark_url <- reactiveVal()
onBookmarked(
fun = function(url) {
latest_bookmark_url(parseQueryString(url))
}
)
onRestored(function(state) {
showNotification(paste("Restored session:", basename(state$dir)),
duration = 10, type = "message")
})
## Download -----
output$session_save <- downloadHandler(
filename = function() {
session$doBookmark()
if (input$session_name != "") {
tmp_session_name <- sub("\\.rds$", "", input$session_name)
tmp_session_name <- sub("[[:punct:]]", "", tmp_session_name)
tmp_session_name <- paste0(tmp_session_name, ".rds")
} else {
paste(req(latest_bookmark_url()), "rds", sep = ".")
}
},
content = function(file) {
file.copy(from = file.path(
".",
"shiny_bookmarks",
req(latest_bookmark_url()),
"input.rds"
),
to = file)
}
)
})
}
2 changes: 1 addition & 1 deletion inst/coda/global.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ rm(list = ls())

## Set Shiny settings ==========================================================
options(shiny.maxRequestSize = 30*1024^2)
enableBookmarking(store = "server")
enableBookmarking(store = janus::get_option("bookmark"))
10 changes: 8 additions & 2 deletions inst/config.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
default:
production: TRUE
verbose: FALSE
bookmark: disable

test:
production: FALSE
verbose: TRUE
verbose: disable

production:
online:
production: TRUE
verbose: FALSE
bookmark: server
2 changes: 1 addition & 1 deletion inst/seriation/global.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ rm(list = ls())

## Set Shiny settings ==========================================================
options(shiny.maxRequestSize = 30*1024^2)
enableBookmarking(store = "server")
enableBookmarking(store = janus::get_option("bookmark"))
2 changes: 1 addition & 1 deletion inst/ternary/global.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ rm(list = ls())

## Set Shiny settings ==========================================================
options(shiny.maxRequestSize = 30*1024^2)
enableBookmarking(store = "server")
enableBookmarking(store = janus::get_option("bookmark"))

0 comments on commit 5a6bed3

Please sign in to comment.