Connection to Google Spreadsheets not opening in Shiny Server

孤街醉人 提交于 2019-12-23 06:21:13

问题


I have been trying to use Shiny to read and analyze new format Google spreadsheets using the functions here.

This works fine locally but not when I deploy to shinyapps.io.

One issue was the known issue of false positives on the test for absolute paths but that is solved by forcing deployment in the console. However once uploaded I'm getting the javascript dump alert with this error

label, label<-Error in file(con, "r") : cannot open the connection

I've looked around and I can see that people get the javascript error dump for a lot of reasons, and the closest I saw was where someone needed to use RCurl, but that didn't help me. I also know that I have https in the url so that is not the issue. The spreadsheet I am reading is definitely published to the web; anything obvious like that would have made it not work locally.

Update: I'll copy the code for the functions here.

library(XML)

cleanGoogleTable <- function(dat, table=1, skip=0,

ncols=NA, nrows=-1, header=TRUE, dropFirstCol=NA){  

if(!is.data.frame(dat)) {

    dat <- dat[[table]]   }   if(is.na(dropFirstCol)) {
    firstCol <- na.omit(dat[[1]])
    if(all(firstCol == ".") || all(firstCol== as.character(seq_along(firstCol)))) {

      dat <- dat[, -1]
    }   } else if(dropFirstCol) {
    dat <- dat[, -1]   }   if(skip > 0){
    dat <- dat[-seq_len(skip), ]   }   if(nrow(dat) == 1) return(dat)   if(nrow(dat) >= 2){
    if(all(is.na(dat[2, ]))) dat <- dat[-2, ]   }   if(header && nrow(dat) > 1){
    header <- as.character(dat[1, ])
    names(dat) <- header
    dat <- dat[-1, ]   }   # Keep only desired columns   if(!is.na(ncols)){

    ncols <- min(ncols, ncol(dat))
    dat <- dat[, seq_len(ncols)]   }   # Keep only desired rows   if(nrows > 0){
    nrows <- min(nrows, nrow(dat))
    dat <- dat[seq_len(nrows), ]   }   # Rename rows   rownames(dat) <- seq_len(nrow(dat))   dat }

and

readGoogleSheet <- function(url, na.string="", header=TRUE){
  stopifnot(require(XML))
  # Suppress warnings because Google docs seems to have incomplete final line
  suppressWarnings({
    doc <- paste(readLines(url), collapse=" ")
  })
  if(nchar(doc) == 0) stop("No content found")
  htmlTable <- gsub("^.*?(<table.*</table).*$", "\\1>", doc)
  ret <- readHTMLTable(htmlTable, header=header, stringsAsFactors=FALSE, as.data.frame=TRUE)
  lapply(ret, function(x){ x[ x == na.string] <- NA; x})
}

回答1:


The problem is in the readGoogleSheet() function which uses readLines() which calls url() which only supports https on Windows, which is what I'm on locally. Google, of course, is end to end https.

To get it to run I made this change

 +doc <- content(GET(url), as="text")
 -doc <- paste(readLines(url), collapse=" ")

and that seemed to work. I got the general idea for this from @hrbmstr here



来源:https://stackoverflow.com/questions/25685712/connection-to-google-spreadsheets-not-opening-in-shiny-server

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!