首页 > 解决方案 > 在 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)

标签: rshiny

解决方案


一种方法是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)

在此处输入图像描述


推荐阅读