css 如何把一个按钮添加字段在闪亮

q7solyqu  于 2023-01-10  发布在  其他
关注(0)|答案(1)|浏览(102)

我正在创建一个Shiny应用程序,我需要默认有两个字段用于上传文件。但是,我需要有一个按钮来添加更多字段(每当用户单击此按钮时,就会添加另一个用于上传文件的字段)和一个重置按钮(返回到只有两个字段的状态)。我如何在Shiny中做到这一点?

body <- dashboardBody(
    tabItems(
      tabItem(tabName = "plot"),
      tabItem(tabName = "table",
              box(width = NULL, status = "primary", solidHeader = TRUE, 
                  title="Upload",
                  fileInput("classe1",
                            label="Class 1",
                            multiple = TRUE, accept = c(".jpg", ".jpeg",".png",".tif")),
                  br(),
                  fileInput("classe2",
                            label="Class 2",
                            multiple = TRUE, accept = c(".jpg", ".jpeg",".png",".tif")),
                  br()
              )
      )
)

9lowa7mx

9lowa7mx1#

这里有一种方法可以做到这一点,使用renderUIuiOutput的组合来动态显示/隐藏第三个fileInput

library(shinydashboard)
library(shiny)

ui <- dashboardPage(
  dashboardHeader(title = "Basic dashboard"),
  dashboardSidebar(),
  dashboardBody(
    fluidRow(column(6, fileInput("classe1",label="Class 1",
              multiple = TRUE, accept = c(".jpg", ".jpeg",".png",".tif"))),
      column(6, actionButton('add', 'Add a new input'))
    ),
      fluidRow(column(6, fileInput("classe2",label="Class 2",multiple = TRUE, 
              accept = c(".jpg", ".jpeg",".png",".tif"))),
            column(6, actionButton('clear', 'Clear')), 
          ),
          br(), 
          uiOutput('new_button')
    )
)

server <- function(input, output) {
  observeEvent(input$add, {
    output$new_button <- renderUI({
      fileInput("classe3",label="Class 3",multiple = TRUE, 
                accept = c(".jpg", ".jpeg",".png",".tif"))
    })
  })
  observeEvent(input$clear, {
    output$new_button <- renderUI({})
  })
}

shinyApp(ui, server)

相关问题