1

I am trying to make an app that according to the selection of the user will subsetting the dataframe of the data. After that I want to create a new dataframe with unique values of col1 abd and the new dataframes added as new columns to the existing dataframe depending on users choice (like from a long shape of the dates making them in wide). Any thoughts??? my dataset is that

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)

what ia am trying to achive is when first the user selects 'a'

the results to be this

 col1   col2    col3    col4
    1    a       1  
    2    a      20      22
    3    a      15      15
    4    a               2

Then if the user selects c the new dataframe will be

 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 

and so on

6
  • 2
    What's your issue? Commented Mar 31, 2017 at 15:47
  • I want to in the dataset dfsel which I create insert each time the user choose another input append the corresping subset on the dfsel. One problem is that the column names are the same and I cant just append them so how reactivly can I change the column names and append the new values? Commented Mar 31, 2017 at 15:52
  • You want to add rows to the displayed table as the user selects subsets? Commented Mar 31, 2017 at 15:56
  • To add columns actually Commented Mar 31, 2017 at 15:57
  • Please try and explain yourself more carefully. Maybe you need a diagram. It is not clear if you are talking about rows or columns. Commented Apr 1, 2017 at 13:20

1 Answer 1

1

Ok, I think I understand what you want. This does it I believe.

I made the following changes to your example

  • added a new datatable (rv$dfnew) as reactiveValues to keep track of our data table as it grows
  • Changed the display to show the currently selected part of the data frame as well as the growing dfnew table
  • Added a count variable that is appended to the columns as we proceed to keep them all unique.
  • Added an actionButton doAppendCols to append the columns.

Here is the code:

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)

And here is the sample output:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

thank you!!! yeas that was exacly what I was trying to do and I had problem with the name of the columns! thank you!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.