简体   繁体   English

如何在R闪亮中读取导入的csv文件中的列

[英]How to read a column in a imported csv file in R shiny

I want to import a CSV file from my computer, that includes URLs for web articles to the shiny app.我想从我的计算机导入一个 CSV 文件,其中包含指向闪亮应用程序的网络文章的 URL。 Then I want to get 100 URLs, from the column "url" in the CSV file and web scrap all of those URLs to create a "Word cloud"然后我想从 CSV 文件中的“url”列中获取 100 个 URL,并通过网络抓取所有这些 URL 以创建“词云”

This is the server part of the code.这是代码的服务器部分。 I want to select the column "url" from the CSV file and iterate through the first 10 of "url"s with a for loop in order to web scrap data from all of the articles from which these URLs lead.我想从 CSV 文件中选择“url”列,并使用 for 循环遍历“url”的前 10 个列,以便从这些 URL 所引出的所有文章中收集数据。 Then I assign that data to the variable called "inputWords", then "inputWords" again assign to a variable called "data" in order to create a word cloud.然后我将该数据分配给名为“inputWords”的变量,然后将“inputWords”再次分配给名为“data”的变量以创建词云。

server <- function(input, output) {
data_source <- reactive({
    if (input$source == "csv") {
        data <- inputWords()
    }
    return(data)
})

inputWords <- reactive({
    if (is.null(input$csv)) {
        return("")
    }
    
    else if (is.table(input$csv)) {
        CSVFile <- read.csv(input$csv$datapath)
        Urls <- c(CSVFile$url[1:10])
        
        pages <- list()
        
        for (i in Urls) {
            ArticlePages <- read_html(i)
            
            articleText = ArticlePages %>% html_elements("h1.newsfull__title, p") %>% html_text()
            pages[[i]] <- c(articleText)
        }
        pages[1:10]
    }
})

And this is where I assign "data_source" to the word cloud这就是我将“data_source”分配给词云的地方

output$cloud <- renderWordcloud2({
    create_wordcloud(data_source(),
                     num_words = input$num)
})

This is the warning message:这是警告消息:

Warning: Error in if: argument is of length zero

Link to sample data 链接到示例数据

Please help, And thank you very much in advance again请帮助,再次非常感谢您在此处输入图片说明

The following works with your sample data.以下适用于您的示例数据。 I am not sure what the issue is as you have not shown your ui .我不确定是什么问题,因为你没有显示你的ui

library(rvest)

ui <- fluidPage(
  titlePanel("title panel"),
  fluidRow(
    column(3, fileInput("csv", h3("File input"))
    )
  ),
  fluidRow(
    column(width = 9, verbatimTextOutput("t1"))
  )
)

server <- function(input, output) {
  # data_source <- reactive({
  #   if (input$source == "csv") {
  #     data <- inputWords()
  #   }
  #   return(data)
  # })
  
  inputWords <- reactive({
    req(input$csv)
    CSVFile <- read.csv(input$csv$datapath)
    Urls <- as.character(CSVFile$url[1:10])
    
    pages <- list()
    
    for (i in Urls) {
      ArticlePages <- read_html(i)
      
      articleText = ArticlePages %>% html_elements("h1.newsfull__title, p")  %>% html_text()
      pages[[i]] <- c(articleText)
    }
    pages[1:10]
  })
  
  output$t1 <- renderText({paste(inputWords())})
  
}

shinyApp(ui = ui, server = server)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM