保存循环会在R中产生新的数据框列

时间:2019-06-02 20:37:11

标签: r loops

我尝试将for循环的结果保存在新的列中,但是尽管message()返回了正确的信息,但数据并未保存。

背景: 我有一个数据框,该数据框在第1列中包含XPath,在第2列中包含网站元素的CSS选择器,并希望将getComputedStyle(在RSelenium的帮助下对JS函数进行评估)的结果保存为多种样式,例如``颜色'',``宽度''等。

#Function filling one row with CSS's
get_computed_styles_loop <- function(some_web_element){
  for (style in 1:length(css_styles)) {
    js_function_styles <- paste("return window.getComputedStyle(",some_web_element,").getPropertyValue('",css_styles[style],"')",sep="")
    my_df_col <- columns_number + as.integer(style)
    my_df[selector, my_df_col] <- as.character(remDr$executeScript(js_function_styles)) # <- THIS ONE DOESN'T SAVE DATA TO THE DATAFRAME
    message(paste(js_function_styles, " ", my_df_col , " ", remDr$executeScript(js_function_styles)))
  }
}
#Filling the whole dataframe
for (selector in 1:nrow(my_df){
  js_function_xpath <- paste("document.getElementByXPath('",my_df[selector,1],"')",sep="");  
  js_function_selector <- paste("document.querySelector('",my_df[selector,2],"')",sep="");
      tryCatch({
        get_computed_styles_loop(js_function_xpath)
      },
      error=function(cond){
        message(paste("XPath not working here: ", selector))
        tryCatch({
          get_computed_styles_loop(js_function_selector)
        },
        error=function(cond1){
          message(paste("Neither does CSS Selector: ", selector))
          return(NA)
        }  
        )
      }  
      )
}

示例性message()结果。看起来还可以,但是没有在新列中显示:

return window.getComputedStyle(document.getElementByXPath('html/body/div[1]/div[1]/table')).getPropertyValue('color')   22   rgb(17, 17, 17)
return window.getComputedStyle(document.getElementByXPath('html/body/div[1]/div[1]/table')).getPropertyValue('background-color')   23   rgba(0, 0, 0, 0)
return window.getComputedStyle(document.getElementByXPath('html/body/div[1]/div[1]/table')).getPropertyValue('background')   24   
return window.getComputedStyle(document.getElementByXPath('html/body/div[1]/div[1]/table')).getPropertyValue('margin')   25   
return window.getComputedStyle(document.getElementByXPath('html/body/div[1]/div[1]/table')).getPropertyValue('padding')   26   
return window.getComputedStyle(document.getElementByXPath('html/body/div[1]/div[1]/table')).getPropertyValue('width')   27   400px
return window.getComputedStyle(document.getElementByXPath('html/body/div[1]/div[1]/table')).getPropertyValue('height')   28   32px
return window.getComputedStyle(document.getElementByXPath('html/body/div[1]/div[1]/table')).getPropertyValue('font-weight')   29   400
return window.getComputedStyle(document.getElementByXPath('html/body/div[1]/div[1]/table')).getPropertyValue('font-family')   30   "Open Sans", sans-serif
return window.getComputedStyle(document.getElementByXPath('html/body/div[1]/div[1]/table')).getPropertyValue('font-size')   31   13px
return window.getComputedStyle(document.getElementByXPath('html/body/div[1]/div[1]/table')).getPropertyValue('border')   32   
return window.getComputedStyle(document.getElementByXPath('html/body/div[1]/div[1]/table')).getPropertyValue('border-radius')   33   
return window.getComputedStyle(document.getElementByXPath('html/body/div[1]/div[1]/table')).getPropertyValue('position')   34   static
return window.getComputedStyle(document.getElementByXPath('html/body/div[1]/div[1]/table')).getPropertyValue('text-align')   35   start
return window.getComputedStyle(document.getElementByXPath('html/body/div[1]/div[1]/table')).getPropertyValue('pointer')   36   

0 个答案:

没有答案