我的应用程序接受用户输入并将其存储在谷歌表格中。我想要一个操作按钮,在单击按钮时重置所有侧边栏面板 ui 输入。就像这篇文章一样,我尝试使用shinyjs ,但每次按下按钮时都会出现灰屏。我还尝试使用 3 个不同的 Shinyjs observeEvent 重置单独重置每个小部件。我的问题是用于捕获位置的“运行 JS 代码”是否会干扰重置侧面板。
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")
)
)
)
)
服务器
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
})
})