for循环R中的子集数据帧

问题描述 投票:0回答:1

目前,我正在开发一个Shiny应用。在sidebarPanelUI中,根据数据帧的数字列创建多个selectInput选项。对于数据框中的每个非数字列(基于用户已选择的excel文件),将创建selectInput选项:

df<-iris 

lapply(names(df %>% select_if(~is.numeric(.x)) %>% head()), function(i) {   selectInput(paste0(i), paste0(i),
              choices = df[i],
              multiple = TRUE,
              selected = "")})

在脚本的Server中,需要获取一个基于用户输入的数据帧。但是,我当前的方法不起作用:

Sepal.Length<-c(5.1,4.6)
Sepal.Width<-c(3.0)
Petal.Width<-c(0.2,0.3)
Petal.Length<-c(1.4,1.3)

input<-data.frame(Sepal.Length,Sepal.Width,Petal.Length,Petal.Width)


data<-df

lapply(names(df %>% select_if(~is.numeric(.x)) %>% head()), function(i) {

  data <- subset(data,
                 i %in% input[[i]])

})

有人可以帮助我获取基于用户输入的数据帧(数据)吗?

r for-loop shiny subset
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.