I can run this code in R from top to end.but I can't run this code out by Single-file Shiny apps. why?

12 views
Skip to first unread message

lfz1...@gmail.com

unread,
Oct 15, 2016, 8:46:26 AM10/15/16
to shinyapps.io Users
my code is down like this, it can tell me the new book price and I can run this code in R from up to down but I can't run it outside like use
> runApp('mydir')
why?

my code:

library(rvest)
library(data.table)
library(dplyr)
#这里是导入网址。研究一下amazon的顺序,直接导入就好
id<-1:5
url_increase_fast<-paste0(
        "http://www.amazon.cn/gp/movers-and-shakers/digital-text/ref=zg_bsms_digital-text_pg_",
        id,
        "?ie=UTF8&pg=",
        id)
url_newest<-paste0(
        "http://www.amazon.cn/gp/new-releases/digital-text/ref=zg_bsnr_digital-text_pg_",
        id,
        "?ie=UTF8&pg=",
        id)
url<-c(url_increase_fast,url_newest)
#这里编写readdata函数,读取网页内容。里面有些不常用的字段,为了最后导出效果好看,我没全部都导。
#有额外需要的可以自己改编,譬如分类啊,好评率啊等等。对我来说,知道价格、书名就够了
readdata<-function(i){
        web<-read_html(url[i],encoding="UTF-8")
        title<-web %>% html_nodes("div.zg_title") %>% html_text()
        title_short<-substr(title,1,20)
        price<-as.numeric(gsub("¥ ","",web %>% html_nodes("div.zg_itemPriceBlock_normal strong.price") %>% html_text()))
        ranking_movement<-web %>% html_nodes("span.zg_salesMovement") %>% html_text()
        rank_number<-as.numeric(gsub("\\.","",web %>% html_nodes("span.zg_rankNumber") %>% html_text()))
        #新书榜里没有销售变动记录,所以记为NA
        if (length(ranking_movement)==0) {ranking_movement=rep(NA,20)
                                          rank_number=rep(NA,20)}
        link<-gsub("\\\n","",web %>% html_nodes("div.zg_title a") %>% html_attr("href"))
        ASIN<-sapply(strsplit(link,split = "/dp/"),function(e)e[2])
        img<-web %>% html_nodes("div.zg_itemImage_normal img")  %>% html_attr("src")
        #这里加上html代码
        img_link<-paste0("<img src='",img,"'>")
        title_link<-paste0("<a href='",link,"'>",title_short,"</a>")
        #合并数据
        combine<-data.table(img_link,title_link,price,ranking_movement)
        setnames(combine,c("图像","书名","价格","销售变动"))
        Sys.sleep(5)
        combine
}
transfer_html_table<-function(rawdata){
        title<-paste0("<th>",names(rawdata),"</th>")
        content<-sapply(rawdata,function(e)paste0("<td>",e,"</td>"))
        content<-apply(content,1,function(e) paste0(e,collapse = ""))
        content<-paste0("<tr>",content,"</tr>")
        bbb<-c("<table border=1><tr>",title,"</tr>",content,"</table>")
        bbb
}
#做一个循坏开始跑数
final<-data.table()
for (i in 1:10){
        final<-rbind(final,readdata(i))
        print(i)
}
library('shiny')
server<-function(input, output, session) {
output$yi <- renderText({
         HTML(transfer_html_table(rawdata= final %>% filter(价格<=1)))
     })
output$er <- renderText({
         HTML(transfer_html_table(rawdata=final %>% filter(价格>1 & 价格<=2)))
     })
output$san <- renderText({
         HTML(transfer_html_table(rawdata=final %>% filter(价格>2 & 价格<=5)))
     })
output$si <- renderText({
         HTML(transfer_html_table(rawdata=final %>% filter(价格>5)))
     })
}
ui<-fluidPage(
titlePanel('亚马逊Kindle今日特价书总结'),
mainPanel(
  tabsetPanel(
    tabPanel("低于1元特价书",htmlOutput("yi")),
    tabPanel("1-2元特价书",htmlOutput("er")),
    tabPanel("2-5元特价书",htmlOutput("san")),
    tabPanel("5元以上特价书",htmlOutput("si"))
  )
)
)
 
shinyApp(ui=ui,server=server)
Reply all
Reply to author
Forward
0 new messages