如何在rshiny中筛选列和拟合模型

如何在rshiny中筛选列和拟合模型,r,shiny,shinydashboard,shiny-server,shinyapps,R,Shiny,Shinydashboard,Shiny Server,Shinyapps,我想将几种聚类算法仅适用于选定列的数据集。请在下面找到我的试用版: library(rshiny) data(mtcars) if (interactive()) { shinyApp( ui = fluidPage( sliderInput('num',label='Insert Number of clusters',value = 3,min = 2,max = 10,step = 1), varSelectInput(&qu

我想将几种聚类算法仅适用于选定列的数据集。请在下面找到我的试用版:

    library(rshiny)
    data(mtcars)
      if (interactive()) {
  shinyApp(
    ui = fluidPage(
      sliderInput('num',label='Insert Number of clusters',value = 3,min = 2,max = 10,step = 1),
      varSelectInput("variables", "Variables:", mtcars, multiple = TRUE),
      selectInput('Model','Model:',choices = c('K-Means','Others')),
      tableOutput("data")
    ),
    server = function(input, output) {
      output$data <- renderTable({
        if (length(input$variables) == 0) return(mtcars)
        mtcars %>% dplyr::select(!!!input$variables)
        if (input$Model == 'K-means'){
          autoplot(kmeans(df_clean,input$num),data=df_clean,label=TRUE,label.size=3)
        }
      }, rownames = TRUE)
    }
  )}
库(rshiny)
数据(mtcars)
if(interactive()){
shinyApp(
ui=fluidPage(
sliderInput('num',label='Insert Number of clusters',value=3,min=2,max=10,step=1),
varSelectInput(“变量”,“变量:”,mtcars,multiple=TRUE),
选择输入('Model','Model:',choices=c('K-Means','Others'),
表格输出(“数据”)
),
服务器=功能(输入、输出){
输出$data%dplyr::选择(!!!输入$variables)
如果(输入$Model=='K-means'){
自动绘图(kmeans(df_clean,输入$num),data=df_clean,label=TRUE,label.size=3)
}
},rownames=TRUE)
}
)}
这使我能够选择适合的列和模型类型,但我无法在仪表板上看到集群算法


任何关于这方面的帮助都将被告知。

您需要将不同类型的输出分开。不能在同一输出中有表格输出和绘图。 我举了一个例子,说明它如何与情节的条件面板一起工作

library(rshiny)
data(mtcars)
if (interactive()) {
  shinyApp(
    ui = fluidPage(
      sliderInput('num',label='Insert Number of clusters',value = 3,min = 2,max = 10,step = 1),
      varSelectInput("variables", "Variables:", mtcars, multiple = TRUE),
      selectInput('Model','Model:',choices = c('K-Means','Others')),
      column(
        width = 4,
        tableOutput("data")
      ),
      column(
        width = 6,
        conditionalPanel(
          condition = "input.Model == 'K-Means'",
          plotOutput("cluster")
        )
      )
    ),
    server = function(input, output) {
      
      df_clean <- reactive({
        if (length(input$variables) == 0) return(mtcars)
        mtcars %>% 
          dplyr::select(!!!input$variables)
      })
      
      output$data <- renderTable({
        df_clean()
      }, rownames = TRUE)
      output$cluster <- renderPlot({
        req(input$Model == 'K-Means')
        
        axises <- unlist(c(input$variables,"mpg", "cyl"))[1:2]
        cluster <- kmeans(df_clean(),input$num)
        ggplot(df_clean(), aes_string(x = axises[[1]],y = axises[[2]] )) +
          geom_point(colour = cluster$cluster)
      })
    }
  )}
库(rshiny)
数据(mtcars)
if(interactive()){
shinyApp(
ui=fluidPage(
sliderInput('num',label='Insert Number of clusters',value=3,min=2,max=10,step=1),
varSelectInput(“变量”,“变量:”,mtcars,multiple=TRUE),
选择输入('Model','Model:',choices=c('K-Means','Others'),
纵队(
宽度=4,
表格输出(“数据”)
),
纵队(
宽度=6,
条件板(
condition=“input.Model=”K-Means',
打印输出(“群集”)
)
)
),
服务器=功能(输入、输出){
df_清洁度%
dplyr::select(!!!输入$variables)
})

输出$data您好,您在autoplot中提到的数据之前没有定义,我认为这可能是一个问题