我使用以下示例在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)
}
我要求在这方面提供一些指导。
答案 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)