R 以排除选定框的方式输入复选框组

R 以排除选定框的方式输入复选框组,r,shiny,shinydashboard,R,Shiny,Shinydashboard,您好,我正在做一个Rshiny仪表板,其中包含一组三个变量,我希望将它们表示为要选择或不选择的选项。如果用户不勾选此框,则应将其从数据文件中排除,并输入到集群模型中 我很难将其连接到输入数据,因为未选择的变量被排除在模型中的数据之外: 下面是示例代码-非常感谢您的帮助 titlePanel("Customer"), sidebarLayout( sidebarPanel( sliderInput("Clus

您好,我正在做一个Rshiny仪表板,其中包含一组三个变量,我希望将它们表示为要选择或不选择的选项。如果用户不勾选此框,则应将其从数据文件中排除,并输入到集群模型中

我很难将其连接到输入数据,因为未选择的变量被排除在模型中的数据之外:

下面是示例代码-非常感谢您的帮助

  titlePanel("Customer"),

    sidebarLayout(
                  sidebarPanel(
                    sliderInput("Clusters",
                                "Number of Clusters:",
                                min = 1,
                                max = 10,
                                value = 1
                  ),

                                    checkboxGroupInput("Checkboxgroup", 
                                       h3("variable selection"), 
                                       choices = list("v1" = 1, 
                                                      "v2" = 2, 
                                                      "v3" = 3),
                                       selected = c(1, 2, 3))

    ),                  mainPanel(position="right",
                            plotOutput("distPlot", height = 500, width = 500)
                  )
    )

)

server <- function(input, output) {

data_train= data[ ,3:14]
### here is where i need to excluded the selected boxes from the data_train on the above line

 k_means=reactive({
    kmeans(data_train, centers=input$Clusters, nstart = 50)
  })


  output$distPlot <- renderPlot({

    plotcluster(data_train, k_means()$cluster)   

  })
}

# Run the application 
shinyApp(ui = ui, server = server)
titlePanel(“客户”),
侧边栏布局(
侧栏面板(
sliderInput(“集群”,
“集群数量:”,
最小值=1,
最大值=10,
值=1
),
checkboxGroupInput(“Checkboxgroup”,
h3(“变量选择”),
选项=列表(“v1”=1,
“v2”=2,
“v3”=3),
选定=c(1,2,3))
),主面板(position=“right”,
绘图输出(“distPlot”,高度=500,宽度=500)
)
)
)

服务器在我看来,您从未在服务器中使用过checkboxgroup。另外,您可以排除的变量不是checkboxgroup中的选项,而是v3。我会建议一些假设你只希望变量3,4和5被排除在外的方法。另外,我还假设您只需要变量3到14,因为这就是您要做的

首先,将复选框更改如下:

checkboxGroupInput("Checkboxgroup", 
                                       h3("variable selection"), 
                                       choices = list("v3" = 3, 
                                                      "v4" = 4, 
                                                      "v5" = 5),
                                       selected = c(3:5))
mytrain=reactive({
vars=3:14;vars=vars[!vars%in%as.numeric(input$Checkboxgroup)]
data_train= data[ ,vars]
data_train
})
然后,您可以更改定义培训集的行,如下所示:

checkboxGroupInput("Checkboxgroup", 
                                       h3("variable selection"), 
                                       choices = list("v3" = 3, 
                                                      "v4" = 4, 
                                                      "v5" = 5),
                                       selected = c(3:5))
mytrain=reactive({
vars=3:14;vars=vars[!vars%in%as.numeric(input$Checkboxgroup)]
data_train= data[ ,vars]
data_train
})
你的分析是:

k_means=reactive({
    kmeans(mytrain(), centers=input$Clusters, nstart = 50)
  })

如果我正确理解了您的问题,应该可以解决。

非常感谢,我认为它为我解决了很多问题-我将尝试此解决方案-我们声明为“v3”=3等的变量,3表示服务器上数据文件的列号?在我的示例中,变量“V1”是数据的第3列。