R - Connect to text file without opening in text editor - r

I need to repeatedly extract contents from a large number of text files with addresses given by URLs. Every time I run my R code and connect to a file it opens in Notepad++ which is my default text editor. The R code alone works perfectly well but in the text editor I have a "Reload" dialog window for each of 100+ files. How can I avoid this?
I use url.show function to extract the text and file function to connect and read the data.
url.show('https://www.playok.com/p/?g=gm136365880&txt', file='game.txt')
con = file("game.txt", open = "r")
### some code: extracting lines of text and processing...
close(con)
Probably these are not the proper functions for my particular task. What I need is just a connection to the file contents without opening the file anywhere (including my RStudio window and the text editor window).

If you don't need a local copy of the file, you can just pass the URL to open:
con <- file("https://www.playok.com/p/?g=gm136365880&txt", open = "r")
readLines(con)
close(con)
will print the content of the URL

Related

Why can't I view or open downloaded PNG file from URL

I have downloaded an image (in fact several images using a for loop) using the below code. However, these images are not opening up, though they seem to have got downloaded completely. In fact these images are not opening up in plain Photo editor or Paint etc., tools. Appreciate inputs and what shall be done..
Below is the code that I tried with for loop:
p <- c("http://assets.pokemon.com/assets/cms2/img/pokedex/full/001.png",
"http://assets.pokemon.com/assets/cms2/img/pokedex/full/002.png",
"http://assets.pokemon.com/assets/cms2/img/pokedex/full/003.png",
"http://assets.pokemon.com/assets/cms2/img/pokedex/full/003_f2.png",
"http://assets.pokemon.com/assets/cms2/img/pokedex/full/004.png")
p
for (url in p)
download.file(url, destfile=file.path("C:/Users/xyz/Desktop/test",basename(url)))
library(imager)
# loading only first image for viewing
i <- load.image("C:/Users/xyz/Desktop/test/001.png")
plot(i)
Then I just downloaded a single file giving a simple destination name and tried to load and display using the below code.
download.file("http://assets.pokemon.com/assets/cms2/img/pokedex/full/001.png",
destfile=file.path("C:/Users/xyz/Desktop/test","first_img.png"))
i_s <- load.image("C:/Users/xyz/Desktop/test/first_img.png")
plot(i_s)
In both the cases I am getting the below error message.
Error in read.bitmap(file) :
File f: C:/Users/xyz/Desktop/test/001.png does not appear to be a PNG, BMP, JPEG, or TIFF
Same way, if I try to open the downloaded images using Photos, Photos editor, Adobe, Paint etc., I get similar messages like format not supported, unable to load photo, etc., messages. However, note that if I simply copy and paste the image url in the browser, the image appears perfectly in the web page.
Appreciate inputs on what can be done here.
Loos like you have to set mode = "wb" in download.file. The manual says:
The choice of binary transfer (‘mode = "wb"’ or ‘"ab"’) is important on Windows, since unlike Unix-alikes it does distinguish between text and binary files and for text transfers changes ‘\n’ line endings to ‘\r\n’ (aka ‘CRLF’).
On Windows, if ‘mode’ is not supplied (‘missing()’) and ‘url’ ends in one of ‘.gz’, ‘.bz2’, ‘.xz’, ‘.tgz’, ‘.zip’, ‘.jar’, ‘.rda’, ‘.rds’ or ‘.RData’, ‘mode = "wb"’ is set so that a binary transfer is done to help unwary users.
So for the single file try:
download.file("http://assets.pokemon.com/assets/cms2/img/pokedex/full/001.png",
destfile=file.path("C:/Users/vsvas/Desktop/test","first_img.png"),
mode = "wb")

How can i convert a .xlsx (with an image on header) to PDF on Unix?

I have a .xlsx file with an image on header. When i try to convert to PDF via terminal using libreoffice it converts but the image doesn't appear on the generated PDF.
Here is a screenshot of the main .xlsx file on Excel (print mode):
And the Screenshot of the generated PDF:
The command that im using is: soffice --headless --convert-to pdf --outdir /home/output /home/input/headers_footers.xlsx
Note: To display the header image of the .xlsx on excel we need to put it into print view mode (ctrl+p)
I tested the conversion of the headers_footers.xlsx with an online convertion tool to PDF and the final result was as expected: the image appeared.
But i need to do this programatically, so im not sure if its libreoffice or if i need to use other tool, or maybe manipulate the final PDF and add an image on it?
This is not only an UNIX problem. I can reproduce the malfunction running MS Windows 10. The header image appears in print view and in manually exported PDF. But when you call
soffice --headless --convert-to pdf --outdir . sample.xlsx
on command line it won't show up. I will keep on searching for an answer but this seems to be a bug to me.
Is using a LO Basic Macro an acceptable approach for you, too?
Sub ExportCalcToPDF
sURL = convertToURL("d:\temp\lo_calc.pdf")
dim mFileType(0)
mFileType(0) = createUnoStruct("com.sun.star.beans.PropertyValue")
mFileType(0).Name = "FilterName"
mFileType(0).Value = "calc_pdf_Export"
thisComponent.storeToURL(sURL, mFileType())
End Sub
When executed this macro exports the spreadsheet to pdf. To run the macro automatically e.g. on file open assign it to "Open Document" in Tools > Customize > Events.
Doing a test by double clicking the xlsx file the image appears in the header of the pdf. However, no success from command line so far.

Programmatically open a text file in R?

Is there something in R to open a text file? In Python, one could open a text file using notepad:
import subprocess as sp
programName = "notepad.exe"
fileName = "file.txt"
sp.Popen([programName, fileName])
Other methods are described in the post as well. Is there something similar in R?
Keeping in mind portability issues, i.e. this is specific to Windows, you can use shell.exec, which will open the file in whatever program is associated with that type of file by default - e.g.
## full path
shell.exec(paste0(c(getwd(), "/tmpfile.txt"),collapse = ""))
## relative to current working directory
shell.exec("tmpfile.txt")
both open tmpfile.txt in notepad on my machine.

How do you use special letters (danish) in R-studio?

I am from Denmark and need to be able to use danish letters in my R-code.
If I add some code like:
print("Århus er bare øv...")
and save it as an R-script file in R-studio, the next time I open the file in R-studio I get:
print("?rhus er bare ?v...")
When saving your file, use File -> Save with encoding and select e.g. "UTF-8". If the file is read incorrectly next time, use File -> Reopen with encoding.
You may also change the RStudio preferences (Tools -> Global options / General->Default text encoding) and set "UTF-8".

How do I read in a CSV file into R?

I'm having some technical issues with loading a CSV file into R. When I inspect the csv file in RStudio's Source pane, all the characters are surrounded by weird red circles or dots. When I inspect another self-made CSV file, the characters appear perfectly fine, without any of the red circles.
What is this issue/symptom, and what would be the best way to fixing this for about 40 similar CSV files?
When I try to run readfile <- read.csv("filename.csv", sep="", collapse=NULL) I get the following error:
Error in read.table(file = file, header = header, sep = sep, quote = quote, :
empty beginning of file
My guess would be that you ran into some encoding issue.
Especially on Windows you can run into all sorts of problems with that.
Try opening the csv file with a text editor that has the capability of saving files with various Encodings (e.g. Notpead++) then change that to e.g. UTF-8 (which is the preferred Encoding of RStudio most other Editors and R itself), save the file and try to run the import again.
Just make sure that you don't loose characters - especially special characters tend to get lost during Encoding changes.
Greetings ...

Resources