How can you make a merge of two reactive datas, in which the user can select the columns through which to make the merge? That is, the user selects who is by.x and by.y

library(shiny)

ui<-fluidPage(
  titlePanel(title="Mi aplicación"),
  sidebarLayout(
    sidebarPanel(
      fileInput("fileA", label = "Cargue aqui su archivo Nro. 1 en formato txt"),
      fileInput("fileB", label = "Cargue aqui su archivo Nro. 2 en formato txt")
      ),
    mainPanel(
      tabsetPanel(type="tab" , 
                    tabPanel ("Data 1" ,
                     tableOutput(outputId="tablita1")),
                    tabPanel("Data 2" ,
                    tableOutput(outputId = "tablita2")),
                   tabPanel("Unión de las datas" ,
                            fluidRow(
                              
                              column(4,uiOutput("seleccione_col1"))
                                     
                              ,
                              
                              column(8,
                                     uiOutput("seleccione_col2"))
                              
                              ,
                              
                              column(10,
                                     downloadButton("downloadData", "Decargar")) 
                              
                                    
                              ))),
                            
                            
                            
                            
      tableOutput(outputId = "tablitaMerge"))
                  ))


server<-function(input,output){
 
 
  dataA <- reactive({
    file1 <- input$fileA
    
    if(is.null(file1)){return()}
    file1$datapath
    
        read.table(file1$datapath,sep="\t",header=TRUE)
  })
  
  
  
  output$tablita1 <- renderTable({
    if(is.null(dataA())){return()}
    dataA()
  }, type = "html", bordered = TRUE, align = "c", width = 5)
  
  
  
  
  dataB <- reactive({
    file2 <- input$fileB
    
    if(is.null(file2)){return()}
    file2$datapath
    
    read.table(file2$datapath,sep="\t",header=TRUE)
  })
  
  
  
  output$tablita2 <- renderTable({
    if(is.null(dataB())){return()}
    h=dataB()
  }, type = "html", bordered = TRUE, align = "c", width = 5)
  
  
  
  output$seleccione_col1 <- renderUI({
  
 seleccion <- names(dataA())
  selectInput(inputId="columnsA", (("Seleccione la columna")), 
  choices  = seleccion,
  selected = seleccion[1])
                                  })
  
  
  
  output$seleccione_col2 <- renderUI({
    
    seleccion <- names(dataB())
    selectInput(inputId="columnsB", (("Seleccione la columna")), 
                choices  = seleccion,
                selected = seleccion[1])
  })
  
  
  
  output$tablitaMerge<- renderTable({
   A=as.character(input$columnsA)
   B=as.character(input$columnsB)
    j=merge(dataA(),dataB(),by.x=A,
            by.y=B,all.x=TRUE)
  })
  
  
  
  #https://shiny.rstudio.com/articles/download.html
  output$downloadData <- downloadHandler(
    filename = function() {
      paste(input$dataset, ".csv", sep = "")
    },
    content = function(file) {
      write.csv(datasetInput(), file, row.names = FALSE)
    }
  )
  
  
}
shinyApp(ui=ui,server=server)

This slightly modified version of your code seems to work. All I did was change the data input from reading files to explicitly constructing the data frames. I put action buttons on the side bar to trigger the creation of the data frames. The download throws an error and I did not investigate that. Do you agree that the merge works?

library(shiny)

ui<-fluidPage(
  titlePanel(title="Mi aplicación"),
  sidebarLayout(
    sidebarPanel(
      actionButton("fileA", label = "Cargue aqui su archivo Nro. 1 en formato txt"),
      actionButton("fileB", label = "Cargue aqui su archivo Nro. 2 en formato txt")
    ),
    mainPanel(
      tabsetPanel(type="tab" , 
                  tabPanel ("Data 1" ,
                            tableOutput(outputId="tablita1")),
                  tabPanel("Data 2" ,
                           tableOutput(outputId = "tablita2")),
                  tabPanel("Unión de las datas" ,
                           fluidRow(
                             
                             column(4,uiOutput("seleccione_col1"))
                             
                             ,
                             
                             column(4,
                                    uiOutput("seleccione_col2"))
                             
                             ,
                             
                             column(4,
                                    downloadButton("downloadData", "Decargar")) 
                             
                             
                           ))),
      
      
      
      
      tableOutput(outputId = "tablitaMerge"))
  ))


server<-function(input,output){
  
  
  dataA <- eventReactive(input$fileA,{
    data.frame(Men = c("Bob", "Steve", "Steve", "Bob", "Bob"),
               Women = c("Mary", "Mary", "Sue", "Mary", "Sue"),
               Value = 1:5)
  })
  
  
  
  output$tablita1 <- renderTable({
    if(is.null(dataA())){return()}
    dataA()
  }, type = "html", bordered = TRUE, align = "c", width = 5)
  
  
  
  
  dataB <- eventReactive(input$fileB,{
    data.frame(Men = c("Bob", "Steve", "Bob", "Steve", "Bob"),
               Women = c("Mary", "Sue", "Sue", "Mary", "Mary"),
               Value = 11:15)
  })
  
  
  
  output$tablita2 <- renderTable({
    if(is.null(dataB())){return()}
    h=dataB()
  }, type = "html", bordered = TRUE, align = "c", width = 5)
  
  
  
  output$seleccione_col1 <- renderUI({
    
    seleccion <- names(dataA())
    selectInput(inputId="columnsA", (("Seleccione la columna")), 
                choices  = seleccion,
                selected = seleccion[1])
  })
  
  
  
  output$seleccione_col2 <- renderUI({
    
    seleccion <- names(dataB())
    selectInput(inputId="columnsB", (("Seleccione la columna")), 
                choices  = seleccion,
                selected = seleccion[1])
  })
  
  
  
  output$tablitaMerge<- renderTable({
    A=as.character(input$columnsA)
    B=as.character(input$columnsB)
    j=merge(dataA(),dataB(),by.x=A,
            by.y=B,all.x=TRUE) #
  })
  
  
  
  #https://shiny.rstudio.com/articles/download.html
  output$downloadData <- downloadHandler(
    filename = function() {
      paste(input$dataset, ".csv", sep = "")
    },
    content = function(file) {
      write.csv(datasetInput(), file, row.names = FALSE)
    }
  )
  
  
}
shinyApp(ui=ui,server=server)

If my version works for you, try setting stringsAsFactors = FALSE in your call to read.table()

Thanks but it does not work for me. I need to upload 2 files with inputfile. Then I need the user to select by.x and by.y to make the merge,

I am sorry I was unclear. I did not intend my version to achieve everything you need. I just want you to test it to see if the merge works as you expect. If it does, then the problem with your application almost certainly has something to do with how the data are being read, since that is the difference between my version and yours. I cannot test your version, because I do not have your files.

Please try my version and if the merging works, try using stringsAsFactors = FALSE in your application.

I already achieved it, the problem had it in the ui. Thanks

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.