-1

I am trying to join two dataframes using user selected two columns from a datatable in a shiny app

I'd like to join them using the column names which I have put into a variable. However when I do the left join I get an error. When I use the character of the name it works fine.

My code

EndoDate<-colnames(RV$data[as.numeric(input$endotable_columns_selected[1])])
EndoNum<-colnames(RV$data[as.numeric(input$endotable_columns_selected[2])])
PathDate<-colnames(RV2$data[as.numeric(input$endotable_columns_selected[1])])
PathNum<-colnames(RV2$data[as.numeric(input$endotable_columns_selected[2])])

This doesn't work:

left_join(RV$data,RV2$data,by = c(EndoDate=PathDate,EndoNum=PathNum))

..But this does work

left_join(RV$data,RV2$data,by = c("endo_resultperformed"="endo_resultperformed","hospitalnumber"="hospitalnumber"))

I need to use the column names chosen by the user. How can I do this and do the join? Is this a symbol dplyr problem?

Sebastian Zeki
  • 6,690
  • 11
  • 60
  • 125
  • Is this of use: https://dplyr.tidyverse.org/articles/programming.html#different-expressions. I cannot try it, as you did not share example data. Also, do you mean datatable or dataframe? If the first, then please add the tag as well. – MartijnVanAttekum Jun 14 '19 at 09:37

1 Answers1

1

We can use setNames

library(dplyr)
band_members$id <- 1:3
band_instruments$id_num <- c(2,3,1)

xid='id'
xid1='name'
yid='id_num'
yid1='plays'

band_members %>% left_join(band_instruments, by=setNames(nm=c(xid,xid1),c(yid,yid1)))

# A tibble: 3 x 4
  name  band       id plays 
  <chr> <chr>   <dbl> <chr> 
1 Mick  Stones      1 NA    
2 John  Beatles     2 guitar
3 Paul  Beatles     3 bass

PS: This answer based on @MrFlick answer here

A. Suliman
  • 12,923
  • 5
  • 24
  • 37