r - 应用程序无法启动 - 无法找到功能" setInternet2"

时间:2016-03-17 18:37:06

标签: r shiny

我遇到了一些问题,每当我尝试部署闪亮的应用程序时,我收到此消息:值[3L]出错:找不到函数setInternet2

通话:本地... tryCatch ......

执行暂停

setInternet2

代码

###  install packages
library(doParallel)
library(devtools)
library(lattice)
library(shiny)
library(stringr)
library(lubridate)
library(shinyBS)
library(doSNOW)

library(XML)
library(httr)
library(RCurl)
library(wordcloud)
library(tm)
library(rJava)
library(qdap)
library(slam)

runApp(list(
  server = shinyServer(function(input, output, session){
    observeEvent(input$getNews, {
      output$News <- renderDataTable({
        # Create a Progress object
        progress <- shiny::Progress$new(session, min=1, max=15)
        progress$set(message = "(╯ ̄▽ ̄)╯ Loading...", value = 0)

        # Close the progress when this reactive exits (even if there's an error)
        on.exit(progress$close())

        data <- list()
        tmp <- paste('.html', sep='')
        url <- paste('https://www.ptt.cc/bbs/Stock/index', tmp, sep='')
        html <- httr:::content(GET(url), encoding = "UTF-8") # xml2
        html <- XML::xmlParse(html) # parse from xml2 to xml
        url.list <- xpathSApply(html, "//div[@class='title']/a[@href]", xmlAttrs)

        data <- rbind(data, paste('https://www.ptt.cc', url.list, sep=''))
        data <- unlist(data)

        #  cl = makeCluster(rep('localhost', 8), 'SOCK')
        #  clusterSetupRNG(cl)
        #  clusterEvalQ(cl, source('R/R/GET.R'))
        getDoc <- function(line){

          start <- regexpr('www', line)[1]
          end <- regexpr('html', line)[1]

          if(start != -1 & end != -1){

            url <- substr(line, start, end + 3)
            name <- strsplit(url, '/')[[1]][4]
            txtName <- gsub('html', 'txt', name)
            if(!file.exists(paste0("document/news/", txtName))){
              #    html <- httr:::content(GET(url, config = set_cookies("over18"="1")), encoding="UTF-8")
              html <- httr:::content(GET(url), encoding = "UTF-8")
              html <- XML::xmlParse(html)
              doc <- xpathSApply(html, "//div[@id='main-content']", xmlValue)
              #write(doc, paste0("document/news/", gsub('html', 'txt', name)),
              #     encoding = "UTF-8")
              writeLines(as.character(doc), paste0("document/news/", gsub('html', 'txt', name)), 
                         useBytes=T)
            }
          }
        }
        #  parSapply(cl, data, getDoc)
        #  stopCluster(cl)
        sapply(data, getDoc)

        cl <- makeCluster(4, type = "SOCK")
        doSNOW:::registerDoSNOW(cl)
        articles <- 
          foreach(i = 1:length(list.files("document/news/")), .combine = 'c') %dopar% {
            readLines(paste0("document/news/", list.files("document/news/")[i]), encoding = "UTF-8")[1]
          }
        stopCluster(cl)

        start = regexpr("新聞", articles)
        end = regexpr("2016", articles)

        news <- substr(articles, start = start - 1, stop = end + 3)[start != -1]
        news
        news2 <- substr(news, start = 1, stop = regexpr("時間", news) - 1)

        # for messages
        Sys.setenv(LANG = "Zh_TW")
        Sys.setlocale("LC_ALL", "cht")

        start = regexpr("時間", news)
        end = regexpr("2016", news)
        newsDate = substr(news, start = start + 6, stop = end + 3)
        newsDate

        Sys.setenv(LANG = "en")
        Sys.setlocale("LC_ALL", "English")
        newsDate = strptime(newsDate, format = "%b %d %H:%M:%S %Y")
        newsDate = as.POSIXct(newsDate)
        #newsDate = as.Date(newsDate, format = "%b %d %Y")

        Sys.setenv(LANG = "Zh_TW")
        Sys.setlocale("LC_ALL", "cht")
        newsDF <- data.frame(Date = newsDate, Event = news2)

        news <- newsDF
        news <- news[order(news$Date, decreasing = TRUE), ]
        news[as.Date(news$Date, format = "%b %d %Y") >= input$dateRange[1] & 
               as.Date(news$Date, format = "%b %d %Y") <= input$dateRange[2], ]
      })
    })

  }),
  ui = shinyUI(tagList(
  tags$head(tags$script(HTML("Shiny.addCustomMessageHandler('closeWindow', function(m) {window.close();});"))),
  navbarPage(
    "MynavbarPage", inverse = TRUE, id = "navbar",
    tabPanel("News", 
             sidebarLayout(
               sidebarPanel(
                 width = 3,
                 bsTooltip("stocks", title = "Please enter stock code from yahoo finance.", placement = "bottom", trigger = "hover", options = NULL),
                 textInput("stocks", label = "Stock Code", value = "2330.TW"),
                 bsTooltip("dateRange", title = "This is a time period you apply WFA to.", placement = "bottom", trigger = "hover", options = NULL),
                 dateRangeInput("dateRange", label = "Choose Period", start = "2015-01-01", end = Sys.Date()),
                 actionButton("getNews", "Start", class="btn-primary btn-lg")
               ),
               mainPanel(
                 bootstrapPage(
                   dataTableOutput("News"))
               )
             ))
  )))
))

但我可以在本地运行我的应用程序。我想知道它是否与我的本地路径设置有关?我的应用程序位于一个文件夹中,例如&#34; MyAPP&#34;其中包括ui.R和server.R。我还有另一个名为&#34的文件夹;文件&#34;在MyAPP内部。我使用了很多list.file(&#34; document /&#34;)。

我在我的应用中没有包含setInternet2功能,但我使用的是httr,XML,tm等Web API。

有什么建议吗?

非常感谢。

1 个答案:

答案 0 :(得分:0)

以下命令似乎连接到互联网并下载数据。在内部,它正在设置互联网选项

html <- httr:::content(GET(url), encoding = "UTF-8") # xml2

您可以尝试以下方法来帮助您调试

  1. 不是从互联网上下载HTMl,而是从文件中读取
  2. 如果它有效,那么我们肯定知道从互联网上获取数据的代码是问题
  3. 此致 了Anant