选择滑块以在r Shinyapps中显示

时间:2018-06-28 15:09:23

标签: r shiny

我试图在闪亮的aps中使用selectInput创建一个菜单来更改显示的滑块。

ui <- fluidPage (
    selectInput (inputId = "sat_act", "Choose your test:", 
            c("SAT" = "SATscore", "ACT" = "ACTscore")
    sliderInput (inputId = "SATscore",
        label = "Select your SAT score", step = 10,
        value = 1000, min = 400, max = 1600),
    sliderInput (inputId = "ACTscore",
        label = "Select your ACT score", 
        value = 18, min = 1, max = 36)
    plotOutput (outputId = "graph")
        )

server <- function(input,output){}
shinyApp(ui = ui, server = server)

当前,我的代码显示了男孩滑块,并且没有下拉菜单。我不确定如何执行此操作,还没有在线找到示例。任何帮助,将不胜感激。

2 个答案:

答案 0 :(得分:1)

您可以使用renderUI()

在服务器端,您评估sat_act输入,并根据呈现新ui元素的选择进行选择:

output$score <- renderUI({
    if(input$sat_act == "SATscore"){
      return(...)
    }
  })

完整应用:

library(shiny)
ui <- fluidPage (
               selectInput (inputId = "sat_act", "Choose your test:", 
                c("SAT" = "SATscore", "ACT" = "ACTscore")),
               uiOutput("score"),
               plotOutput (outputId = "graph")
)

server <- function(input,output){
  output$score <- renderUI({
    if(input$sat_act == "SATscore"){
      return(
        sliderInput (inputId = "SATscore",
                     label = "Select your SAT score", step = 10,
                     value = 1000, min = 400, max = 1600)
      )
    }else{
      return(
        sliderInput (inputId = "ACTscore",
                     label = "Select your ACT score", 
                     value = 18, min = 1, max = 36)    
      )
    }
  })
}
shinyApp(ui = ui, server = server)

答案 1 :(得分:1)

创建UI

library(shiny)
ui <- fluidPage (
  selectInput(inputId = "sat_act", "Choose your test:", 
              c("SAT" = "SATscore", "ACT" = "ACTscore")),
  uiOutput('slider'),
  plotOutput (outputId = "graph")
)

server <- function(input,output){
  output$slider = renderUI({
    if (input$sat_act == 'SATscore') {
      sliderInput(inputId = "SATscore",
              label = "Select your SAT score", step = 10,
              value = 1000, min = 400, max = 1600)
    } else {
      sliderInput(inputId = "ACTscore",
              label = "Select your ACT score", 
              value = 18, min = 1, max = 36)
    }

  })
}
shinyApp(ui = ui, server = server)