簡體   English   中英

閃亮錯誤:參數暗示行數不同

[英]Shiny Error: arguments imply differing number of rows

我正在嘗試開發一個簡單的應用程序,該程序可從Kijiji網站獲取本地分類廣告。 我制作了一個具有幾乎相同的完全相同腳本的類似應用程序,但是我沒有收到下面描述的錯誤,所以我不知道該腳本出了什么問題。 我嘗試了所有我能想到的,但無法使它起作用。

server.R df數據幀的結構如下:(請注意,我已將值截斷以使其更具可讀性)

'data.frame':   38 obs. of  7 variables:
 $ Title            : chr  "Baby-boy 3-6 month" ...
 $ Price            : num  0 6.92 8 10 10 15 15 15 20 20 ...
 $ Short.Description: chr  "Give for free ..."
 $ Address          : chr  "Calgary, AB T2Z 0V6" "341 ..."
 $ Date             : Date, format: "2014-12-05" "2014-10-28" "2014-12-05" ...
 $ Full.Description : chr  "Give for free some ..."
 $ Link             : chr  "http://www.kijiji...."

可以復制如下:(一個具有5條記錄的示例,用於演示)

df <- data.frame(
Title= c("Baby-boy 3-6 month","Giggle Life Optimize Cloth Diapers & 4 layer mixed insert", "Beluga Baby","Baby sled",  "Avent Sterilizer & Various Medela Items"),
Price= c(0.00,  6.92,  8.00, 10.00, 10.00),
Short.Description=c("Give for free some staff for boy 3-6 month. Live in New Brighton, SE", "If you have any questions or are looking to order please don't hesitate to call our local line 587-774-2404, toll free line 1-877-883-3069 or visit our website…","Calgary! Fall in love with your all natural and freshly handmade products for mama and baby. www.belugaskincare.com Like us on Facebook.com/belugaskincare FREE shipping in Canada with a $25 cart!…", "Yellow plastic baby sled Safety seat belt Tow rope Dimensions: 23\" long x 14 1/2\" wide x 12\" high $10 Located in Willow Park off Southland Dr. and Fairmount Dr. SE CALL: 403 460 - 0978 (will not…", "Avent microwave sterilizer in good condition 1 Avent bottle 2 Medela bottles 1/2 box of unused Medela pump and save bags 1bag of replaceable Medela pump parts From clean, smoke free home. Take all…"),
Address=c("Calgary, AB T2Z 0V6","341 Westvale Drive, Waterloo, ON N2T 2M2","Canada", "Calgary, AB T2J 1H6, Canada","Calgary, AB T2W, Canada"),
Date=c(as.Date(c( "2014-12-05", "2014-10-28", "2014-12-05", "2014-12-05", "2014-12-05"))),
Full.Description=c("Give for free some staff for boy 3-6 month. Live in New Brighton, SE", "If you have any questions or are looking to order please don't hesitate to call our local line 587-774-2404, toll free line 1-877-883-3069 or visit our website http://www.gigglelife.com/catalog/?Calgary.  \rEnter the promo code \"KIJCALGARY\" in the comment box when ordering to receive a free gift with your order.\rThe new Giggle Life Optimize Cloth Diaper is very affordable, effective, and comfortable. It is simply the best value on market. These reusable diapers are one size fits all (7-36lbs). \rThey are $6.92 each when you purchase 12. Please note there is a $1 surcharge for pattern designs. \rManufacturer’s Warranty \rSupport for as long as you use the diapers! \r****FREE SHIPPING ACROSS CANADA!!**** \rAll diapers are new and individually packaged - never worn OR washed. They are high quality, one-size-fits-all, pocket cloth diapers. \rAll orders are shipped via Canada Post within 24 hours of placing your order. We send all packages Expedited with insurance and a tracking number, which is provided immediately upon shipping out. \rLooking for other package sizes? We have packages of 10, 24, 38 and 100 also..", "Calgary! Fall in love with your all natural and freshly handmade products for mama and baby. www.belugaskincare.com Like us on Facebook.com/belugaskincare FREE shipping in Canada with a $25 cart! \"making life simpler and greener so you can better do the things you are most passionate about\" - Beluga Skin CareThis ad was posted with the Kijiji mobile app.This ad was posted WITH the Kijiji mobile app.", "Yellow plastic baby sledSafety seat beltTow ropeDimensions: 23\" long x 14 1/2\" wide x 12\" high$10Located in Willow Park off Southland Dr. and Fairmount Dr. SECALL: 403 460 - 0978  (will not respond to texts at this land line number)   TEXT OR CALL: 403 463 - 1038PLEASE SEE MY OTHER ADSThis ad was posted with the Kijiji mobile app.This ad was posted WITH the Kijiji mobile app.","Avent microwave sterilizer in good condition1 Avent bottle2 Medela bottles1/2 box of unused Medela pump and save bags1bag of replaceable Medela pump partsFrom clean, smoke free home. Take all for 10$This ad was posted with the Kijiji mobile app.This ad was posted WITH the Kijiji mobile app."),
Link= c("http://www.kijiji.ca/v-baby-clothes-3-6-months/calgary/baby-boy-3-6-month/1037502424","http://www.kijiji.ca/v-baby-bathing-changing-diapers/calgary/giggle-life-optimize-cloth-diapers-4-layer-mixed-insert/1008481541?src=topAdSearch", "http://www.kijiji.ca/v-baby-bathing-changing-diapers/calgary/beluga-baby/1037483143" , "http://www.kijiji.ca/v-baby-toy/calgary/baby-sled/1037493662", "http://www.kijiji.ca/v-baby-feeding-high-chair/calgary/avent-sterilizer-various-medela-items/1037481182"  )
    )

這是server.R 請注意,我已注釋掉源代碼,以避免提供源代碼和延遲的計算。 請使用上面給出的df數據幀重現結果:

#Install required packages
ListofPackages= c('shiny','ggplot2','scales')
NewPackages= ListofPackages[!(ListofPackages %in% installed.packages()[,'Package'])]
if(length(NewPackages)>0) install.packages(NewPackages)

#Load required packages
lapply(ListofPackages,require,character.only=TRUE)

#Load source code
#source('C:/Users/Bahae.Omid/Google Drive/My R Case Studies/Shiny Apps/Kijiji App/adscraper.R',local=TRUE)


shinyServer(function(input,output){

    #Create a reactive function to deal with inputs of the user
    search <- reactive({
       if(length(input$t)>0) {ind <- grep(input$t,df[,'Title'],ignore.case = T); df <- df[ind,] }
       if(length(input$d)>0) {ind <- grep(input$d,df[,'Full.Description'],ignore.case = T); df <- df[ind,]}
       if(length(input$a)>0) {ind <- grep(input$a,df[,'Address'],ignore.case = T); df <- df[ind,]}
       if(input$p >=0) {ind <- df[,'Price']<=input$p ; df <- df[ind,]}
    })

    #Send the searchresult table to ui.R
    output$searchresult <- renderDataTable({
      input$action1 #triggered only when button is pressed
      if(input$action1==0) return() 
      else{isolate({
        transformed <- transform(search(), URL = paste('<a href = ', shQuote(Link), '>', 'Click</a>'))
        transformed[-7] #Remove the old Link column
      })
      }
    }, option=list(autoWidth=FALSE,pageLength=100,
                   columnDefs = list(list(targets =c(2,5,7) -1, searchable = FALSE),list(sWidth="75px",aTargets = list(4,5)))))

    #Allow user to download the data via downloadhandler
    output$down <- downloadHandler(
        filename='filtered.csv',
        content=function(file){write.csv(search(),file,row.names=FALSE)}
    )



})

這是ui.R 請注意,我已注釋掉圖像標簽,以避免在運行代碼時出錯:

shinyUI(fluidPage(

    #Display datatable filters on top
    tags$head(tags$style("tfoot {display: table-header-group;}")),        

    #Add a title
    #img(src="kijiji.gif", height = 100, width = 100),
    #img(src="plus.png", height = 20, width = 20),
    #img(src="plus.png", height = 20, width = 20),    

    #Use the Sidebar layout
    sidebarLayout(
        sidebarPanel(


            #Add fields to search by and download button to allow exporting search results to csv.
             h5('Note: Running the app takes a little while to run at startup.'),
             helpText('Ad Title:'),
             textInput('t',''),
             helpText('Description:'),
             textInput('d',''),
             helpText('Address:'),
             textInput('a',''),
             sliderInput('p','Show Prices up to:',min = 0,max = 10000,step = 50,value = 10000),
             actionButton('action1','Search!'), 
             br(),
             br(),
             helpText('Click below to download the results of your search:'),
             downloadButton('down','Download')

        ),


        mainPanel(
        dataTableOutput('searchresult')
        )

    )   
))

當我運行應用程序時,所有過濾器似乎都可以正常工作,但是當過濾器未從數據幀(即0行數據幀)返回任何記錄時,出現以下錯誤:

Error in data.frame(structure(list(Title = character(0), Price = numeric(0),  : 
  arguments imply differing number of rows: 0, 1

我已經使用renderTable而不是renderDataTable測試了相同的腳本,並且效果很好。 但是我想要DataTable格式的輸出,特別是我希望它能夠在另一個相同的應用程序中工作。

如果需要更多說明,請告訴我。

search()返回零行的data.frame

paste('<a href = ', shQuote(search()$url), '>', 'Click</a>')

回報

"<a href = \"\" > Click</a>"

然后,您嘗試將這一行的新列綁定到沒有行的data.frame中。 因此,錯誤消息。

您可以使用類似的方法修復它

  transformed <- transform(search()
                           , Link = if(length(url) > 0){
                             paste('<a href = ', shQuote(url), '>', 'Click</a>')
                           }else{
                             character(0)
                           }
  )

該錯誤基本上說:沒有任何顯示。 因此,使用browser()並檢查控制台中的當前輸出是什么

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM