r - 在 Shiny 中重置 FileInputi
问题描述
我有一个闪亮的应用程序fileInput
。
我创建了一个重置按钮来从fileInput
. 我知道该文件缓存在闪亮的内存中,但我只想重置 FileInput 对象,使其看起来像在我上传文件之前它最初的样子,当我按下重置按钮时。
shinyjs
我搜索了一下,发现大多数人都使用shinyjs
.
我在下面插入一个可执行代码!
library(shiny)
library(shinythemes)
ui <- fluidPage(
titlePanel("Old Faithful Geyser Data"),
fileInput(inputId = "ABC", label = "Input File", multiple = FALSE, accept = NULL,
width = "20%", buttonLabel = "Browse...",
placeholder = "You didn't choose any files to test, so select beside"),
sidebarLayout(
sidebarPanel(h4("Select the best the number of bins"),
br(),
br(),
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30),
actionButton("reset", "Reset"),
),
mainPanel(
plotOutput("distPlot")
)
)
)
server <- function(input, output,session) {
output$distPlot <- renderPlot({
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
observeEvent(input$reset, {
updateSliderInput(session,"bins",value = 1)
})
}
shinyApp(ui = ui, server = server)
解决方案
一种方法是fileInput
从服务器端动态使用,因此当单击重置按钮时,您可以重新加载输入。
library(shiny)
library(shinythemes)
ui <- fluidPage(
titlePanel("Old Faithful Geyser Data"),
uiOutput('file'),
sidebarLayout(
sidebarPanel(h4("Select the best the number of bins"),
br(),
br(),
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30),
actionButton("reset", "Reset"),
),
mainPanel(
plotOutput("distPlot")
)
)
)
server <- function(input, output,session) {
output$distPlot <- renderPlot({
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
output$file <- renderUI({
fileInput(inputId = "ABC", label = "Input File", multiple = FALSE, accept = NULL,
width = "20%", buttonLabel = "Browse...",
placeholder = "You didn't choose any files to test, so select beside")
})
observeEvent(input$reset, {
updateSliderInput(session,"bins",value = 1)
output$file <- renderUI({
fileInput(inputId = "ABC", label = "Input File", multiple = FALSE, accept = NULL,
width = "20%", buttonLabel = "Browse...",
placeholder = "You didn't choose any files to test, so select beside")
})
})
}
shinyApp(ui = ui, server = server)
推荐阅读
- java - 将json数据ajax传递给Spring boot
- jquery - 为什么 Asp.net Core 无法读取 Ajax 参数?
- json - jq unique_by 选择排序后的第一个元素
- javascript - 为什么我不能从另一个文件中的一个文件访问全局范围变量?
- python - 合并 Pandas Dataframes 时如何只使用第一个匹配项?
- android - 从多模块项目构建 aar 库
- angular - Angular 9:在 GUI 上列出用户详细信息
- python-3.x - 无法在 Colab 中使用 PyTorch 重现结果
- php - PHP如何通过控制器转发表单然后返回另一个页面
- python - 可变和不可变 Deep/Shallow 复制 Python