In the app.R file below, I'm trying to create a selectInput()
box for each column name of the iris dataset. However, I am not sure what must go in place of the ellipsis selectInput(..., 'Sepal.Length', c(0, 1)),
in order to make it work, and I also don't like that I had to type out the name of the column itself. Of course, I could have done something like selectInput(..., 'names(iris)[1]', c(0, 1)),
instead, but I'm looking to create something even more automated and streamlined (i.e., writing out selectInput(..., 'column name', c(0, 1)),
five times for each of the five columns seems grossly inefficient). In other words, perhaps I could make Shiny automatically recognize that the iris dataset has 5 columns by default, which would create the five selectInput()
boxes appropriately?
The whole reason why I'm trying to accomplish this is because I would like to create a simple automated character vector (shown in the code below as vec <- c(1,1,0,0,1) #corresponding to c(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width, Species)
), which automatically updates depending on whether I choose 0
or 1
in the respective selectInput()
boxes discussed above.
Complete code (with critical sections marked with a hashtag):
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)
vec <- c(1,1,0,0,1)
(i.e., so I can make itc(0,0,1,0,1)
orc(1,0,0,0,0)
or whatever, based on what I select in the drop down menu) – warshipselectInput
withmultiple = TRUE
and then setvec
based on the choices selected? – tblznbits