Shiny 有没有一种基于R中的下拉菜单合并数据的好方法?

Shiny 有没有一种基于R中的下拉菜单合并数据的好方法?,shiny,Shiny,我一直在尝试根据下拉列表中的输入将数据与另一个数据集合并。我刚刚开始学习R,遇到了一些问题,想知道是否有更好的方法来解决这个问题 我得到一个错误,它无法将类c(ReactiveExpr,reactive)强制到数据帧 library(shiny) library(plyr) library(dplyr) library(xlsx) server <- function(input, output){ annotation1 <- read.xlsx("i

我一直在尝试根据下拉列表中的输入将数据与另一个数据集合并。我刚刚开始学习R,遇到了一些问题,想知道是否有更好的方法来解决这个问题

我得到一个错误,它无法将类c(ReactiveExpr,reactive)强制到数据帧

library(shiny)
library(plyr)
library(dplyr)
library(xlsx)

server <- function(input, output){
    
    annotation1 <- read.xlsx("input1.xlsx", sheetIndex = 1, header = TRUE)
    
    annotation2 <- read.xlsx("input2.xlsx", sheetIndex = 1, header = TRUE)
    
    data_input <- eventReactive(input$userfile, {
        df <- read.xlsx(input$userfile$datapath, sheetIndex = 1, header = TRUE)
    })
    
    output$data_input <- renderTable(data_input())
    output$annotation <- renderTable(annotation)
    
    data_species <-  c("Set1", "Set2")
    
    # Drop-down selection box for which data set
    output$choose_species <- renderUI ({
        selectInput("species", "Species", as.list(data_species))
    })
    
    output$mergeddata <- renderTable({
        if(input$species == "Set1"){
            eventReactive("Set1",({left_join(data_input(), annotation1, by = c("Column1" = "Column1"))}))
        }
        
        else if(input$species == "Set2"){
            eventReactive("Set2",({left_join(data_input(), annotation2, by = c("Column1" = "Column1"))}))
        }
        
    })
}

ui <- fluidPage(
    
    titlePanel(
        div("Test")
    ),
    

    sidebarLayout(
        
        sidebarPanel(
            fileInput("userfile", "Input File", multiple =FALSE,
                      buttonLabel = "Browse Files", placeholder = "Select File"),
            
            uiOutput("choose_species"),
            
            uiOutput("choose_annotations"),
        ),
        
        mainPanel(
            tableOutput("mergeddata"),
            br()
        ),
    
),

)


# Run the application 
shinyApp(ui = ui, server = server)

库(闪亮)
图书馆(plyr)
图书馆(dplyr)
图书馆(xlsx)

服务器一般来说,您的方法似乎还可以。您得到的错误来自于该行

eventReactive("Set1",({left_join(data_input(), annotation1, by = c("Column1" = "Column1"))}))
eventReactive
返回一个(未计算的)reactive表达式,您尝试使用
renderable
将其呈现为data.frame。要避免这种情况,您可以使用:

eventReactive("Set1",({left_join(data_input(), annotation1, by = c("Column1" = "Column1"))}))()
但是,这里不需要
eventReactive
,因为您的反应性来自
input$species
(您希望根据此输入更改表输出)。因此,您只需使用:

output$mergeddata <- renderTable({
  
  if(input$species == "Set1"){
    merge_data <- annotation1
  } else {
    merge_data <- annotation2
  }
  
  left_join(data_input(), merge_data, by = c("Column1"))
  
})

output$mergeddata一般来说,您的方法似乎还可以。您得到的错误来自于该行

eventReactive("Set1",({left_join(data_input(), annotation1, by = c("Column1" = "Column1"))}))
eventReactive
返回一个(未计算的)reactive表达式,您尝试使用
renderable
将其呈现为data.frame。要避免这种情况,您可以使用:

eventReactive("Set1",({left_join(data_input(), annotation1, by = c("Column1" = "Column1"))}))()
但是,这里不需要
eventReactive
,因为您的反应性来自
input$species
(您希望根据此输入更改表输出)。因此,您只需使用:

output$mergeddata <- renderTable({
  
  if(input$species == "Set1"){
    merge_data <- annotation1
  } else {
    merge_data <- annotation2
  }
  
  left_join(data_input(), merge_data, by = c("Column1"))
  
})
输出$mergeddata