首页 > 解决方案 > 在 Shiny 中单击按钮后如何打开新表单

问题描述

我是 Shiny 的新手,想知道在选择选项后如何打开/显示新表单。

在下面的示例中,如果我选择上传,那么我想打开/显示一个可以让我上传数据的表单,如果我选择分析数据,那么它将让我打开/显示一个让我分析数据的表单。

任何建议/帮助将不胜感激。

library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(

# Application title
titlePanel("Old Faithful Geyser Data"),
selectInput(inputId = "Task", label = "Select Task",choices =  c("Please select","Upload","Analyze Data")),
textOutput("SR_Text")
)

# Define server logic required to draw a histogram
server <- function(input, output) {
observeEvent(input$Task, {
if(input$Task == "Upload"){
  output$SR_Text<-renderText({
    "Upload"
  }) 
} else if (input$Task == "Analyze Data"){
  output$SR_Text<-renderText({
    "Analyze Data"
  }) 
}
})
}

# Run the application 
shinyApp(ui = ui, server = server)

标签: rshiny

解决方案


我想这conditionalPanel就是你所需要的:

library(shiny)

ui <- fluidPage(
    # Application title
    titlePanel("Old Faithful Geyser Data"),
    column(3,
           selectInput("Task", label = "Select Task",choices =  c("Please select","Upload","Analyze Data"))
    ),
    column(9,
           conditionalPanel(
               condition = "input.Task == 'Upload'",
               fileInput("Upload", h3("File input Upload")),
               selectInput(
                   "breaks", "Breaks",
                   c("Sturges",
                     "Scott",
                     "Freedman-Diaconis",
                     "[Custom]" = "custom")),

           ),
           conditionalPanel(
               condition = "input.Task == 'Analyze Data'",
               sliderInput("breakCount", "Break Count", min=1, max=1000, value=10)
           )
    )
)

# Define server logic required to draw a histogram
server <- function(input, output,session) {

}

# Run the application 
shinyApp(ui = ui, server = server)

推荐阅读