Reputation: 113
I have an app that looks like this
library(shiny)
library(DT)
ui <- fluidPage(
sidebarPanel(numericInput("c1","Example", NA),
tags$p(actionButton("update", "Update")),
tags$p(actionButton("reset", "Clear")),
tags$p(actionButton("restart", "Restart")),
textOutput("count"),
htmlOutput("display")
),
mainPanel( DT::dataTableOutput("example")
)
)
server <- function(input, output, session) {
# stores the current data frame
values <- reactiveVal(data.frame(A=1, B=2, C=3))
# update values table on button click
observeEvent(input$update,{
old_values <- values()
A_new <- input$c1
B_new <- A_new + 2
C_new <- A_new + B_new
new_values <- data.frame(A=A_new, B=B_new, C=C_new)
# attach the new line to the old data frame here:
new_df <- rbind(old_values, new_values)
#store the result in values variable
values(new_df)
#reset the numeric input to NA
updateNumericInput(session, "c1", "Example", NA)
})
#Delete last row
deleteEntry <- observeEvent(input$reset,{
values( values()[-nrow(values()),])
})
updateCounter <- reactiveValues(i = 0)
##This output value must be reset to 0
output$count <- renderText({
paste0("Iteractions: ", updateCounter$i)
})
observe({
input$update
isolate({
updateCounter$i <- updateCounter$i + 1
})
})
observe({
input$reset
isolate(updateCounter$i <- updateCounter$i - 1)
})
#Restart
defaultvalues <- observeEvent(input$restart, {
isolate(updateCounter$i == 0)
values( values()[1,])
updateCounter <<- reactiveValues(i = 0)
})
Total <- reactive({ colSums(values()[3]) })
output$display <- renderText({
partialsum <-Total()
if(updateCounter$i==3) {
value3 <<- partialsum
return(paste("Partial result", value3))
}
if(updateCounter$i<3){
return()
}
if(updateCounter$i==0){
return()
}
if(updateCounter$i>3) {
display1 <- paste("Partial result", value3)
display2 <- paste("Total result", partialsum)
twolinesdisplay<<- HTML(paste(display1, display2, sep = '<br/>'))
return(twolinesdisplay)
}
})
# # Print the table on mail panel
output$example <- DT::renderDataTable({ return(values()) })
}
shinyApp(ui = ui, server = server)
When interactions equals 3 is displayed the sum of column 3, more than 3 displays the partial sum and the current total sum. Each time that the restart button is clicked, table is reset to default values and process starts again. But the interaction counter does not reset to zero.
I have tried to use the idea of this post but without success. Any suggestion?
Upvotes: 0
Views: 609
Reputation: 7685
Is this what you are looking for?
library(shiny)
library(DT)
ui <- fluidPage(
sidebarPanel(numericInput("c1", "Example", NA),
tags$p(actionButton("update", "Update")),
tags$p(actionButton("reset", "Clear")),
tags$p(actionButton("restart", "Restart")),
textOutput("count"),
htmlOutput("display")
),
mainPanel( DT::dataTableOutput("example"))
)
server <- function(input, output, session) {
# stores the current data frame
values <- reactiveVal(data.frame(A = 1, B = 2, C = 3))
# update values table on button click
observeEvent(input$update,{
old_values <- values()
A_new <- input$c1
B_new <- A_new + 2
C_new <- A_new + B_new
new_values <- data.frame(A = A_new, B = B_new, C = C_new)
# attach the new line to the old data frame here:
new_df <- rbind(old_values, new_values)
#store the result in values variable
values(new_df)
#reset the numeric input to NA
updateNumericInput(session, "c1", "Example", NA)
})
#Delete last row
deleteEntry <- observeEvent(input$reset,{
values( values()[-nrow(values()),])
})
updateCounter <- reactiveValues(i = 0)
##This output value must be reset to 0
output$count <- renderText({
paste0("Iteractions: ", updateCounter$i)
})
observe({
input$update
isolate({
updateCounter$i <- updateCounter$i + 1
})
})
observe({
input$reset
isolate(updateCounter$i <- updateCounter$i - 1)
})
#Restart
defaultvalues <- observeEvent(input$restart, {
isolate(updateCounter$i == 0)
values( values()[1,])
updateCounter$i <- 0
})
Total <- reactive({ colSums(values()[3]) })
output$display <- renderText({
partialsum <-Total()
if(updateCounter$i==3) {
value3 <<- partialsum
return(paste("Partial result", value3))
}
if (updateCounter$i < 3){
return()
}
if (updateCounter$i == 0){
return()
}
if(updateCounter$i > 3) {
display1 <- paste("Partial result", value3)
display2 <- paste("Total result", partialsum)
twolinesdisplay<<- HTML(paste(display1, display2, sep = '<br/>'))
return(twolinesdisplay)
}
})
# # Print the table on mail panel
output$example <- DT::renderDataTable({ values() })
}
shinyApp(ui, server)
It seems like
updateCounter$i <- 0
works while
updateCounter <- reactiveValues(i = 0)
does not.
Upvotes: 1