R pagedown/shinyapps error - Google Chrome cannot be found - r

I've been trying to use the pagedown package to create a pdf from a html which is better formatted and more presentable from a shinyapp. It tested fine locally on my machine. After deployment, the document failed to download with the following error from the logs:
Warning: Error in find_chrome: Cannot find Chromium or Google Chrome
[No stack trace available]
I am using Google Chrome to view the app but I am guessing there's more to it than this?
The code for the download is as follows:
output$downloadDrug <- downloadHandler(
filename = function() {("drug-instructions.pdf)},
content = function(file) {
src <- normalizePath('report_drugHTML.Rmd')
src2 <- normalizePath("printout.css")
label <- normalizePath("pxLabel.png")
logo <-normalizePath("logo.png")
fasting <- normalizePath("fasting.png")
owd <- setwd(tempdir())
on.exit(setwd(owd))
file.copy(src, 'report_drugHTML.Rmd', overwrite = TRUE)
file.copy(src2, "printout.css", overwrite = TRUE)
file.copy(label, "pxLabel.png", overwrite = TRUE)
file.copy(logo, "logo.png", overwrite = TRUE)
file.copy(fasting, "fasting.png", overwrite = TRUE)
library(rmarkdown)
out <- render('report_drugHTML.Rmd',
params = list(name = input$px_name, dob = input$dob),
'html_document')
library(pagedown)
out <- pagedown::chrome_print(out, "drug-instructions.pdf")
file.rename(out, file)
}
)
Any suggestions would be greatly appreciated. I am still a novice so please feel free to point out the obvious.

Related

Generate report from shiny app with markdown : file not found

I want to generate a pdf report from my shiny app on shiny server. It work well on my computer.
I try to put in on my shiny server, but when i want to generate the report, I get a firefox "file not found" page instead of getting the pdf.
I use the code shown at : https://shiny.rstudio.com/articles/generating-reports.html
I also try to use directly my .Rmd file instead of copy/paste it to the temp dir, but I got the same error.
My server file :
output$pdfGen <- downloadHandler(
# For PDF output, change this to "report.pdf"
filename = "rapport_preci.pdf",
content = function(file) {
withProgress(message = "Génération du pdf en cours", value = 0,{
src <- normalizePath('report.Rmd')
# temporarily switch to the temp dir, in case you do not have write
# permission to the current working directory
owd <- setwd(tempdir())
on.exit(setwd(owd))
file.copy(src, 'report.Rmd', overwrite = TRUE)
# Set up parameters to pass to Rmd document
params <- list(varSite = input$pdfSite,
...
varTrans= input$valTrans1
)
# Knit the document, passing in the `params` list, and eval it in a
# child of the global environment (this isolates the code in the document
# from the code in this app).
out <- render("report.Rmd", output_file = file,
params = params,
encoding = "UTF-8"
)
file.rename(out, file)
})
}
)
I think my app does not found my .Rmd file, but how do I fix that ? All my files are in the same folder.
I just run part of the code, and apparently the problem is when you set owd <- setwd(tempdir())
Here is what that does:
> tempdir()
[1] "/tmp/RtmpKafhlt"
> owd = setwd(tempdir())
> owd
[1] "/home/med"
and you probably don't have access to the home dir of the host server. You need to set replace on.exit(setwd(owd)) with on.exit(owd)

Copy-to-clipboard and downloadhandler not working when Shiny App deployed on Ubuntu server

I developed my Shiny apps in Windows. All features (including copy-to-clipboard and download buttons) work as expected.
However, when I deploy it in our AWS Server (ubuntu), the copy-to-clipboard and download buttons fail.
Any ideas? Thanks!
shiny::observeEvent(input$copy_cross, {
clipr::write_clip(eval(parse(text = "x = as.data.frame.matrix(table(sel()[,1],sel()[,2]))")), row.names= T, col.names = NA)
})
output$download_cross <- shiny::downloadHandler(
filename = function() {paste("Crosstabs (Counts).xlsx")},
content = function(file) {
openxlsx::write.xlsx(x = as.data.frame.matrix(table(sel()[,1],sel()[,2])), file, sheetName = "Crosstabs", row.names= T, col.names = T, append=F)
}
)
I also tried using xclip as suggested in this article
How to write to clipboard on Ubuntu/Linux in R?. However, it's also not working.

Download.File Issue with xls Excel Workbook

I'm trying to download an Excel workbook xls using R's download.file function (Windows 10, R version 3.4.4 (2018-03-15)).
When I download the file manually (using Internet Explorer or Chrome) then the file downloads and I can then open it in Excel without any problems.
When I use download.file in R, the file downloads but size is smaller than correct download file - this file is hmtl file with some notes that my browser is not supported. Tyred different modes and no luck.
My code:
download.file(
url = "https://www.atsenergo.ru/nreport?fid=696C3DB7A3F6019EE053AC103C8C8733",
destfile = "C:/MyExcel.xls",
mode = "wb",
method = "auto"
)
Solving this problem with RSelenium library. ATS site reject any query for downloading file (return .hmtl file with Required javascript enabled message) and in this case Selenium method only works. My code below (where urlList data frame with files download links):
rD <- rsDriver(port = 4444L,
browser = "chrome",
check = FALSE,
geckover = NULL,
iedrver = NULL,
phantomver = NULL)
remDr <- rD$client
for (i in 1:nrow(urlList)) {
tryCatch({
row <- urlList[i,]
remDr$navigate(row$url)
webElem <-
remDr$findElement(using =
'link text', row$FileName)
webElem$clickElement()
},
error = function(e)
logerror(paste(
substr(e, 1, 50),
atsCode,
dateFileName,
sep = "\t"
), logger = loggerName),
finally = next)
}
remDr$close()
# stop the selenium server
rD[["server"]]$stop()

Error in download.file : scheme not supported

I need to download some csv files from "http://www.elections.state.md.us".
And here is my code.
url <- "http://www.elections.state.md.us/elections/2012/election_data/index.html"
# recognize the links
links <- getHTMLLinks(url)
filenames <- links[str_detect(links,"_General.csv")]
filenames_list <- as.list(filenames)
filenames
# create a function
downloadcsv <- function(filename,baseurl,folder){
dir.create(folder,showWarnings = FALSE)
fileurl <- str_c(baseurl,filename)
if(!file.exists(str_c(folder,"/",filename))){
download.file(fileurl,
destfile = str_c(folder,"/",filename))
# 1 sec delay between files
Sys.sleep(1)
}
}
library(plyr)
l_ply(filenames_list,downloadcsv,
baseurl = "www.elections.state.md.us/elections/2012/election_data/",
folder = "elec12_maryland")
The error comes out as :
Error in download.file(fileurl, destfile = str_c(folder, "/",
filename)) : scheme not supported in URL 'www.elections.state.md.us/elections/2012/election_data/State_Congressional_Districts_2012_General.csv'
However, when I try to paste the url into the IE and it did work. So what is the problem of my code?
Any idea would be helpful,Thx.
It turns out that the url must start with a scheme such as http://, https://, ftp:// or file://. So in the last line, I changed the code to
l_ply(filenames_list,downloadcsv,
baseurl = "http://www.elections.state.md.us/elections/2012/election_data/",
folder = "elec12_maryland")
And it works.

Specify download folder in RSelenium

I am using RSelenium to navigate towards a webpage which contains a button to download a file. I use RSelenium to click this button which downloads the file. However, the files are by default downloaded in my folder 'downloads', whereas I want to file to be downloaded in my working directory. I tried specifying a chrome profile as below but this did not seem to do the job:
wd <- getwd()
cprof <- getChromeProfile(wd, "Profile 1")
remDr <- remoteDriver(browserName= "chrome", extraCapabilities = cprof)
The file is still downloaded in the folder 'downloads', rather than my working directory. How can this be solved?
The solution involves setting the appropriate chromeOptions outlined at https://sites.google.com/a/chromium.org/chromedriver/capabilities . Here is an example on a windows 10 box:
library(RSelenium)
eCaps <- list(
chromeOptions =
list(prefs = list(
"profile.default_content_settings.popups" = 0L,
"download.prompt_for_download" = FALSE,
"download.default_directory" = "C:/temp/chromeDL"
)
)
)
rD <- rsDriver(extraCapabilities = eCaps)
remDr <- rD$client
remDr$navigate("http://www.colorado.edu/conflict/peace/download/")
firstzip <- remDr$findElement("xpath", "//a[contains(#href, 'zip')]")
firstzip$clickElement()
> list.files("C:/temp/chromeDL")
[1] "peace.zip"
I've been trying the alternatives, and it seems that #Bharath's first comment about giving up on fiddling with the prefs (it doesn't seem possible to do that) and instead moving the file from the default download folder to the desired folder is the way to go. The trick to making this a portable solution is finding where the default download directory is—of course it varies by os (which you can get like so)—and you need to find the user's username too:
desired_dir <- "~/Desktop/cool_downloads"
file_name <- "whatever_I_downloaded.zip"
# build path to chrome's default download directory
if (Sys.info()[["sysname"]]=="Linux") {
default_dir <- file.path("home", Sys.info()[["user"]], "Downloads")
} else {
default_dir <- file.path("", "Users", Sys.info()[["user"]], "Downloads")
}
# move the file to the desired directory
file.rename(file.path(default_dir, file_name), file.path(desired_dir, file_name))
Look this alternative way.
Your download folder should be empty.
# List the files inside the folder
down.list <- list.files(path = "E:/Downloads/",all.files = T,recursive = F)
# Move all files to specific folder
file.rename(from = paste0("E:/Downloads/",down.list),to = paste0("E:/1/scrape/",down.list))

Resources