R:Profvis没有显示任何结果

时间:2017-09-10 19:23:36

标签: r for-loop

我需要修复我的代码才能更快。我已经阅读了,并且我已经了解了profvis包。

问题在于它在练习中有效,但与我的真实代码无关。

这是我正在谈论的例外代码:

library("profvis")
profvis({
  data(movies, package = "ggplot2movies") # Load data
  movies = movies[movies$Comedy == 1,]
  plot(movies$year, movies$rating)
  model = loess(rating ~ year, data = movies) # loess regression line
  j = order(movies$year)
  lines(movies$year[j], model$fitted[j]) # Add line to the plot
})

这是我的代码(我需要优化的代码): 但profvis仅显示空白屏幕。

library("profvis")
library(RSelenium)
library(rvest)
library(dplyr)
library(stringr)
library(urltools)




#start RSelenium


rD  <- rsDriver(port = 4506L, browser = "firefox", version = "latest", chromever = "latest",
                geckover = "latest", iedrver = NULL, phantomver = "2.1.1",
                verbose = TRUE, check = TRUE)



remDr <- rD[["client"]]




### URLS ###



falabella_urls <- c("http://www.falabella.com.pe/falabella-pe/category/cat7230497/Accesorios-Hombre?No=0&Nrpp=1000",
                    "http://www.falabella.com.pe/falabella-pe/category/cat7230497/Accesorios-Hombre?No=1000&Nrpp=1000")


#############################


testaaa <- function() {


falabella_data_list <- list()




for (i in falabella_urls$url[1:2]) {

  remDr$navigate(i)

  print(i)

  Sys.sleep(05)

  page_source<-remDr$getPageSource()




  product_info <- function(node){


    subcategoria_url <- str_split(path(i), "\\/")[[1]][4]
    s.marca <- html_nodes(node,"div.marca a") %>% html_text
    s.producto <- html_nodes(node,"div.detalle a") %>% html_attr("href")
    s.precio.antes <- html_nodes(node, "div.precio2 span") %>% html_text
    s.precio.actual <- html_nodes(node, "div.precio1 span") %>% html_text 

    data.frame(
      fecha = as.character(Sys.Date()),
      subcategoria = subcategoria_url,
      ecommerce = "Falabella",
      marca = s.marca,
      producto = s.producto,
      precio.antes = ifelse(length(s.precio.antes) == 0, NA, s.precio.antes),
      precio.actual = ifelse(length(s.precio.actual) == 0, NA, s.precio.actual),
      stringsAsFactors=F
    )


  }



  doc <- read_html(iconv(page_source[[1]]), to="UTF-8") %>% 
    html_nodes(".cajaLP4x")




  productos <- lapply(doc, product_info) %>%
    bind_rows()


  falabella_data_list[[i]] <- productos # add it to your list




}





falabella = do.call(rbind, falabella_data_list)

}


profvis({testaaa()})

2 个答案:

答案 0 :(得分:0)

我有同样的问题,您可以尝试将所有代码放入profvis({...})

profvis({
library("profvis")
library(RSelenium)
library(rvest)
library(dplyr)
library(stringr)
library(urltools)




#start RSelenium


rD  <- rsDriver(port = 4506L, browser = "firefox", version = "latest", chromever = "latest",
                geckover = "latest", iedrver = NULL, phantomver = "2.1.1",
                verbose = TRUE, check = TRUE)



remDr <- rD[["client"]]




### URLS ###



falabella_urls <- c("http://www.falabella.com.pe/falabella-pe/category/cat7230497/Accesorios-Hombre?No=0&Nrpp=1000",
                    "http://www.falabella.com.pe/falabella-pe/category/cat7230497/Accesorios-Hombre?No=1000&Nrpp=1000")


#############################


testaaa <- function() {


falabella_data_list <- list()




for (i in falabella_urls$url[1:2]) {

  remDr$navigate(i)

  print(i)

  Sys.sleep(05)

  page_source<-remDr$getPageSource()




  product_info <- function(node){


    subcategoria_url <- str_split(path(i), "\\/")[[1]][4]
    s.marca <- html_nodes(node,"div.marca a") %>% html_text
    s.producto <- html_nodes(node,"div.detalle a") %>% html_attr("href")
    s.precio.antes <- html_nodes(node, "div.precio2 span") %>% html_text
    s.precio.actual <- html_nodes(node, "div.precio1 span") %>% html_text 

    data.frame(
      fecha = as.character(Sys.Date()),
      subcategoria = subcategoria_url,
      ecommerce = "Falabella",
      marca = s.marca,
      producto = s.producto,
      precio.antes = ifelse(length(s.precio.antes) == 0, NA, s.precio.antes),
      precio.actual = ifelse(length(s.precio.actual) == 0, NA, s.precio.actual),
      stringsAsFactors=F
    )


  }



  doc <- read_html(iconv(page_source[[1]]), to="UTF-8") %>% 
    html_nodes(".cajaLP4x")




  productos <- lapply(doc, product_info) %>%
    bind_rows()


  falabella_data_list[[i]] <- productos # add it to your list




}





falabella = do.call(rbind, falabella_data_list)

}


})

答案 1 :(得分:0)

我有同样的问题, 我找到了一种可以尝试的方法。 拳头:在你的Rstudio中运行这个例子,

p <- profvis({
pause(0.5)}) 
htmlwidgets::saveWidget(p, "title")

并且,您将遇到以下问题: 系统错误(粘贴(shQuote(pandoc_path),&#34; - 版本&#34;),intern = TRUE):   &#39;&#34; C:/用户/管理员/ Anaconda3 /库/斌/ pandoc&#34;&#39;找不到

因此您需要安装pandoc

1https://github.com/jgm/pandoc/releases/tag/2.1.3,just在您的路线中出错信息。

并且您通过profvis({})尝试代码,它可以解决您的问题。enter image description here