簡體   English   中英

如何根據用戶在 Shiny 中的選擇將新列添加到 dataframe 中的特定 position 中?

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

我正在嘗試創建一個 Shiny 應用程序,您可以在其中選擇要添加的列。 為了 select 列,我使用了 checkBoxGroupInput 以便能夠在用戶需要時添加所有列(或多於 1 個)。

沒有新列的原始 dataframe 是這樣的:

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

最終的 dataframe(如果用戶選擇所有 NEW 列)將是這樣的,具有這些確切的位置。

#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)。 與其他組合相同。

我一直在搜索如何做到這一點,但我仍然不知道如何將它們添加到我想要的 position 中(而不是最后)。

現在,我的代碼添加了它們,但前提是您只需要 select 一個一個。

1

如果您的 select 超過 1 個,您將不會在 dataframe 中看到超過 1 個新列。

2

有人可以幫我嗎?

非常感謝

代碼:

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)

不是讓這些向量單獨包含在 dataframe 中。 從 dataframe 到 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM