我有两个响应式数据帧(为了说明方便,我创建了模拟数据帧),需要使用输入字段作为键在 shiny 中将它们连接起来。
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()
})
}
我遇到了这个错误:
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)) :
我需要根据key1或key2执行联接,这应该是用户输入的,因为用户上传文件时可能有一个键或另一个键。
提前致谢!