我有一个用于自定义输入的闪亮模块,并且每次单击此按钮时,我都试图添加一个按钮以重置此自定义输入(界面和服务器)。以下是使用Shinyjs :: reset:
的示例app.R
library(shinyjs)
library(rintrojs)
library(shinydashboard)
library(shinyWidgets)
source("customTextInput.R", local = TRUE, encoding = "UTF-8")
ui <- fluidPage(
useShinyjs(),
# Assembling page
dashboardPage(
# Assembling header
dashboardHeader(title = "Custom Inputs", titleWidth = 1294),
# Assembling sidebar
dashboardSidebar(
sidebarMenu(
menuItem("Custom inputs reset", tabName = "custom", icon = icon("search"))
)
),
# Assembling the body of the page
dashboardBody(
tabItems(
tabItem(tabName = "custom",
br(),
br(),
customTextInputUI("text1"),
fluidPage(
textInput(inputId = "text2", label = "Native text input", width = "100%"),
actionButton(inputId = "reseter1", label = "Reset custom"),
actionButton(inputId = "reseter2", label = "Reset native")
),
)
)
)
)
)
server <- function(input, output, session) {
{### Reset -----
observe({
shinyjs::onclick("reseter1", {
reset("text1")
})
})
observe({
shinyjs::onclick("reseter2", {
reset("text2")
})
})
}
}
shinyApp(ui, server)
customTextInput.R
library(shinyjs)
{# UI Module -----
customTextInputUI <- function(id, addWidth = "100%", addHeight="64px", addTop="5px", addValue = "") {
if (is.null(addValue)){addValue <- ""}
ns <- NS(id)
return(
fluidPage(
HTML(
sprintf(
"
<form id='%s' autocomplete='off' class = 'form-group shiny-input-container' style = 'padding-top:%s; width:%s; height:%s;'>
<label for='%s'>Custom text input</label>
<input charset='UTF-8' type='text' class = 'form-control'
id='%s' data-slots=' ' value = \"%s\"
data-shinyjs-resettable-id='%s' data-shinyjs-resettable-type='Text' data-shinyjs-resettable-value='' required>
</form>", ns('textBloco'), addTop, addWidth, addHeight, ns('textInput'), ns('textInput'), addValue, ns('textInput')))
)
)
}
}
在这种情况下,reseter2可以重置闪亮的text2本机textInput,但是reseter1无法重置自定义输入text1。
为什么会有这种意外行为?是否有任何解决方法,也许使用纯JavaScript?
提前谢谢!