emr2
emr2

Reputation: 1732

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

I am trying to create a Shiny app where you can choose what columns you want to add. In order to select the columns I have used checkBoxGroupInput to be able to add all of them (or more than 1) if the user wants.

The original dataframe without the new columns is like this:

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

And the final dataframe (if the user selects all the NEW columns) will be like this, with these exactly positions.

#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

However, if the user only wants to see col1 and col3, I want to see that order (first col1 and the second col3). Same with the other combinations.

I have been searching a lot about how to do it, but I still don't know how to do add them in the position that I want (and not at the end).

Now, my code adds them, but only if you only select one by one.

1

If you select more than 1 you won't see more than 1 new column in the dataframe.

2

Does anyone can help me with this?

Thanks very much

Code:

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)

Upvotes: 2

Views: 108

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389275

Instead of having those vectors separately include them in the dataframe. It is easier to select columns from the dataframe. Try this approach -

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)

Upvotes: 1

Related Questions