user1790894
user1790894

Reputation: 417

reading configuration from text file

I have a txt file which has entries

indexUrl=http://192.168.2.105:9200
jarFilePath = /home/soumy/lib

How can I read this file from R and get the value of jarFilePath ?

I need this to set the .jaddClassPath()... I have problem to copying the jar to classpath because of the difference in slashes in windows and linux

in linux I want to use

.jaddClassPath(dir("target/mavenLib", full.names=TRUE ))

but in windows

.jaddClassPath(dir("target\\mavenLib", full.names=TRUE ))

So thinking to read location of jar from property file !!! If there is anyother alternative please let me know that also

Upvotes: 5

Views: 4281

Answers (2)

arun
arun

Reputation: 11023

As of Sept 2016, CRAN has the package properties.

It handles = in property values correctly (but does not handle spaces after the first = sign).

Example:

Contents of properties file /tmp/my.properties:

host=123.22.22.1
port=798
user=someone
pass=a=b

R code:

install.packages("properties")
library(properties)

myProps <- read.properties("/tmp/my.properties")

Then you can access the properties like myProps$host, etc., In particular, myProps$pass is a=b as expected.

Upvotes: 7

Patrick
Patrick

Reputation: 1631

I do not know whether a package offers a specific interface.

If not, I would first load the data in a data frame using read.table:

myProp <- read.table("path/to/file/filename.txt, header=FALSE, sep="=", row.names=1, strip.white=TRUE, na.strings="NA", stringsAsFactors=FALSE)

sep="=" is obviously the separator, this will nicely separate your property names and values.

row.names=1 says the first column contains your row names, so you can index your data properties this way to retrieve each property you want. For instance: myProp["jarFilePath", 2] will return "/home/soumy/lib".

strip.white=TRUE will strip leading and trailing spaces you probably don't care about.

One could conveniently convert the loaded data frame into a named vector for a cleaner way to retrieve the property values: myPropVec <- setNames(myProp[[2]], myProp[[1]]).

Then to retrieve a property value from its name: myPropVec["jarFilePath"] will return "/home/soumy/lib" as well.

Upvotes: 4

Related Questions