【问题标题】:R shiny input in one input to form input of the next input一个输入中的 R 闪亮输入以形成下一个输入的输入
【发布时间】:2019-03-13 21:25:36
【问题描述】:

我正在使用以下示例在 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)
  }

我请求这方面的一些指导。

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    如果您只想要一个额外的列,您可以这样做:

    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)
    

    【讨论】:

      猜你喜欢
      • 2018-07-21
      • 1970-01-01
      • 1970-01-01
      • 2018-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-05
      相关资源
      最近更新 更多