I have two reactive dataframes (for explanation purposes I'm creating mock dataframes) and I need them to be joined in shiny using input fields as keys.
library(shiny)
library(tidyverse)
df <- data.frame(key1=c(1:4),key2 = c(5:8),
left= c("l1", "l2", "l3", "l4"),
right = c("r1", "r2", "r3", "r4"))
left_df <- df[,1:3]
right_df <- df[,c(1:2,3)]
ui <- fluidPage(
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
selectInput("key_right", "Right Key", c("key1", "key2")),
selectInput("key_left", "Right Key", c("key1", "key2"))
),
mainPanel(
tableOutput("final_data")
)
)
)
server <- function(input, output) {
left_df2 <- reactive({
left_df
})
rightt_df2 <- reactive({
right_df
})
final_df <- reactive({
left_df2() %>%
left_join(right_df2(), by = c(input$key_right = input$key_left))
})
output$final_data <- reactive({
final_df()
})
}
I get this error:
Error in parse(file, keep.source = FALSE, srcfile = src, encoding = enc) :
/app.R:37:60: unexpected '='
36: left_df2() %>%
37: left_join(right_df2(), by = c(input$key_right =
Error in sourceUTF8(fullpath, envir = new.env(parent = sharedEnv)) :
I need to perform the join by either key1 or key2 and that should be a user input as there is a chance the user uploads a file and may have one key or the other.
Thanks in advance!