13

I'm making an app and I need to add a button to refresh page (same function to press F5). Is there anyone can share a piece of code to implement it?

Thanks a lot!

Mark Zhou
  • 167
  • 1
  • 1
  • 5
  • 1
    what would it do differently than the refresh button in the browser? – Rorschach Jun 15 '15 at 18:49
  • Please do expand on what you mean by "refresh" - do you want to literally do a page refresh or do you just want all the inputs to go back to their initial state? – DeanAttali Jun 15 '15 at 22:38
  • Actually I want all the inputs to go back to the initial values. I have a upload input and I don't know how to reset this input value as null. So I thought refresh page might be a shortcut. @daattali, do you have any idea on how to do it? Thanks! – Mark Zhou Jun 16 '15 at 01:07

2 Answers2

18

I do have a very simple and nice solution but it won't work for a file input.

Here's a solution that'll work for all inputs except a file input:

UPDATE 2017: this solution did not work on file inputs for the first 2 years, but it does now.

library(shiny)
library(shinyjs)
runApp(shinyApp(
  ui = fluidPage(
    shinyjs::useShinyjs(),
    div(
      id = "form",
      textInput("text", "Text", ""),
      selectInput("select", "Select", 1:5),
      actionButton("refresh", "Refresh")
    )
  ),
  server = function(input, output, session) {
    observeEvent(input$refresh, {
      shinyjs::reset("form")
    })
  }
))

When you press "Refresh", all inputs will be reset to their initial values. This is what the poster said in a comment that they actually want to do.

But file inputs are very strange and it's hard to "reset" them. See here. You could hack some JavaScript together to try to almost kind of reset an input field if you want.

However, for completeness, you can also refresh the entire page. The easiest way to do that is with session$reload(). You can also do it with {shinyjs}:

library(shiny)
library(shinyjs)
runApp(shinyApp(
  ui = fluidPage(
    shinyjs::useShinyjs(),
    shinyjs::extendShinyjs(text = "shinyjs.refresh_page = function() { location.reload(); }", functions = "refresh_page"),
    textInput("text", "Text", ""),
    actionButton("refresh", "Refresh")
  ),
  server = function(input, output, session) {
    observeEvent(input$refresh, {
      shinyjs::js$refresh_page()
    })
  }
))

Disclaimer: both these solutions use a package I wrote, shinyjs

DeanAttali
  • 25,268
  • 10
  • 92
  • 118
1

I have a drop-down list input:

selectInput("domain", label = h4("Domain:"), choices = Domain, selected = CurrentDomain) 

The choices set is based on a table in the database. It should change after I add or delete record from the table.

When I was experimenting with your reset or refresh function, the choice set could not reflect the changes and always stay the same. However, when I use the "reload" button provided by the browser, the choice set will update immediately. I am wondering whether you have a reset/refresh solution that is equivalent to the "reload" button of the browser.

I provided my code here, which will not work but will give you an idea what I want to do.

conn<-odbcDriverConnect(connString)
 SystemInfo<-sqlQuery(conn, 'SELECT * FROM [DQ].[DQSystemInfo]',       stringsAsFactors = FALSE)
 close(conn)

 Domain<-unique(SystemInfo$Domain)
 Domain<-c(Domain,'NEW')
 SubDomain<-unique(SystemInfo$SubDomain[SystemInfo$Domain==Domain[1]])
 SubDomain<-c(SubDomain,'NEW')
 CurrentDomain<-Domain[1]
 CurrentSubDomain<-SubDomain[1]
 SystemInfo1<-SystemInfo[SystemInfo$Domain==CurrentDomain &      SystemInfo$SubDomain==CurrentSubDomain,]

  jsResetCode <- "shinyjs.reset = function() {history.go(0)}"

 shinyApp(


ui = fluidPage(
shinyjs::useShinyjs(),
shinyjs::extendShinyjs(text = "shinyjs.refresh = function() { location.reload(); }"),
#  div(
#      id = "form",
fluidRow(
  column(6, selectInput("domain", label = h4("Domain:"), 
                        choices = Domain, selected = CurrentDomain)),
  column(6,uiOutput("Condition2"))
),

#  fluidRow(column(2, verbatimTextOutput("value"))),

fluidRow(
  column(6, uiOutput("Condition1")),
  column(6,uiOutput("Condition3"))
),

    extendShinyjs(text = jsResetCode),

fluidRow(
  column(2, actionButton("submit", "Save", class="btn btn-primary btn-lg")),
  column(2, actionButton("cancel", "Cancel", class="btn btn-primary btn-

lg")),
      column(2, actionButton("delete", "Delete", class="btn btn-primary btn-lg"))
    )
    #)
  ),




  server = function(input, output) {

    observeEvent(input$domain, {
      if (input$domain=='NEW') {
        shinyjs::disable("domain")
    shinyjs::disable("delete") 
    CurrentSubDomain<-'NEW'

    output$Condition1 = renderUI({
      textInput("domainT",label = "", value = "")
    })

    output$Condition3 = renderUI({
      textInput("subdomainT", label = "",value = "")
    })

})   

  } else {
    CurrentDomain<-input$domain
    SubDomain<-unique(SystemInfo$SubDomain[SystemInfo$Domain==input$domain])
    SubDomain<-c(SubDomain,'NEW')}

  output$Condition2 = renderUI({
    selectInput("subdomain", label = h4("SubDomain:"),
                choices = SubDomain, selected =CurrentSubDomain)
  })

})


observeEvent(input$subdomain, {

  if (input$subdomain=='NEW') {
    shinyjs::disable("domain")  
    shinyjs::disable("subdomain")
    shinyjs::disable("delete") 

    output$Condition3 = renderUI({
      textInput("subdomainT", label = "", value = "")
    })


  } else {
    CurrentSubDomain<-input$subdomain
    conn<-odbcDriverConnect(connString)
    SystemInfo<-sqlQuery(conn, 'SELECT * FROM [DQ].[DQSystemInfo]', stringsAsFactors = FALSE)
    close(conn)
    SystemInfo1<-SystemInfo[SystemInfo$Domain==input$domain & SystemInfo$SubDomain==input$subdomain,]


  }
})


observeEvent(input$submit, {



    conn<-odbcDriverConnect(connString)
    DQ.DQSystemInfo<-SystemInfo[FALSE,c("Domain","SubDomain")]
    DQ.DQSystemInfo[1,]<-c("","","","","","","",0,48)
    DQ.DQSystemInfo$Domain<-ifelse(input$domain=='NEW',input$domainT,input$domain)
    DQ.DQSystemInfo$SubDomain<-input$subdomainT
    varType1 <- c("varchar(20)", "varchar(20)" )
    names(varType1)<-colnames(DQ.DQSystemInfo)
    sqlSave(conn, DQ.DQSystemInfo, append = TRUE, rownames = FALSE, varTypes = varType1)
    close(conn)

  # js$reset()
  #shinyjs::reset("form")
  # js$reset("form")

  conn<-odbcDriverConnect(connString)
  SystemInfo<-sqlQuery(conn, 'SELECT * FROM [DQ].[DQSystemInfo]', stringsAsFactors = FALSE)
  close(conn)

  Domain<-unique(SystemInfo$Domain)
  Domain<-c(Domain,'NEW')
  SubDomain<-unique(SystemInfo$SubDomain[SystemInfo$Domain==Domain[1]])
  SubDomain<-c(SubDomain,'NEW')
  CurrentDomain<-Domain[1]
  CurrentSubDomain<-SubDomain[1]
  SystemInfo1<-SystemInfo[SystemInfo$Domain==CurrentDomain & SystemInfo$SubDomain==CurrentSubDomain,]
  shinyjs::js$refresh()

})

observeEvent(input$cancel, {
  #js$reset()
  #shinyjs::reset("form")
  #js$reset("form")
  shinyjs::js$refresh()
})

observeEvent(input$delete, {
  conn<-odbcDriverConnect(connString)
  delete.query <- paste0("DELETE DQ.DQSystemInfo WHERE Domain='",
                         input$domain,"' and SubDomain='",input$subdomain,"'")
  sqlQuery(conn, delete.query)
  close(conn)

  #js$reset()
  # shinyjs::reset("form")
  # js$reset("form")

  conn<-odbcDriverConnect(connString)
  SystemInfo<-sqlQuery(conn, 'SELECT * FROM [DQ].[DQSystemInfo]', stringsAsFactors = FALSE)
  close(conn)

  Domain<-unique(SystemInfo$Domain)
  Domain<-c(Domain,'NEW')
  SubDomain<-unique(SystemInfo$SubDomain[SystemInfo$Domain==Domain[1]])
  SubDomain<-c(SubDomain,'NEW')
  CurrentDomain<-Domain[1]
  CurrentSubDomain<-SubDomain[1]
  SystemInfo1<-SystemInfo[SystemInfo$Domain==CurrentDomain & SystemInfo$SubDomain==CurrentSubDomain,]
  shinyjs::js$refresh()      
    })

  },options = list(height = 520))
ASR
  • 1,801
  • 5
  • 25
  • 33
user3049498
  • 111
  • 1