在我的
shiny
应用程序中,加载数据后,我禁用了 fileInput
。我还有一个条件重置按钮,可以让用户从头开始。然而,虽然 shinyjs::disable()
对我的 fileInput
效果很好,但 shinyjs::enable()
被忽略,并且输入在重置后不会重新激活。我错过了什么吗?有没有其他方法可以重新启用输入?尽管删除了该类,它也没有将外观从“禁用”重置为“启用”。
是因为我在同一个输入上运行多个连续的js吗?我可以将它们合并为一个
runjs()
吗?我对 JS 不太熟悉。
library(shiny)
library(shinyjs)
ui <- fluidPage(
shinyjs::useShinyjs(),
tags$head(tags$style(".shiny-file-input-progress {display: none}")),
fileInput("file", label=NULL, buttonLabel="Get File", placeholder=NULL),
uiOutput('reset')
)
server <- function(input, output, session) {
dataloaded<-reactiveVal()
observeEvent(input$file,{
#...loading the data and doing something with it...
shinyjs::disable("file") #disable the button
runjs('$("#file").parents("span").addClass("disabled")') #make it look disabled
runjs("var button = document.querySelector('.btn-file');button.textContent = 'Done';") #change the label
dataloaded(T)
})
output$reset <- renderUI({
req(dataloaded())
actionButton("reset", "Reset")
})
observeEvent(input$reset,{
dataloaded(NULL)
shinyjs::enable("file")
runjs("var button = document.querySelector('.btn-file');button.textContent = 'Get File';")
runjs('$("#file").parents("span").removeClass("disabled")')
})
}
shinyApp(ui,server)
问题是在
observeEvent
中 input$file
你正在这样做:
var button = document.querySelector('.btn-file');
button.textContent = 'Done'; #change the label
但是,请注意 MDN 上写的内容:
警告:在节点上设置 textContent 会删除该节点的所有内容 子节点并用给定的单个文本节点替换它们 字符串值。
所以你特别删除了
file
,然后shinyjs::enable("file")
找不到它。
而不是像上面那样,这样做:
var button = document.querySelector('.btn-file');
button.childNodes[0].nodeValue = 'Done';