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

How to add new columns into a specific position in a dataframe depending on user's choice in Shiny?

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

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

var1<-rep(c(1:4),3)
var2<-c('a','b','c','a','b','c','a','b','c','a','b','c')
var3<-c(1,2,3,1,23,14,15,12,23,20,21,22)
var4<-c(23,23,1,2,3,14,15,20,21,22,23,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

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

#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,var2,var3,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)。与其他组合相同。

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

现在,我的代码会添加它们,但前提是您 select 一个一个地添加。

如果您 select 超过 1 个,您将不会在数据框中看到超过 1 个新列。

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

非常感谢

代码:

library(shiny)
library(DT)

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

### NEW COLUMNS THAT I WANT TO ADD
col1 <-rep(c("Name"),12)
col2 <- rep(c("Function"),12)
col3 <- rep(c(4:7),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)

而不是将这些向量单独包含在数据框中。从数据框中 select 列更容易。试试这个方法 -

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

####DATA
var1<-rep(c(1:4),3)
var2<-c('a','b','c','a','b','c','a','b','c','a','b','c')
var3<-c(1,2,3,1,23,14,15,12,23,20,21,22)
var4<-c(23,23,1,2,3,14,15,20,21,22,23,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)