haohao Tsing
2011-Nov-17 04:45 UTC
[R] how to read a free text file into individual variables
hi ,I am writing a soft package based on R. But when I try to read a configure file showed as below. How can I read the parameter in this text file. How can I read the parameter into each variable in this file ? configinfo<-scan(file(configfile),ok=TRUE,n=-1) scan seems need every line have same column ? configinfo <- readLines(configfile,ok=TRUE,n=-1) methodnum <- unlist(strsplit(configinfo[2],":"))[2] methodname <- unlist(strsplit(configinfo[3],":")) time <- strtrim((unlist(strsplit(configinfo[4],":"))[2]),3) and time is a string "3 " and strtoi(time) failed and is "[1] NA" when time <- strtoi(strtrim((unlist(strsplit(configinfo[4],":"))[2]),1)) time is a integer "6" actually this parameter could be a 1 to 4 digital number from 1 to 9999 How could I can read this kind of variables into my config R file? thank you ################################################ ##########config file ########################### method=1; # amount of methods method1;method2;method3; #name of the method time=6 #time intv byear=1990 #strar year eyear=2010 #end year title = "pictitle" #title xlab = "xlabinfo" #xlab xxlimmin = 0 #x xxlimmax = 100 #x ylab = "ylabinfo" # yylimmin = 0 # yylimmax = 100 # graph = "phase;discrete;oscillo" # ################################################ [[alternative HTML version deleted]]
Jeff Newmiller
2011-Nov-17 07:32 UTC
[R] how to read a free text file into individual variables
?source
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live
Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.
haohao Tsing <haohaorain at gmail.com> wrote:
>hi ,I am writing a soft package based on R.
>But when I try to read a configure file showed as below.
>How can I read the parameter in this text file.
>How can I read the parameter into each variable in this file ?
>
>configinfo<-scan(file(configfile),ok=TRUE,n=-1)
>scan seems need every line have same column ?
>configinfo <- readLines(configfile,ok=TRUE,n=-1)
>methodnum <- unlist(strsplit(configinfo[2],":"))[2]
>methodname <- unlist(strsplit(configinfo[3],":"))
>time <- strtrim((unlist(strsplit(configinfo[4],":"))[2]),3)
>
>and
>time is a string "3 "
>and
>strtoi(time) failed and is "[1] NA"
>when
>time <-
strtoi(strtrim((unlist(strsplit(configinfo[4],":"))[2]),1))
>time is a integer "6"
>actually this parameter could be a 1 to 4 digital number from 1 to 9999
>How could I can read this kind of variables into my config R file?
>thank you
>
>################################################
>##########config file ###########################
>method=1; # amount of methods
>method1;method2;method3; #name of the method
>time=6 #time intv
>byear=1990 #strar year
>eyear=2010 #end year
>title = "pictitle" #title
>xlab = "xlabinfo" #xlab
>xxlimmin = 0 #x
>xxlimmax = 100 #x
>ylab = "ylabinfo" #
>yylimmin = 0 #
>yylimmax = 100 #
>graph = "phase;discrete;oscillo" #
>################################################
>
> [[alternative HTML version deleted]]
>
>______________________________________________
>R-help at r-project.org mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.
Reasonably Related Threads
- how to read a freetext line ?
- reformat time from hhmm
- How to evaluate a Variable Name?
- Function for trim blanks from a string(s)?
- Multidomain / IP Address Setup (Dovevot 2.2.10 on CentOS7 ) is failing: Fatal: Couldn't parse private ssl_key: error:0906D06C:PEM