My app takes user input and stores it in google sheets.I'd like to have an action button that resets all sidebar-panel ui inputs when button is clicked. I gave it a try with shinyjs just like this post but I get the grey screen every time I press the button. I also tried resetting each widget individually using 3 different Shinyjs observeEvent reset's. My question is if the "Run JS code" for capturing location is interfering with resetting the side-panel.
library(shiny) library(googlesheets) library(dplyr) library(shinyjs)
shinyUI(fluidPage(
titlePanel(""),
sidebarLayout(
sidebarPanel(
shinyjs::useShinyjs(),
id = "side-panel",
selectInput("names",
"Name:",c(" ","Steve","Bob","Sally"),selected=" "),
radioButtons("radio", "",
c("Checking In" = "In","Heading Out" = "Out"),
selected="NULL"),
textInput("destination","Notes"),
actionButton("capture", "Share Location"),
tags$hr(),
actionButton("submit","Submit")
),
mainPanel(
textOutput("distPlot"),
verbatimTextOutput("lat"),
verbatimTextOutput("long"),
tableOutput("Table")
#verbatimTextOutput("geolocation")
)
)
)
)
Server
shinyServer(function(input, output,session) {
output$distPlot <- renderText({
x= if(input$names=="Steve") {
print(gs_edit_cells(safety, ws = "Sheet1",input=input$names, anchor = "A2"))
print(gs_edit_cells(safety, ws = "Sheet1",input=input$destination, anchor = "B2"))
print(gs_edit_cells(safety, ws = "Sheet1",input=input$radio, anchor = "C2"))
print(gs_edit_cells(safety, ws = "Sheet1",input=format(Sys.time(), "%a %D %X"), anchor = "D2"))
print(gs_edit_cells(safety, ws = "Sheet1",input=input$lat, anchor = "E2"))
print(gs_edit_cells(safety, ws = "Sheet1",input=input$long, anchor = "F2"))
} else if (input$names=="Bob"){
print(gs_edit_cells(safety, ws = "Sheet1",input=input$names, anchor = "A3"))
print(gs_edit_cells(safety, ws = "Sheet1",input=input$destination, anchor = "B3"))
print(gs_edit_cells(safety, ws = "Sheet1",input=input$radio, anchor = "C3"))
print(gs_edit_cells(safety, ws = "Sheet1",input=format(Sys.time(), "%a %D %X"), anchor = "D3"))
print(gs_edit_cells(safety, ws = "Sheet1",input=input$lat, anchor = "E3"))
print(gs_edit_cells(safety, ws = "Sheet1",input=input$long, anchor = "F3"))
} else if (input$names=="Sally"){
print(gs_edit_cells(safety, ws = "Sheet1",input=input$names, anchor = "A4"))
print(gs_edit_cells(safety, ws = "Sheet1",input=input$destination, anchor = "B4"))
print(gs_edit_cells(safety, ws = "Sheet1",input=input$radio, anchor = "C4"))
print(gs_edit_cells(safety, ws = "Sheet1",input=format(Sys.time(), "%a %D %X"), anchor = "D4"))
print(gs_edit_cells(safety, ws = "Sheet1",input=input$lat, anchor = "E4"))
print(gs_edit_cells(safety, ws = "Sheet1",input=input$long, anchor = "F4"))
} else {
print()
}
})
observeEvent(input$submit, {
shinyjs::reset("side-panel")
})
observeEvent(input$capture, {
# Run JS code to get location
runjs('
$(document).ready(function () {
navigator.geolocation.getCurrentPosition(onSuccess, onError);
function onError (err) {
Shiny.onInputChange("geolocation",false);
}
function onSuccess (position) {
setTimeout(function () {
var coords = position.coords;
console.log(coords.latitude + ", " + coords.longitude);
Shiny.onInputChange("geolocation", true);
Shiny.onInputChange("lat", coords.latitude);
Shiny.onInputChange("long", coords.longitude);
}, 1100)
}
});
')
})
output$lat <- renderPrint({
input$lat
})
output$long <- renderPrint({
input$long
})
})