有没有办法在单击按钮并触发事件完成后更新
fileInput
按钮的按钮标签?我将 fileInput 调用自定义为 隐藏进度条和 文本字段。完成后我也会禁用该按钮。我希望按钮的“获取文件”标签更改为“完成”之类的内容。我找到了使用 runjs()
来更改整个按钮类的方法,但我不确定如何具体调用按钮标签?如果我还可以向更新后的标签添加图标,那就加分了。
所有这一切都可以通过
actionButton
和 file.choose()
的组合轻松实现,但是 在shinyapps.io 上不起作用,所以我需要一个解决方法。
library(shiny)
library(shinyjs)
#custom function to hide the text field next to the button
fileInput2 <- function(..., label="") {
temp <- fileInput(..., label=label)
# Cut away the label
temp$children[[1]] <- NULL
# Cut away the input field (after label is cut, this is position 1 now)
temp$children[[1]]$children[[2]] <- NULL
# Remove input group classes (makes button flat on one side)
temp$children[[1]]$attribs$class <- NULL
temp$children[[1]]$children[[1]]$attribs$class <- NULL
temp
}
ui <- fluidPage(
shinyjs::useShinyjs(),
tags$head(tags$style(".shiny-file-input-progress {display: none}")),
fileInput2("file", label=NULL, buttonLabel="Get File", placeholder=NULL, width='175px'),
)
server <- function(input, output, session) {
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("$('#file') ... now also update the button label...")
})
}
shinyApp(ui,server)
您可以使用 Javascript 来完成此操作:
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, width='175px'),
)
server <- function(input, output, session) {
observeEvent(input$file,{
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';")
})
}
shinyApp(ui,server)