微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何根据用户在 Shiny 中的选择将新列添加到数据框中的特定位置?

如何解决如何根据用户在 Shiny 中的选择将新列添加到数据框中的特定位置?

我正在尝试创建一个 Shiny 应用程序,您可以在其中选择要添加的列。为了选择列,我使用了 checkBoxGroupInput 以便能够在用户需要时添加所有列(或超过 1 个)。

没有新列的原始数据框是这样的:

var1<-rep(c(1:4),3)
var2<-c('a','b','c','a','c')
var3<-c(1,2,3,1,23,14,15,12,20,21,22)
var4<-c(23,22,45)
df<-data.frame(var1,var2,var3,var4)

> df
   var1 var2 var3 var4
1     1    a    1   23
2     2    b    2   23
3     3    c    3    1
4     4    a    1    2
5     1    b   23    3
6     2    c   14   14
7     3    a   15   15
8     4    b   12   20
9     1    c   23   21
10    2    a   20   22
11    3    b   21   23
12    4    c   22   45

最终的数据框(如果用户选择所有新列)将是这样的,具有这些准确的位置。

#New columns
col1 <-rep(c("Name"),12)
col2 <- rep(c("Function"),12)
col3 <- rep(c(4:7),3)

final_df <- data.frame(col1,col2,col3,var1,var4)

> final_df
   col1     col2 col3 var1 var2 var3 var4
1  Name Function    4    1    a    1   23
2  Name Function    5    2    b    2   23
3  Name Function    6    3    c    3    1
4  Name Function    7    4    a    1    2
5  Name Function    4    1    b   23    3
6  Name Function    5    2    c   14   14
7  Name Function    6    3    a   15   15
8  Name Function    7    4    b   12   20
9  Name Function    4    1    c   23   21
10 Name Function    5    2    a   20   22
11 Name Function    6    3    b   21   23
12 Name Function    7    4    c   22   45

但是,如果用户只想看到 col1 和 col3,我想看到那个顺序(第一个 col1 和第二个 col3)。与其他组合相同。

我一直在寻找如何去做,但我仍然不知道如何将它们添加到我想要的位置(而不是最后)。

现在,我的代码添加了它们,但前提是您只能一一选择。

1

如果您选择 1 个以上,您将不会在数据框中看到 1 个以上的新列。

2

有人可以帮我解决这个问题吗?

非常感谢

代码

library(shiny)
library(DT)

####DATA
var1<-rep(c(1:4),var4)

### NEW COLUMNS THAT I WANT TO ADD
col1 <-rep(c("Name"),3)


#### SHINY APP
ui <- fluidPage(
  
  titlePanel("My table"),sidebarLayout(
    sidebarPanel(
      
      radioButtons("OptionTodo","What do you want to do?",c("See the table" = "table","See more columns in the table" = "new_columns")),conditionalPanel(
        condition = "input.OptionTodo == 'new_columns'",checkBoxGroupInput("new_col","What columns do you want to add to the table?",c("col1" = "col1","col2" = "col2","col3" = "col3")))
      
    ),mainPanel(
      dataTableOutput("table"),)
  )
)
# Define server logic required to draw a histogram
server <- function(session,input,output) {
  
  new_df <- reactive({
    data <- df
    
    if(input$OptionTodo == 'new_columns'){
      data <- df
      
      if(input$new_col == "col1"){
        data <- cbind(Name = col1,df)
      }
      
      if(input$new_col == "col2"){
        data <- cbind(Func = col2,df)
      }
      
      if(input$new_col == "col3"){
        data <- cbind(Numb = col3,df)
      }
      return(data)
      
    }
    return(data)
    
  })
  
  output$table<- renderDataTable({
    new_df()
  })
  
}

# Run the application 
shinyApp(ui = ui,server = server)

解决方法

不要将这些向量单独包含在数据帧中。从数据框中选择列更容易。试试这个方法 -

library(dplyr)
library(shiny)
library(DT)

####DATA
var1<-rep(c(1:4),3)
var2<-c('a','b','c','a','c')
var3<-c(1,2,3,1,23,14,15,12,20,21,22)
var4<-c(23,22,45)
col1 <-rep(c("Name"),12)
col2 <- rep(c("Function"),12)
col3 <- rep(c(4:7),3)
df<-data.frame(col1,col2,col3,var1,var2,var3,var4)

#### SHINY APP
ui <- fluidPage(
  titlePanel("My table"),sidebarLayout(
    sidebarPanel(
      radioButtons("OptionToDo","What do you want to do?",c("See the table" = "table","See more columns in the table" = "new_columns")),conditionalPanel(
        condition = "input.OptionToDo == 'new_columns'",checkboxGroupInput("new_col","What columns do you want to add to the table?",c("col1" = "col1","col2" = "col2","col3" = "col3")))
      
    ),mainPanel(
      dataTableOutput("table"),)
  )
)
# Define server logic required to draw a histogram
server <- function(session,input,output) {
  new_df <- reactive({
    data <- df %>% select(var1:var4)
    if(input$OptionToDo == 'new_columns'){
      data <- df %>% select(all_of(input$new_col),var1:var4)
      return(data)
    }
    return(data)
  })
  
  output$table<- renderDataTable({
    new_df()
  })
}

# Run the application 
shinyApp(ui = ui,server = server)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。