R readline, issues with User Input - r

1
Firstly my issue is when sourcing a script, if that contains a readline call, the input in console is attached to the prompt
eg
sourcefile <- readline(prompt="Enter csv file name for FUND DATA, 2nd column price data\n")
in the console I type in equil, it appears as/this is where I'm typing
Inputting looks like
After hitting enter, this then indents with \n. Notice how equil.. is now on the next line. Then for next readline I am again inputting with 0 space between my input and the prompt.
This is super unreadable/awkward. Is it possible to input for readlines on it's own line for readability? ie to have the user typing on the next line like it is after pressing enter
Output then indents

try this:
sourcefile <- readline(prompt=cat("Enter csv file name for FUND DATA, 2nd column price data\n"))

Related

Text writes over itself instead of creating a new line in R session

I am running an R session on the Unix cluster, and whenever I reach a certain amount of characters, instead of starting on the new line, the text starts to write over itself at the beginning. So, for example, it should look like this:
foo <- read.table("/file/buried/deep/within/lots/of/folders/makes/for/a
/long/name.txt")
but instead it looks like this:
ame.txt")read.table("/file/buried/deep/within/lots/of/folders/makes/for/a/long/na
because the text is wrapping around and starting again at the beginning of the line.
I've changed my width parameter in the options() function, but no such luck.
ETA: I am running R on the command line in a Unix environment

What does the "+" symbol mean on the left side of the R console?

I have entered in code to plot a graph, but when I press Enter to execute the graph, it does not plot the graph. Rather, + symbols appear every time I press Enter to execute the command and plot the code.
Now there is a long column of + symbols in my R consoles.
Why is this happening and what can I do to prevent this from happening?
The prompt has + because it signifies that the prompt is expecting more from the line of code, a sort of continuation. This may be because you forgot to close something so the prompt expects the closing side. For example, say you forgot to close a string like so:
> "
+
+
Here, I entered a double-quote into the prompt and kept on pressing Enter. There is a missing double-quote to tell the prompt I've ended the string literal, so the prompt expects another double-quote. Once you enter the double quote the prompt will stop expecting it. For example:
> "
+
+ "
[1] "\n\n"
This is standard on all command prompts, to expect more code if something isn't ended properly, like the string literal above. Check your code to make sure you've closed all opening quotes, symbols, etc, so the prompt doesn't expect it and your code executes correctly.
The ways of exiting the prompt when this happens are:
Esc on RGui and RStudio
Ctrl-C on Terminals and Command Prompts
This could happen if you used space in the middle of an object name, eg. Column names. R doesn't allow space in between " ".

How to remove the extra commas from a csv file?

I was trying to use a csv file in R in read.transactions() command from arules package.
The csv file when opened in Notepad++ shows extra commas for every non-existing values. So, I'm having to manually delete those extra commas before using the csv in read.transactions(). For example, the actual csv file when opened in Notepad++ looks like:
D115,DX06,Slz,,,,
HC,,,,,,
DX06,,,,,,
DX17,PG,,,,,
DX06,RT,Dty,Dtcr,,
I want it to appear like below while sending it into read.transactions():
D115,DX06,Slz
HC
DX06
DX17,PG
DX06,RT,Dty,Dtcr
Is there any way I can make that change in read.transactions() itself, or any other way? But even before that, we don't get to see those extra commas in R(that output I showed was from Notepad++)..
So how can we even remove them in R when we can't see it?
A simple way to create a new file without the trailing commas is:
file_lines <- readLines("input.txt")
writeLines(gsub(",+$", "", file_lines),
"without_commas.txt")
In the gsub command, ",+$" matches one or more (+) commas (,) at the end of a line ($).
Since you're using Notepad++, you could just do the substitution in that program: Search > Replace, replace ,+$ with nothing, Search Mode=Regular Expression.

Reading a string with special character in R

I have a little problem in reading a file in R. In particular, I run a script that load a file, say X, which stores a string per each line. There are string with special characters like, ' and therefore I get some errors.
I run the script by command line as follows
Rscript.exe MyScript.R "C:\X.txt"
The content of file X is, for instance:
I'll win a prize
I'll shutdown my pc
The MyScript.R script initially loads the file X.txt as follows
args <- commandArgs(TRUE)
args <- read.table(args[1], sep="\n")
and then uses it as follows:
print(nrow(args))
The previous line returns 0. However, if I remove the ' character from the two lines in file X.txt then everything works fine (i.e., and the returned length is 2).
Any solution to handle this tricky input?
read.table is meant for reading structured data, ie data that is in the form of multiple fields per row. If you just want to read a bunch of strings, use readLines.
args <- readLines(args[1])

inserting line breaks after every record in the textpad

I have a textpad file that has rows of text. For e.g.
Cat: Meaning - animal. The cat ran up the house
Rat: Meaning- rodent. The rat lives in the borough and feeds on leftovers
Word 3: Description
Word 4: Description
I have many such record in my file. I want to insert a line break at the end of every record for proper presentation. Doing it manually is tedious. Please help if you know an automated process to insert line break.
You can quickly do this by using a feature called "Regular Expressions" to find and add empty lines.
Open up the Find/Replace (Search menu > Replace)
In the "Find what" field, type the following: (^.+$)\n(^.+$)
In the "Replace with" field, type the following: \1\n\n\2
Tick the "Regular expression" checkbox
Click the Replace All button at least twice, but perhaps 3 times, until you get the message Cannot find the Regular Expression
Untick the "Regular expression" checkbox
Close the Replace dialog
Confirm the file is formatted as you are expecting
Save the file.
You can write a simple C# prgram that uses a loop that adds this code after every line :
But first add the namespace using System.Enviorment
Enviorment.NewLine;
If you have any more trouble i'll help with some code to get started
Open up the Find/Replace (Search menu > Replace)
In the "Find what" field, type the following so that the replace occurs at the end of each line: $
In the "Replace with" field, type the following. Note each 'n' represents a <return>. In this instance, I added a return at the end of a SQL statement, the word 'GO' on the next line and another <return>: \n\GO\n
Started with text file containing:
select * from <tablename>
select * from <tablename>
Ended with text file containing:
select * from <tablename
GO
select * from <tablename>
GO
Hope that helps.
from your text it is difficult to understand what you are intending to do. I'll give you some questions. The answers will help others to help you.
Do you really mean textpad as the product from company helios in UK or do you use this word as a general word for a class of tools (like notepad - but there is a general definition AND the tool as part of Windows).
Your file hase line breaks yet. You don't see them, but in the file itself they are present (in Unix systems line feed (hex code 0A) or in the windows world carriage return followed by line feed (hex code 0D 0A)).
Or would you like to publish your text in HTML? So you have to put the necessary tags around each line like paragraph, line break, list item etc.?

Resources