R Shiny自动化selectInput()功能

时间:2016-01-04 03:06:59

标签: r shiny

在下面的app.R文件中,我尝试为iris数据集的每个列名创建一个selectInput()框。但是,我不确定必须使用省略号selectInput(..., 'Sepal.Length', c(0, 1)),才能使其正常工作,而且我也不喜欢我必须输入列本身的名称。当然,我本可以做一些像selectInput(..., 'names(iris)[1]', c(0, 1)),这样的事情,但我希望创建一些更加自动化和简化的东西(即,为五列中的每一列写出selectInput(..., 'column name', c(0, 1)),五次似乎非常低效)。换句话说,也许我可以让Shiny自动识别iris数据集默认有5列,这会适当地创建5个selectInput()框?

我试图完成此操作的全部原因是因为我想创建一个简单的自动字符向量(在下面的代码中显示为vec <- c(1,1,0,0,1) #corresponding to c(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width, Species)),它会根据我是否选择自动更新在上面讨论的相应0框中1selectInput()

完整代码(关键部分标有主题标签):

  palette(c("#E41A1C", "#377EB8", "#4DAF4A", "#984EA3",
  "#FF7F00", "#FFFF33", "#A65628", "#F781BF", "#999999"))

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

  # Combine the selected variables into a new data frame
  selectedData <- reactive({
    iris[, c(input$xcol, input$ycol)]
  })

  clusters <- reactive({
    kmeans(selectedData(), input$clusters)
  })

  output$plot1 <- renderPlot({
    par(mar = c(5.1, 4.1, 0, 1))
    plot(selectedData(),
         col = clusters()$cluster,
         pch = 20, cex = 3)
    points(clusters()$centers, pch = 4, cex = 4, lwd = 4)
  })

  #vec <- c(1,1,0,0,1)  #corresponding to c(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width, Species)

})


ui <- shinyUI(pageWithSidebar(
  headerPanel('Iris k-means clustering'),
  sidebarPanel(
    selectInput('xcol', 'X Variable', names(iris)),
    selectInput('ycol', 'Y Variable', names(iris),
                selected=names(iris)[[2]]),
    #selectInput(..., 'Sepal.Length', c(0, 1)),
    #selectInput(..., 'Sepal.Width', c(0, 1)),
    #selectInput(..., 'Petal.Length', c(0, 1)),
    #selectInput(..., 'Petal.Width', c(0, 1)),
    #selectInput(..., 'Species', c(0, 1)),
    numericInput('clusters', 'Cluster count', 3,
                 min = 1, max = 9)
  ),
  mainPanel(
    plotOutput('plot1')
  )
))


# Return a Shiny app object
shinyApp(ui = ui, server = server)

2 个答案:

答案 0 :(得分:1)

这是使用checkboxGroupInput:

ui <- shinyUI(pageWithSidebar(
  headerPanel('Iris k-means clustering'),
  sidebarPanel(
    selectInput('xcol', 'X Variable', names(iris)),
    selectInput('ycol', 'Y Variable', names(iris),
                selected=names(iris)[[2]]),
    checkboxGroupInput('columnNames', 'Column Name', names(iris)),
    numericInput('clusters', 'Cluster count', 3,
                 min = 1, max = 9)
  ),
  mainPanel(
    plotOutput('plot1')
  )
))

要检索所选列(1)和未选择(0)的列名,可以在服务器端使用以下选项:

> s <- c('Sepal.Length', 'Sepal.Width', 'Petal.Width') # Example vector from checkbox group input
> names(iris) %in% s
[1]  TRUE  TRUE FALSE  TRUE FALSE
> as.integer(names(iris) %in% s)
[1] 1 1 0 1 0
> which(names(iris) %in% s) # Provides vector indices set to 1
[1] 1 2 4
> which(!names(iris) %in% s) # Provides vector indices set to 0
[1] 3 5
> 

答案 1 :(得分:1)

您可以使用multiple = TRUE上的selectInput值来允许选择多个列。然后在服务器端,您只需设置vec <- as.numeric(names(iris) %in% input$iris_columns) input$iris_columns引用您的selectInput元素。

举个例子:

ui <- shinyUI(pageWithSidebar(
  headerPanel('Iris k-means clustering'),
  sidebarPanel(
    selectInput('xcol', 'X Variable', names(iris)),
    selectInput('ycol', 'Y Variable', names(iris), selected=names(iris)[[2]]),
    selectInput('iris_columns', 'Select Columns', choices = names(iris), multiple = TRUE),
    numericInput('clusters', 'Cluster count', 3, min = 1, max = 9)
  ),
  mainPanel(
    plotOutput('plot1')
  )
))

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

  # Combine the selected variables into a new data frame
  selectedData <- reactive({
    iris[, c(input$xcol, input$ycol)]
  })

  clusters <- reactive({
    kmeans(selectedData(), input$clusters)
  })

  output$plot1 <- renderPlot({
    par(mar = c(5.1, 4.1, 0, 1))
    plot(selectedData(),
         col = clusters()$cluster,
         pch = 20, cex = 3)
    points(clusters()$centers, pch = 4, cex = 4, lwd = 4)
  })

  vec <- as.numeric(names(iris) %in% input$iris_columns)
})
相关问题