一个输入中的R个闪亮输入形成下一个输入的输入

时间:2018-10-09 07:13:58

标签: r shiny

我使用以下示例在R Shiny中设置数据框

if (interactive()) {

  ui <- fluidPage(
  selectInput("variable", "Variable:",
            c("Cylinders" = "cyl",
              "Transmission" = "am",
              "Gears" = "gear")),
   tableOutput("data")
  )

   server <- function(input, output) {
   output$data <- renderTable({
   mtcars[, c("mpg", input$variable), drop = FALSE]
   }, rownames = TRUE)
 }

shinyApp(ui, server)
}

这适用于一组预定的列

我已经用滑块编写了以下代码,以创建输入,这些输入将用作以上示例中使用的MTcars数据集的列号。有没有一种方法可以使用滑块上指示的数字作为列号的输入,并以闪亮的方式动态子集数据框

我已经写了下面的代码,但是似乎没有用

if (interactive()) {

 ui <- fluidPage(
 sliderInput(inputId = "slider1", label = "numberinput", min = 1, max = 10, 
 value = 1, step = 1),

 selectInput(inputId = "Columnno", label = "Column Number",choices = 
 as.numeric(input$slider1)),
tableOutput("data")
  )

 server <- function(input, output) {
 output$data <- renderTable({
  mtcars[, c("mpg", input$Columnno), drop = FALSE]
  }, rownames = TRUE)
  }

  shinyApp(ui, server)
  }

我要求在这方面提供一些指导。

1 个答案:

答案 0 :(得分:2)

如果只需要多一列,则可以执行以下操作:

library(shiny)

ui <- fluidPage(
  sliderInput(inputId = "slider1", label = "numberinput", min = 1, max = 10, value = 1, step = 1),
  selectInput(inputId = "Columnno", label = "Column Number",choices = NULL,selected = NULL),
  tableOutput("data")
)

server <- function(input, output,session) {

  observeEvent(input$slider1,{
    updateSelectInput(session,"Columnno",choices = as.numeric(1:input$slider1), selected = as.numeric(input$slider1))
  })
  output$data <- renderTable({
    req(input$Columnno)
    mtcars[, c("mpg", colnames(mtcars)[as.numeric(input$Columnno)]), drop = FALSE]
  }, rownames = TRUE)
}

shinyApp(ui, server)

enter image description here