Naming variable in R from a text File - java

I am very new to R and am looking for a possible solution for this problem.
Suppose I have a variables.txt file (or any other file for that matter), which contains a list of variable names. EX, Product,
Ingredient,
Label,
Manufacturer,
Marketing,
This text file is generated in java and this file has to be read in R and variable are to be named according to the names in the file.
My example code is :
list(Product=0,Ingredient=0,Label=0,Manufacturer=0,Marketing=0)
which is now manually hard coded.
I need a way to get these names of variables from the variables.txt file and dynamically assign them in R. How can this be done?? is there any config file concept in R so that can also be a way out??

Maybe you can use:
data = read.table("file.txt",header=TRUE, sep=".") ?
The sep is depends on the seperator in the file. It could be comma, tab, space, dot or whatever.
With header=TRUE that means you want to take the original variable name from the file.

If you need the list structure described above you can use any read.table or read.csv command to get the names into R as mthbnd showed above.
Say your file.txt looks like: Product,Ingredient,Label,Manufacturer,Marketing
Read in the file and create a list from it. The Elements will then be filled with logical(0). Then you can easily set all elements to a 0 by using [ ] in order to keep the list structure
vars <- as.list(read.csv(file = "file.txt", header = T))
vars[] <- 0

Related

How to get "sout" shorthand to work in Sublime Text 3?

I am trying to get the "sout" shorthand to work in Sublime Text 3 for Java. In vscode and other editors typing "sout + [tab]" will fill in "System.out.println". When I try this in Sublime Text it instead prints "southPane".
This is something that can be done via a snippet or a completion;
both can do this but which you use depends largely on the complexity of the text you want to insert and how many you have.
The main difference is that a snippet is a XML based format where each file contains a single completion whereas a sublime-completions file is JSON formatted file that can contain many completions at once. Additionally, all snippets are automatically added to the command palette and made available only in files to which they apply.
Thus the XML based snippet is good for larger stretches of code (e.g. blocks) or for any text that needs to contain characters that would be a pain to encode as JSON, whereas the JSON based completions are favored for shorter sequences of text, since you can pack more of them into a file.
To demonstrate a snippet, use Tools > Developer > New Snippet to generate a stub, then replace the stub with this content and save it as a file in the default offered location (your User package) as a sublime-snippet file; the name doesn't matter, but the extension does:
<snippet>
<content><![CDATA[
System.out.println($0);
]]></content>
<tabTrigger>sout</tabTrigger>
<scope>source.java</scope>
</snippet>
This says that in a Java file the abbreviation soutTab will expand out to the text System.out.println(); with the cursor left inside the parenthesis.
Alternately, create a file with the following content and save it in your User package as a sublime-completions file (name doesn't matter, only extension, and you can use Preferences > Browse Packages to find the User package`:
{
"scope": "source.java",
"completions": [
{ "trigger": "sout", "contents": "System.out.println($0);" },
]
}
This does the same as the above example, but the file is smaller, and you can include multiple items in it, say for example by also adding:
{ "trigger": "serr", "contents": "System.err.println($0);" },

How to read all properties into an array with spring?

I know you can use spring to read a single property, and to read a single property that has a list of values into a list. But what about reading all the properties from a file into a list?
I.E.
EDIT: The property file we are reading is litterally just a list of values, no key, like the updated example below:
Property File
queueName1
quename2
queName3
...etc (the file is like 100 lines long hence why its not a list of values with one property name)
and then be able to do something like
//Imaginary Code
#Value("${GET ALL THE LINES}")
List<String> eachLineOfPropertyFile;
If you would want to do it using Spring alone, then separate each of the value using "," in the property file and make use of spring EL.
Your properties file will be like:
property.values=queueName1,quename2,queName3
And with Spring Value Annotation
#Value("#{'${property.values}'.split(',')}")
List<String> eachLineOfPropertyFile;
Can you not use the following
List<String> list = Files.readAllLines(new File("propertiesFile").toPath(), Charset.defaultCharset() );
PS: This is part of Java 7

keeping array list content separately in a properties file in java

I have the below class which is using google guava map as you can see below , now the only query is that in it a list is been created as you can see but i want to keep this list in a properties file seprately since later on many new items will be added and deleted in this list so i want developer to only change the properties file
Below is the code
final Table<String, String, List<String>> values = HashBasedTable.create();
values.put("Tbon", "cy", Lists.newArrayList("cat1","cat12","cat13","cat14"));
values.put("Tbon", "ype", Lists.newArrayList("rat1","rat2","rat3"));
for (Cell<String, String, List<String>> cell: values.cellSet()){
System.out.println(cell.getRowKey()+" "+cell.getColumnKey()+" "+cell.getValue());
}
now as you can see both the array list are hardcoded here "cat1","cat12","cat13","cat14" and "rat1","rat2","rat3" but i want to keep thsese array list in a properties file seprately so that it can be map against row key so the properties file content will be like lets say name of the properties file is abc.properties stored in my local computer C: drive
cy cat1
cy cat12
cy cat13
cy cat14
ype rat1
ype rat2
ype rat3
so please advise if i keep this mappings in a properties filethen how they will be get called in the data structure values
There is plenty of information on how to write property files on your disk on the internet, e.g. here: http://www.mkyong.com/java/java-properties-file-examples/
The default format assumes one value per key so you'll need some splitting and joining. Here's a simple answer to that issue: Multiple values in java.util.Properties

Find specific file

I want capture a specific file name and the easiest way I found was using a class called JavaXT, based on examples of official site (http://www.javaxt.com/javaxt-core/io/Directory/Recursive_Directory_Search) I tried return the result in my console application across
javaxt.io.Directory directory = new javaxt.io.Directory("/temp");
javaxt.io.File[] files;
//Return a list of PDF documents found in the current directory and in any subdirectories
files = directory.getFiles("*.pdf", true);
System.out.println(files);
But the returned value always are strange characters like [Ljavaxt.io.File;#5266db4e
Someone could help me to print the correct file(s) name?
When you try to print an array, what you get is its hashcode. Try this if you want to visualize it:
Integer[] a = { 1, 2, 3 };
System.out.println(a);
the output will be
[Ljava.lang.Integer;#3244331c
If you want to print element by element, you can iterate through the array. In this case, using a for-each:
for (javaxt.io.File f : files)
System.out.println(f);
Note that this will print the String returned by the method toString() of the object.
Your files variable is an array. You need
for(javaxt.io.File f:files) System.out.println(f);
Because files is an array, Java will print the array type and the hex hash code.

Matching filenames with id

I have 1 text file which contains numbers from 1 to 11644. Beside the numbers are the names of the xml files that i have in another folder. I have a total of 8466 xml files. I need to match the filename of all the xml files with the id in the text file and extract the value of the id out. All of the id are in random position. An example would be my first xml file id is 7025, which means it's id is 7025. I'm new to java so i really hope someone would enlighten me thanks.
The data structure for this is a map.
Read in the input file, and add each line to a java.util.HashMap<String, Integer>. The key should be the filename. The value should be the id. Thus, for each line, myMap.put(filename, id). Now, when you want to check the ID of a file, do myMap.get(filename). It will return the Integer ID of the file.

Categories

Resources