将dataframe附加到数据库中的现有dataframe上
我试图使一个应用程序,根据用户的选择将子集的数据帧的数据。之后,我想创建一个新的数据框,其唯一值为col1 abd,新的数据框作为新列添加到现有的数据框中,具体取决于用户的选择(比如从一个很长的日期形状开始,使它们变宽)。 有什么想法吗??? 我的数据集是将dataframe附加到数据库中的现有dataframe上,r,shiny,R,Shiny,我试图使一个应用程序,根据用户的选择将子集的数据帧的数据。之后,我想创建一个新的数据框,其唯一值为col1 abd,新的数据框作为新列添加到现有的数据框中,具体取决于用户的选择(比如从一个很长的日期形状开始,使它们变宽)。 有什么想法吗??? 我的数据集是 col1<-rep(c(1:4),3) col2<-c('a','b','c','a','b','c','a','b','c','a','b','c') col3<-c(1,2,3,NA,NA,14,15,NA,NA,20,
col1<-rep(c(1:4),3)
col2<-c('a','b','c','a','b','c','a','b','c','a','b','c')
col3<-c(1,2,3,NA,NA,14,15,NA,NA,20,21,22)
col4<-c(NA,NA,1,2,3,14,15,20,21,22,NA,NA)
df<-data.table(col1,col2,col3,col4)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
selectInput('Ind', 'Index',choices = c(unique(df$col2))),
dataTableOutput('Table')
)
# Define server logic required to draw a histogram
server <- function(input, output) {
dfsel<-reactive({dfnew<-subset(df,col2==input$Ind)
dfnew
})
output$Table<-renderDataTable(dfsel())
}
# Run the application
shinyApp(ui = ui, server = server)
然后,如果用户选择c,则新的数据帧将被删除
col1 col2 col3 col4 col2 col3 col4
1 a 1 c 21
2 a 20 22 c 14 14
3 a 15 15 c 3 1
4 a 2 c 22
等等好的,我想我明白你想要什么。我相信是这样的 我对您的示例做了以下更改
- 添加了一个新的datatable(
)作为反应值,以跟踪数据表的增长rv$dfnew
- 更改显示以显示数据帧的当前选定部分以及不断增长的
表格dfnew
- 添加了一个count变量,该变量在我们继续操作时附加到列中,以保持它们的唯一性
- 添加了一个actionButton
来追加列doAppendCols
library(shiny)
library(data.table)
col1<-rep(c(1:4),3)
col2<-c('a','b','c','a','b','c','a','b','c','a','b','c')
col3<-c(1,2,3,NA,NA,14,15,NA,NA,20,21,22)
col4<-c(NA,NA,1,2,3,14,15,20,21,22,NA,NA)
df<-data.table(col1,col2,col3,col4)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Append Columns"),
selectInput('Ind', 'Index',choices = c(unique(df$col2))),
actionButton("doAppendCols","Append Columns"),
h2("Selected Data"),
dataTableOutput('selTable'),
h2("New Table"),
dataTableOutput('newTable')
)
# Define server logic required to draw a histogram
server <- function(input, output) {
rv <- reactiveValues(dfnew=NULL,count=0)
observeEvent(input$doAppendCols,{
rv$count <- rv$count+1
dfs <- dfsel()
# add a running number as suffix to column names to make the columns unique
newsuffix <- paste0(".",rv$count)
ncols <- ncol(dfs)
names(dfs)[2:ncols] <- paste0( names(dfs)[2:ncols],newsuffix )
if(is.null(rv$dfnew)){
rv$dfnew <- dfs
} else {
rv$dfnew <- merge(rv$dfnew,dfs,by="col1")
}
})
dfsel<-reactive({
df<-subset(df,col2==input$Ind)
df <- df[order(df$col1),]
})
output$selTable<-renderDataTable(dfsel())
output$newTable<-renderDataTable(rv$dfnew)
}
# Run the application
shinyApp(ui = ui, server = server)
库(闪亮)
库(数据表)
Col1您的问题是什么?我想在每次用户选择另一个输入时在创建的数据集dfsel中插入相应的子集,并将其附加到dfsel上。一个问题是列名是相同的,我不能只是附加它们,因此我如何才能反应性地更改列名并附加新值?是否要在用户选择子集时向显示的表中添加行?若要实际添加列,请尝试更仔细地解释。也许你需要一张图表。不清楚你说的是行还是列。谢谢!!!是的,这正是我想要做的,我对列的名称有问题!非常感谢。
library(shiny)
library(data.table)
col1<-rep(c(1:4),3)
col2<-c('a','b','c','a','b','c','a','b','c','a','b','c')
col3<-c(1,2,3,NA,NA,14,15,NA,NA,20,21,22)
col4<-c(NA,NA,1,2,3,14,15,20,21,22,NA,NA)
df<-data.table(col1,col2,col3,col4)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Append Columns"),
selectInput('Ind', 'Index',choices = c(unique(df$col2))),
actionButton("doAppendCols","Append Columns"),
h2("Selected Data"),
dataTableOutput('selTable'),
h2("New Table"),
dataTableOutput('newTable')
)
# Define server logic required to draw a histogram
server <- function(input, output) {
rv <- reactiveValues(dfnew=NULL,count=0)
observeEvent(input$doAppendCols,{
rv$count <- rv$count+1
dfs <- dfsel()
# add a running number as suffix to column names to make the columns unique
newsuffix <- paste0(".",rv$count)
ncols <- ncol(dfs)
names(dfs)[2:ncols] <- paste0( names(dfs)[2:ncols],newsuffix )
if(is.null(rv$dfnew)){
rv$dfnew <- dfs
} else {
rv$dfnew <- merge(rv$dfnew,dfs,by="col1")
}
})
dfsel<-reactive({
df<-subset(df,col2==input$Ind)
df <- df[order(df$col1),]
})
output$selTable<-renderDataTable(dfsel())
output$newTable<-renderDataTable(rv$dfnew)
}
# Run the application
shinyApp(ui = ui, server = server)