Shiny App中的文件输入错误

问题描述:

我正在制作一个需要数据集输入的Shiny App。我能得到它与数据集中的一个值,但是在工作的时候我上传多个值的错误信息,那就说:Shiny App中的文件输入错误

实例数据集(CSV)输入时的作品:

category 
action 

示例数据集(csv)输入时不起作用:

category 
action 
noir 

Error: replacement has 1 row, data has 0 

以下是我的用户界面和服务器。完全可重复的设置。

它只适用于我输入类别“action”,但是当我在一个csv列中输入类别“action”和“noir”时,出现错误。

服务器:

library(shiny) 
library(readr) 
library(dplyr) 

actor <- c('Matt Damon','George Clooney','Brad Pitt', 'Clive Owen', 'Morgan Freeman', 'Edward Norton', 'Adrian Granier') 
category<-c('action', 'action', 'noir', 'action', 'thriller', 'noir', 'action') 
movie <- c('Oceans Eleven', 'Oceans Twelve', 'Fight Club', 'Children of Men', 'The Shawshank Redemption', 'American History X', 'Entourage') 
movies <- c(21, 23, 26, 12, 90, 14, 1) 
cost <- c(210000, 2300000, 260000, 120000, 90000, 140000, 10000) 
Type <- c('A','B','C', 'A', 'B', 'C', 'A') 

moviedata<-data.frame(actor, category, movie, movies, cost, Type) 

shinyServer(function(input,output){ 
    data <- reactive({ 
    file1 <- input$file 
    if(is.null(file1)){return()} 
    read_csv(file=file1$datapath) 

    }) 
    output$sum <- renderTable({ 
    if(is.null(data())){return()} 
    test<-subset(moviedata, category %in% data()) 
    test1<-filter(test, `Type`==input$file4) 
    test1$`BUDGET`<-input$file5 
    test1$CHECKING<-ifelse(test1$`BUDGET`>test1$cost,"YES", "NO") 
    filter(test1, CHECKING=="YES") 
    }) 

    output$tb <- renderUI({ 
    if(is.null(data())) 
     h5("Powered by", tags$img(src='optimatic.png')) 
    else 
     tabsetPanel(tabPanel("Summary", tableOutput("sum"))) 
    }) 

} 
) 

UI

library(shiny) 
shinyUI(fluidPage(
    titlePanel("Actor Finder"), 
    sidebarLayout(
    sidebarPanel(
     fileInput("file","Upload Category List: Must have category as header"), 
     selectInput("file4", "Select Type", c("A" = "A", 
              "B" = "B", 
              "C" = "C"), selected = "A"), 
     numericInput("file5", "Choose cost", 1000000000), 
     tags$hr()),  
    mainPanel(
     uiOutput("tb") 
    ) 

) 
)) 

因为您读过与readr CSV文件,它是一个tbl_df data.frame,其中 - 与基地data.frame - 这么想的得到简化当它包含单个列时的向量。

比较:

> c(1,2) %in% tibble(a=c(1,2))[,1] 
[1] FALSE FALSE 
> c(1,2) %in% tibble(a=c(1,2))[[1]] 
[1] TRUE TRUE 
> c(1,2) %in% data.frame(a=c(1,2))[,1] 
[1] TRUE TRUE 
> c(1,2) %in% data.frame(a=c(1,2))[[1]] 
[1] TRUE TRUE 

您可以使用此语法只取第一列:

category %in% data()[[1]] 
+0

感谢您的帮助,真是棒极了! @HubertL –

+0

我问了一个类似的问题,将格式更改为数据表并添加一个下载按钮。如果你对这个主题了解得更多,我会非常感谢几周前发布的这个问题的帮助,我仍然没有找到答案:http://*.com/questions/41939805/download-handler-上rendertable-数据帧中闪亮 –