I am trying to replicate a shiny app with long running tasks and notification of the user of its status and allowing someone to cancel running the analysis. My code is as follows:
library(shiny)
library(promises)
library(future)
plan(multiprocess)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Long run Stoppable MBA Async"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
actionButton('run', 'Run'),
actionButton('cancel', 'Cancel'),
actionButton('status', 'Check Status')
),
# Show the plot
mainPanel(
plotOutput("result")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
transPlot <- reactive({
data <- read.csv("data/MBA_Online.csv")
data %>%
mutate(tDate=as.Date(Date)) %>%
filter(tDate >= as.Date(input$dRange[1]) & tDate <= as.Date(input$dRange[2])) %>%
# dplyr::mutate(Month = as.factor(month(as.Date(Date)))) %>%
mutate(Month=as.factor(month(Date))) %>%
# dplyr::rename(item = Item) %>%
dplyr::group_by(Month) %>%
# dplyr::summarise(n_distinct(Transaction)) %>%
dplyr::summarize(Transactions = n_distinct(Transaction)) %>%
# dplyr::summarise(Count=n()) %>%
ggplot(aes(x=Month, y = Transactions, fill = Month)) +
geom_bar(stat="identity") +
geom_label(aes(label= format(Transactions, big.mark = ",")))+
theme(legend.position="none")+
theme(panel.background = element_blank())+
labs(x = "Month", y = "Transactions", title = "Transactions per month")
})
N <- 10
# status file
status_file <- tempfile()
get_status <- function(){
scan(status_file, what = "character",sep = "\n")
}
set_status <- function(msg){
write(msg, status_file)
}
fire_interupt <- function(){
set_status("interrupt")
}
fire_ready <- function(){
set_status("Ready")
}
fire_running <- function(perc_complete){
if(missing(perc_complete))
msg <- "Running..."
else
msg <- paste0("Running...", perc_complete, "%
Complete")
set_status(msg)
}
interrupted <- function(){
get_status() == "interrupt"
}
#Delete file at end of session
onStop(function(){
print(status_file)
if(file.exists(status_file))
unlink(status_file)
})
# create status file
fire_ready()
nclicks <- reactiveVal(0)
result_val <- reactiveVal()
observeEvent(input$run,{
# Don't do anything if analysis is already being run
if(nclicks() !=0){
showNotification("Already running analysis")
return(NULL)
}
#increment clicks and prevent concurent analysis
nclicks(nclicks() + 1)
result_val(data.frame(Status = "Running..."))
fire_running()
result <- future({
print("Running...")
for(i in 1:N){
# Long running task
Sys.sleep(1)
# Check for user interrupts
if(interrupted()){
print("Stopping...")
}
# Notify staus file of progress
fire_running(100*i/N)
}
# Some results
transPlot()
###
}) %...>% result_val()
# Catch interrupt (or any other error) and notify user
result <- catch(result,
function(e){
result_val(NULL)
print(e$message)
showNotification(e$message)
})
# After the promise has been evaluated set nclicks to 0
#to allow for another run
result <- finally(result,
function(){
fire_ready()
nclicks(0)
})
# Return something other than the promise so shiny remains responsive
NULL
})
output$result <- renderPlot({
req(result_val())
})
# Register user interrupt
observeEvent(input$cancel,{
print("Cancel")
fire_interupt
})
# Let user get analysis progress
observeEvent(input$status,{
print("Status")
showNotification(get_status)
})
}
# Run the application
shinyApp(ui = ui, server = server)
Shiny applications not supported in static R Markdown documents
Created on 2019-06-21 by the reprex package (v0.2.1)
However i am encountering some reactive error which is shown below and i am unable to resolve it. Kindly assist:
[1] "Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)"
Data set example is below:
Transaction Item Date Quantity CustomerID Amount monthYear
1 547663 DOG BOWL VINTAGE CREAM 16:00.0 10 16022 42.5 Mar-11
2 547663 BEST DAD CANDLE LETTERS 16:00.0 2 16022 1.7 Mar-11
3 547663 VINTAGE CREAM DOG FOOD CONTAINER 16:00.0 1 16022 8.5 Mar-11
4 547663 CAT BOWL VINTAGE CREAM 16:00.0 10 16022 32.5 Mar-11
5 547663 BLACK AND WHITE DOG BOWL 16:00.0 2 16022 5.9 Mar-11
6 547663 PINK DOG BOWL 16:00.0 2 16022 5.9 Mar-11
7 547663 BLACK AND WHITE DOG BOWL 16:00.0 2 16022 5.9 Mar-11
8 547663 BLACK AND WHITE CAT BOWL 16:00.0 2 16022 4.2 Mar-11
9 547663 PINK CAT BOWL 16:00.0 2 16022 4.2 Mar-11
10 547663 IVORY DINER WALL CLOCK 16:00.0 1 16022 8.5 Mar-11
11 547663 IVORY DINER WALL CLOCK 16:00.0 1 16022 8.5 Mar-11
12 547664 DOORMAT RED RETROSPOT 16:00.0 1 13069 7.95 Mar-11
13 547664 TEA TIME PARTY BUNTING 16:00.0 1 13069 4.95 Mar-11
14 547664 PARTY BUNTING 16:00.0 1 13069 4.95 Mar-11
15 547664 BLUE HAPPY BIRTHDAY BUNTING 16:00.0 1 13069 5.45 Mar-11
16 547664 RED RETROSPOT PICNIC BAG 16:00.0 2 13069 5.9 Mar-11
17 547664 KINGS CHOICE GIANT TUBE MATCHES 16:00.0 1 13069 2.55 Mar-11
18 547664 BLUE POLKADOT COFFEE MUG 16:00.0 12 13069 4.68 Mar-11
19 547664 ROUND SNACK BOXES SET OF4 WOODLAND 16:00.0 1 13069 2.95 Mar-11
20 547664 ROUND SNACK BOXES SET OF 4 FRUITS 16:00.0 1 13069 2.95 Mar-11
21 547664 SET OF 4 PANTRY JELLY MOULDS 16:00.0 2 13069 2.5 Mar-11
22 547664 BLUE POLKADOT EGG CUP 16:00.0 6 13069 2.34 Mar-11
23 547664 RED EGG SPOON 16:00.0 12 13069 1.44 Mar-11
24 547664 SINGLE HEART ZINC T-LIGHT HOLDER 16:00.0 6 13069 5.7 Mar-11
25 547664 HANGING HEART ZINC T-LIGHT HOLDER 16:00.0 6 13069 5.1 Mar-11
26 547664 WHITE SPOT BLUE CERAMIC DRAWER KNOB 16:00.0 9 13069 11.25 Mar-11
27 547664 GARLAND WOODEN HAPPY EASTER 16:00.0 1 13069 1.25 Mar-11
28 547664 RECYCLING BAG RETROSPOT 16:00.0 10 13069 21 Mar-11
29 547664 TOY TIDY SPACEBOY 16:00.0 10 13069 21 Mar-11
30 547664 LOVE HEART TRINKET POT 16:00.0 10 13069 3.9 Mar-11
31 547664 ASSTD MULTICOLOUR CIRCLES MUG 16:00.0 12 13069 4.68 Mar-11
32 547664 MULTICOLOUR SPRING FLOWER MUG 16:00.0 12 13069 4.68 Mar-11
33 547664 YELLOW BREAKFAST CUP AND SAUCER 16:00.0 2 13069 0.78 Mar-11
34 547664 PINK BREAKFAST CUP AND SAUCER 16:00.0 2 13069 0.78 Mar-11
35 547664 TRAVEL CARD WALLET UNION JACK 16:00.0 5 13069 2.1 Mar-11
36 547664 TRAVEL CARD WALLET SUKI 16:00.0 5 13069 2.1 Mar-11
37 547664 TRAVEL CARD WALLET RETROSPOT 16:00.0 5 13069 2.1 Mar-11
38 547664 TRAVEL CARD WALLET VINTAGE TICKET 16:00.0 5 13069 2.1 Mar-11
39 547664 SMALL LICORICE DES PINK BOWL 16:00.0 40 13069 16.8 Mar-11
40 547664 CERAMIC BOWL WITH LOVE HEART DESIGN 16:00.0 4 13069 3.32 Mar-11
41 547664 SMALL CHOCOLATES PINK BOWL 16:00.0 24 13069 10.08 Mar-11
42 547664 BISCUITS SMALL BOWL LIGHT BLUE 16:00.0 24 13069 10.08 Mar-11
43 547664 MIXED NUTS LIGHT GREEN BOWL 16:00.0 24 13069 10.08 Mar-11
44 547664 SMALL MARSHMALLOWS PINK BOWL 16:00.0 24 13069 10.08 Mar-11
45 547664 SMALL DOLLY MIX DESIGN ORANGE BOWL 16:00.0 40 13069 16.8 Mar-11
46 547664 PINK HEART SHAPE EGG FRYING PAN 16:00.0 2 13069 3.3 Mar-11
47 547664 BLUE POLKADOT PUDDING BOWL 16:00.0 6 13069 2.34 Mar-11
48 547664 CUBIC MUG PINK POLKADOT 16:00.0 6 13069 2.34 Mar-11
49 547664 BASKET OF TOADSTOOLS 16:00.0 1 13069 1.25 Mar-11
50 547664 WOOD S/3 CABINET ANT WHITE FINISH 16:00.0 1 13069 8.95 Mar-11
51 547664 VICTORIAN SEWING BOX MEDIUM 16:00.0 1 13069 7.95 Mar-11
52 547664 SMALL CHOCOLATES PINK BOWL 16:00.0 8 13069 3.36 Mar-11
53 547664 LOVE BUILDING BLOCK WORD 16:00.0 1 13069 5.95 Mar-11
54 547664 PAPER CHAIN KIT EMPIRE 16:00.0 1 13069 2.95 Mar-11
55 547664 GARLAND WOODEN HAPPY EASTER 16:00.0 2 13069 2.5 Mar-11
56 547664 BLUE POLKADOT BEAKER 16:00.0 12 13069 4.68 Mar-11
57 547664 RED POLKADOT COFFEE MUG 16:00.0 12 13069 4.68 Mar-11
58 547664 BLUE POLKADOT COFFEE MUG 16:00.0 12 13069 4.68 Mar-11
59 547664 RED POLKADOT BEAKER 16:00.0 12 13069 4.68 Mar-11
60 547665 PACK OF 20 NAPKINS PANTRY DESIGN 17:00.0 12 14256 10.2 Mar-11
61 547665 JAM MAKING SET WITH JARS 17:00.0 12 14256 45 Mar-11
62 547665 JAM MAKING SET PRINTED 17:00.0 12 14256 17.4 Mar-11
63 547665 SET 2 PANTRY DESIGN TEA TOWELS 17:00.0 6 14256 19.5 Mar-11
64 547665 SET OF 3 CAKE TINS PANTRY DESIGN 17:00.0 6 14256 29.7 Mar-11
65 547665 PANTRY ROLLING PIN 17:00.0 6 14256 22.5 Mar-11
66 547665 3 TRADITIONAl BISCUIT CUTTERS SET 17:00.0 6 14256 12.6 Mar-11
67 547665 ROUND SNACK BOXES SET OF4 WOODLAND 17:00.0 6 14256 17.7 Mar-11
68 547665 BLACK KITCHEN SCALES 17:00.0 4 14256 34 Mar-11
69 547665 TABLECLOTH RED APPLES DESIGN 17:00.0 2 14256 19.9 Mar-11
70 547665 COFFEE MUG APPLES DESIGN 17:00.0 6 14256 15.3 Mar-11
71 547665 COFFEE MUG PEARS DESIGN 17:00.0 6 14256 15.3 Mar-11
72 547665 SET OF 2 TEA TOWELS APPLE AND PEARS 17:00.0 6 14256 17.7 Mar-11
73 547665 BISCUIT TIN VINTAGE GREEN 17:00.0 2 14256 13.5 Mar-11
74 547665 BISCUIT TIN VINTAGE RED 17:00.0 2 14256 13.5 Mar-11
75 547665 ROUND CAKE TIN VINTAGE GREEN 17:00.0 2 14256 15.9 Mar-11
76 547665 ROUND CAKE TIN VINTAGE RED 17:00.0 2 14256 15.9 Mar-11
77 547665 3 TIER CAKE TIN GREEN AND CREAM 17:00.0 1 14256 14.95 Mar-11
78 547665 CREAM SWEETHEART EGG HOLDER 17:00.0 4 14256 19.8 Mar-11
79 547665 SWEETHEART CAKESTAND 3 TIER 17:00.0 3 14256 29.85 Mar-11
80 547665 WHITE WIRE EGG HOLDER 17:00.0 6 14256 45 Mar-11
81 547666 PICNIC BASKET WICKER SMALL 23:00.0 16 14235 79.2 Mar-11
82 547666 WRAP DOILEY DESIGN 23:00.0 25 14235 10.5 Mar-11
83 547666 WRAP DOLLY GIRL 23:00.0 25 14235 10.5 Mar-11
84 547666 SET OF 3 NOTEBOOKS IN PARCEL 23:00.0 24 14235 39.6 Mar-11
85 547666 WRAP, BILLBOARD FONTS DESIGN 23:00.0 25 14235 10.5 Mar-11
86 547666 FANCY FONTS BIRTHDAY WRAP 23:00.0 25 14235 10.5 Mar-11
87 547666 JUMBO BAG STRAWBERRY 23:00.0 40 14235 78 Mar-11
88 547666 JUMBO BAG RED RETROSPOT 23:00.0 40 14235 78 Mar-11
89 547666 SET/5 RED RETROSPOT LID GLASS BOWLS 23:00.0 6 14235 17.7 Mar-11
90 547666 RED RETROSPOT BUTTER DISH 23:00.0 3 14235 14.85 Mar-11
91 547666 JAM MAKING SET PRINTED 23:00.0 12 14235 17.4 Mar-11
92 547666 SET OF 3 CAKE TINS PANTRY DESIGN 23:00.0 24 14235 102 Mar-11
93 547666 SET OF 6 SPICE TINS PANTRY DESIGN 23:00.0 24 14235 82.8 Mar-11
94 547666 GIN AND TONIC MUG 23:00.0 12 14235 15 Mar-11
95 547666 FILIGRIS HEART WITH BUTTERFLY 23:00.0 48 14235 60 Mar-11
96 547666 SET 12 RETRO WHITE CHALK STICKS 23:00.0 48 14235 20.16 Mar-11
97 547666 GINGHAM ROSE WRAP 23:00.0 25 14235 10.5 Mar-11
98 547666 TEA BAG PLATE RED RETROSPOT 23:00.0 24 14235 20.4 Mar-11
99 547666 RECIPE BOX PANTRY YELLOW DESIGN 23:00.0 6 14235 17.7 Mar-11
100 547666 FRENCH BLUE METAL DOOR SIGN No 23:00.0 10 14235 12.5 Mar-11
101 547666 FRENCH BLUE METAL DOOR SIGN 0 23:00.0 10 14235 12.5 Mar-11
102 547666 FRENCH BLUE METAL DOOR SIGN 9 23:00.0 10 14235 12.5 Mar-11
103 547666 FRENCH BLUE METAL DOOR SIGN 8 23:00.0 10 14235 12.5 Mar-11
104 547666 FRENCH BLUE METAL DOOR SIGN 7 23:00.0 10 14235 12.5 Mar-11