先谢谢大家。我试图缩小范围,但我不知所措,所以我在缩小范围时遇到了麻烦。我的主要困惑是反应性与其他类型的反应性(?)结构。
我要做的是列出R基本数据包。我在选择框中有这个。这样可行。 选择后,R将显示该数据集的字段,以便用户可以接受Y和X值。 (我意识到有些套装没有格式化。) 下面列出的代码给出了"不正确的维数和#34;在主面板中。 错误似乎在调用名称的某处。我似乎并不了解如何在他们创作之后将反应元素纳入ui。 关于这个的任何提示?
同样,数据集列表也有效。数据表被正确拉出。但这些名字并不存在,因此可以选择它们来构建情节。
library(shiny)
set.seed(10101)
dataPkgs <- data(package='datasets')[[3]]
pkgNames <- dataPkgs[,3]
ui <- fluidPage(
headerPanel('R Data k-means clustering'),
sidebarPanel(
selectInput('choosedata', 'Choose a Data Set',pkgNames,selected="mtcars"),
helpText("Listing should make available all basic datasets available in base R install.
Remember many of the data sets will be ill-fit for this type of analysis."),
selectInput('ycol', 'Select Independent Y', textOutput('selDFNames')),
selectInput('xcol', 'Select Dependent X', textOutput('selDFNames')),
numericInput('clusters', '1-7 clusters', 3,
min = 1, max = 7)
),
mainPanel(
plotOutput('plot1')
)
)
server <- function(input, output, session) {
selDF <- renderTable({
get(input$choosedata)
})
output$selDFNames <- renderText({
names(selDF())
})
selDFset <- reactive({
selDF()[, c(input$xcol, input$ycol)]
})
clusters <- reactive({
kmeans(selDFset(), input$clusters)
})
output$plot1 <- renderPlot({
plot(selDFset(),
col = clusters()$cluster,
pch = 20, cex = 3)
points(clusters()$centers, pch = 4, cex = 4, lwd = 4)
})
}
shinyApp(ui = ui, server = server)
答案 0 :(得分:0)
使用observe
和updateSelectInput
更新您的选择输入。这是服务器功能,它运行良好。祝你好运。
server <- function(input, output, session) {
selDF <- reactive({
get(input$choosedata, envir = as.environment('package:datasets'))
})
observe({
updateSelectInput(session, "ycol", choices = names(selDF()))
})
observe({
updateSelectInput(session, "xcol", choices = names(selDF()))
})
selDFset <- reactive({
selDF()[, c(input$xcol, input$ycol)]
})
clusters <- reactive({
kmeans(selDFset(), input$clusters)
})
output$plot1 <- renderPlot({
plot(selDFset(),
col = clusters()$cluster,
pch = 20, cex = 3)
points(clusters()$centers, pch = 4, cex = 4, lwd = 4)
})
}
shinyApp(ui = ui, server = server)
答案 1 :(得分:0)
我会用我的答案添加更多的测试用例和捕获量,但如果你得到你知道的数据集而不仅仅是要求所有这些数据集,那么它是最好的:
library(shiny)
set.seed(10101)
dataPkgs <- data(package='datasets')[[3]]
pkgNames <- dataPkgs[,3]
longley[,c("GNP.deflator","GNP.deflator")]
ui <- fluidPage(
headerPanel('R Data k-means clustering'),
sidebarPanel(
selectInput('choosedata', 'Choose a Data Set',pkgNames,selected="mtcars"),
helpText("Listing should make available all basic datasets available in base R install.
Remember many of the data sets will be ill-fit for this type of analysis."),
selectInput('ycol', 'Select Independent Y', choices = NULL),
selectInput('xcol', 'Select Dependent X', choices = NULL),
numericInput('clusters', '1-7 clusters', 3,min = 1, max = 7)
),
mainPanel(
plotOutput('plot1')
)
)
server <- function(input, output, session) {
selDF <- reactive({
data <- tryCatch(get(input$choosedata), error=function(e) NULL)
if(is.null(data)){
return()
}
data
})
observeEvent(selDF(),{
updateSelectInput(session,"xcol",choices=c(names(selDF())))
updateSelectInput(session,"ycol",choices=c(names(selDF())))
})
selDFset <- eventReactive(c(input$xcol, input$ycol),{
req(input$xcol)
req(input$ycol)
selDF()[,c(input$xcol, input$ycol)]
})
clusters <- reactive({
kmeans(selDFset(), input$clusters)
})
output$plot1 <- renderPlot({
req(selDFset())
plot(selDFset(),col = clusters()$cluster,pch = 20, cex = 3)
points(clusters()$centers, pch = 4, cex = 4, lwd = 4)
})
}
shinyApp(ui = ui, server = server)