Re: [R] how to stop interpretation in system()

2016-07-21 Thread Michael Peng
Yes. I forgot to  escape the escape. Thank you so much.

2016-07-21 16:07 GMT-04:00 Sarah Goslee :

> You could escape the backslash.
>
> system("cmd 'a\\tb'")
>
>
> On Thu, Jul 21, 2016 at 4:00 PM, Michael Peng
>  wrote:
> > Hi,
> >
> > I am trying to use system() to run some command in OS. such as
> >
> > system("cmd 'a\tb')
> >
> > however,  it alway runs
> > cmd 'ab'
> > instead of
> > cmd 'a\tb'
> >
> > How can I prevent system to interpret 'a\tb' to 'ab'?
> >
> >
> > Thanks
>
> --
> Sarah Goslee
> http://www.functionaldiversity.org
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


[R] how to stop interpretation in system()

2016-07-21 Thread Michael Peng
Hi,

I am trying to use system() to run some command in OS. such as

system("cmd 'a\tb')

however,  it alway runs
cmd 'ab'
instead of
cmd 'a\tb'

How can I prevent system to interpret 'a\tb' to 'ab'?


Thanks

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] How to avoid endless loop in shiny

2016-03-10 Thread Michael Peng
Hi Greg,

Isolate may not solve the problem.  For the following code. It runs only
1-2 times and stopped without isolate. If we update the slider with same
value, it will not send a new message.

library("shiny")

ui <- fluidPage(

  titlePanel("Slider Test"),

  sidebarLayout(
sidebarPanel(
  min=2, max=10, value=10),

  sliderInput("sliderB", "B:",
  min = 1, max = 5, value = 5)
  ),

mainPanel(
  plotOutput("plot")
)
  )
)

server <- function(input, output, clientData, session) {

  observeEvent(input$sliderA,{updateSliderInput(session, "sliderB", value =
as.integer(input$sliderA/2))})

  observeEvent(input$sliderB,{updateSliderInput(session, "sliderA", value =
isolate(input$sliderB*2))})
}


shinyApp(server = server, ui = ui)

For example:

set B: 4 -> update A: 8 -> update B: 8 (same, no message, end)

changing of A is tricky
If original A is 10, B is 5.

set A: 9  -> update B: 4 (not same, continue) -> update A: 8 (not same,
continue) -> update B: 4 (same, no message, end)


If original A is 8, B is 4.
set A:9 - > update B: 4(same, no message, end)

isolate doesn't work in this case.

Instead, use the following code:

library("shiny")

ui <- fluidPage(

  titlePanel("Slider Test"),

  sidebarLayout(
sidebarPanel(
  min=2, max=10, value=10),

  sliderInput("sliderB", "B:",
  min = 1, max = 5, value = 5)
  ),

mainPanel(
  plotOutput("plot")
)
  )
)

server <- function(input, output, clientData, session) {

server <- function(input, output, clientData, session) {

  ignoreNext <- ""

  observeEvent(input$sliderA,{
  if (ignoreNext == "A") {
ignoreNext <<- ""
  }
  else{
valB <- as.integer(input$sliderA/2)
if(valB != input$sliderB){
  ignoreNext <<- "B"
  updateSliderInput(session, "sliderB", value = valB)
}
  }
})

  observeEvent(input$sliderB,{
if (ignoreNext == "B") {
  ignoreNext <<- ""
}
else{
  valA <- as.integer(input$sliderA*2)
  if(valA != input$sliderA){
ignoreNext <<- "A"
updateSliderInput(session, "sliderA", value = valA)
  }
}
})
}


shinyApp(server = server, ui = ui)

2016-03-08 18:00 GMT-05:00 Greg Snow <538...@gmail.com>:

> You need to use `isolate` on one of the assignments so that it does
> not register as an update.  Here are a few lines of code from the
> server.R file for an example that I use that has a slider for r
> (correlation) and another slider for r^2 and whenever one is changed,
> I want the other to update:
>
>   observe({
> updateSliderInput(session, 'r',
> value=isolate(ifelse(input$r<0,-1,1))*sqrt(input$r2))
>   })
>
>   observe({
> updateSliderInput(session, 'r2', value=input$r^2)
>   })
>
>
> I did end up in a loop once when I happened to choose just the wrong
> value and the rounding caused a jumping back and forth, but all the
> other times this has worked perfectly without the endless loop.
>
>
> On Tue, Mar 8, 2016 at 12:35 PM, Michael Peng
>  wrote:
> > Hi,
> >
> > I added two sliderInput into the app with package "shiny": sliderA and
> > sliderB. The values in the two sliders are correlated. If I change
> sliderA,
> > I used updateSliderInput to update the value in sliderB. And also If I
> > change sliderB, I used  updateSliderInput to update the value in slideA.
> >
> > The problem is it is an endless loop. How can I use updateSliderInput
> > without sending message to update the other slider.
> >
> > Thank.
> >
> > [[alternative HTML version deleted]]
> >
> > __
> > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > 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.
>
>
>
> --
> Gregory (Greg) L. Snow Ph.D.
> 538...@gmail.com
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


[R] How to avoid endless loop in shiny

2016-03-08 Thread Michael Peng
Hi,

I added two sliderInput into the app with package "shiny": sliderA and
sliderB. The values in the two sliders are correlated. If I change sliderA,
I used updateSliderInput to update the value in sliderB. And also If I
change sliderB, I used  updateSliderInput to update the value in slideA.

The problem is it is an endless loop. How can I use updateSliderInput
without sending message to update the other slider.

Thank.

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] ts time-object indexing question?

2016-02-15 Thread Michael Peng
a.ts[time(a.ts)==1965]

2016-02-15 10:53 GMT-05:00 ce :

>
> Dear all,
>
> I can't find an answer to this simple question:
>
>  a.ts <- ts(1:10, frequency = 1, start = c(1959, 1))
>  > a.ts
> Time Series:
> Start = 1959
> End = 1968
> Frequency = 1
>  [1]  1  2  3  4  5  6  7  8  9 10
>
> Now I want to get let's say value for 1965 , how to get it ?
> > a.ts["1965"]
> [1] NA
>
> doesn't work .
>
> And  how I can get the date of a.ts[5]  ?
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] Cannot change R package name in Windows and cannot install an R package in Mac OS Yosemite

2014-11-11 Thread Michael Peng
I see. Thanks a lot.

Gang

On Tuesday, November 11, 2014, Uwe Ligges 
wrote:

>
>
> On 11.11.2014 23:05, Michael Peng wrote:
>
>> Hi,
>>
>> I am now writing an R package LFSpro. I build it for both Mac OS and
>> Windows.
>>
>> For Windows, the binary package name is LFSpro_1.0.3.zip. It can be
>> installed into R successfully. But when I changed the name to
>> LFSpro_1.0.3.Windows.zip, an error occurred:
>>
>> Error in read.dcf(file.path(pkgname,"DESCRIPTION"), c("Package",
>> "TYPE")):cannot open the connection
>> In addition: Warning message:
>> In read.dcf(file.path(pkgname,"DESCRIPTION") c("Package", "TYPE")) cannot
>> open compressed file 'LFSpro_1.0.3.Windows/DESCRIPTION', probable reason
>> 'No such file or directory'
>>
>> Why we cannot change the package name in windows?
>>
>
> Since the filename is an esential part of the package used by the tools.
>
> Best,
> Uwe Ligges
>
>
>  The R package is build on http://win-builder.r-project.org/. There is no
>> error when built it on http://win-builder.r-project.org/. We installed it
>> in Windows 7 and 8.1. The R version is 3.1.2.
>>
>> For Mac OS, someone tested it on Yosemite, an error occurred:
>>
>> R[1588:958444] plugin com.getdropbox.dropbox.garcon interrupted
>>
>> I googled the error message, it seems that there is an incompatibility
>> between R and Yosemite. But when I tested it by myself on Yosemite, there
>> is no such an error. The user is in Brazil, and he is not very familiar
>> with R. I don't know why this error occurs.
>>
>> Is anyone has any idea why there is this error message. The R package is
>> build in R 3.1.1 and Mac OS X 10.6.8.
>>
>> If anyone want to test the R package, you can download it from
>> http://bioinformatics.mdanderson.org/main/LFSpro
>>
>> Thanks a lot for your help.
>>
>> Best,
>> Gang
>>
>> [[alternative HTML version deleted]]
>>
>> __
>> R-help@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.
>>
>>

[[alternative HTML version deleted]]

__
R-help@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.


[R] Cannot change R package name in Windows and cannot install an R package in Mac OS Yosemite

2014-11-11 Thread Michael Peng
Hi,

I am now writing an R package LFSpro. I build it for both Mac OS and
Windows.

For Windows, the binary package name is LFSpro_1.0.3.zip. It can be
installed into R successfully. But when I changed the name to
LFSpro_1.0.3.Windows.zip, an error occurred:

Error in read.dcf(file.path(pkgname,"DESCRIPTION"), c("Package",
"TYPE")):cannot open the connection
In addition: Warning message:
In read.dcf(file.path(pkgname,"DESCRIPTION") c("Package", "TYPE")) cannot
open compressed file 'LFSpro_1.0.3.Windows/DESCRIPTION', probable reason
'No such file or directory'

Why we cannot change the package name in windows?

The R package is build on http://win-builder.r-project.org/. There is no
error when built it on http://win-builder.r-project.org/. We installed it
in Windows 7 and 8.1. The R version is 3.1.2.

For Mac OS, someone tested it on Yosemite, an error occurred:

R[1588:958444] plugin com.getdropbox.dropbox.garcon interrupted

I googled the error message, it seems that there is an incompatibility
between R and Yosemite. But when I tested it by myself on Yosemite, there
is no such an error. The user is in Brazil, and he is not very familiar
with R. I don't know why this error occurs.

Is anyone has any idea why there is this error message. The R package is
build in R 3.1.1 and Mac OS X 10.6.8.

If anyone want to test the R package, you can download it from
http://bioinformatics.mdanderson.org/main/LFSpro

Thanks a lot for your help.

Best,
Gang

[[alternative HTML version deleted]]

__
R-help@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.


[R] Error when install R package in Windows

2014-11-10 Thread Michael Peng
Dear all,

I am now writing a R library. After I build it in Windows *R CMD INSTALL
--build --compile-both foo.tar.gz*, I get a file foo.zip. foo.tar.gz is the
source file tarball. When I tried to install the package in R (
*install.packages("foo.zip")*, or choose the zip file from menu
package/install packages), an error occurred:

In read.dcf(file.path(pkgname, "DESCRIPTION"), c("Package", "Type")) :
cannot open compressed file ‘/DESCRIPTION', probable reason 'No such
file or directory'

But if I install the package in the console: *R CMD INSTALL foo.zip*, there
is no such problem.

I am working on Windows 7 and R version is 3.1.2.

And there is no problem for Mac OS and Linux.

Thanks,
Gang

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] x axis labelling

2014-07-01 Thread Michael Peng
Did you add xaxt = "n" in the plot function?

Try the following:
plot(x,y, xaxt = "n")
axis(1, at = c(14, 20),labels = c("14h", "20h") )


2014-07-01 12:41 GMT-05:00 Michael Millar :

> Hi,
>
> I am new to R and am trying to create a graph with Time(24hr) along the x
> axis. Rather than start at 01.00, I wanted to start at 14.00.
>
> I tried to use the axis(side=1, at=c(  )) function but it continues to put
> then in numeric order. Is there another way I can add labels to the x axis?
>
> Thank You.
>
> Michael
>
> [[alternative HTML version deleted]]
>
> __
> R-help@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.
>

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] Analysis using repeated measure mixed effect anova

2014-06-29 Thread Michael Peng
For the t-test, you used the two-tailed t-test. It is the default. Here I
think you should set alternative="less" in the t.test function.



2014-06-29 8:41 GMT-05:00 dhs :

> Trying to understand how to analyze my data, sample data follows. I want
> to know if the student scores increased from sem1 to sem2 (semesters), and
> whether the inGroup scores increase more.
> Here’s what I did with sample data:
>
> students <- c("s1”, “s2”, "s3")
> inGroup<- c(T, F, T)
> score   <- c(4, 3, 4,  6, 4, 6)
> time <- as.factor(c("sem1","sem1","sem1", "sem2","sem2","sem2"))
>
> data <- data.frame(students, inGroup, score, time)
>
> #to determine if scores for all students increased over time I did a t.test
> t.test(data[data$time=="sem1","score"],data[data$time=="sem2","score"],
> paired=T)
>
> #to determine if the inGroup had a greater increase I did a mixed effect
> anova
> library("lme4")
> mixedEffect  <- lme(score~time, data=data,random=~ 1 | inGroup, na.action
> = na.exclude); summary(mixedEffect)
>
>
> Is this right?  in the above the t-Test p-value <.05 so there was a
> significant change. the mean of differences was -1.66 so the change was an
> increase?
>
> For the mixed effect the difference was significant, p < 0.5 timesem2  is
> this right? If so how do I know if the inGroup scores increased more?
> [[alternative HTML version deleted]]
>
>
> __
> R-help@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.
>
>

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] merge question

2014-06-29 Thread Michael Peng
you can get a new data frame by
merge(qpiso, qplegit, all.x = TRUE, all.y = TRUE, by = "iso" )
Take the subtraction on the new data frame.




2014-06-29 11:24 GMT-05:00 Dr Eberhard Lisse :

> I have two data frames like so
>
> > qpiso
> iso requests
> 1A1   20
> 2A2  199
> 3AD5
> 4AE  176
> ...
> 189  ZW   82
>
> > qplegit
> iso requests
> 1A2   36
> 2AE4
> 3AM2
> 4AO1
> ...
> 100  ZW3
>
>
> I want to create another dataframe qpspam which contains all pairs
> from pqiso with the values for requests of qplegit being subtracted
> from those which exist in qpiso,
>
> ie
> iso requests
> 1A1   20
> 2A2  163
> 3AD5
> 4AE  172
> ...
> 189  ZW   79
>
> but don't know how to do this, and google isn't too helpful.
>
> As usual, a solution is preferred, but a pointer to where I can
> read this up is equally appreciated :-)-O
>
> el
>
> __
> R-help@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.
>

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] saving a 'get' object in R

2014-06-24 Thread Michael Peng
You can use the following command:

tmp <- get(foo)
save(tmp,file=paste(foo,'.rData',sep=''))

Best,
Mike



2014-06-24 15:35 GMT-05:00 David Stevens :

> R community,
>
> Apologies if this has been answered. The concept I'm looking for is to
> save() an object retrieved using get() for an object
> that resulted from using assign. Something like
>
> save(get(foo),file=paste(foo,'rData',sep=''))
>
> where assign(foo,obj) creates an object named foo with the contents of obj
> assigned. For example, if
>
> x <- data.frame(v1=c(1,2,3,4),v2=c('1','2','3','4'))
> foo = 'my.x'
> assign(foo,x)
> # (... then modify foo as needed)
> save(get(foo),file=paste(foo,'.rData',sep=''))
>
> # though this generates " in save(get(foo), file = paste(foo, ".rData",
> sep = "")) :
> object ‘get(foo)’ not found", whereas
>
> get(foo)
>
> at the command prompt yields the contents of my.x
>
> There's a concept I'm missing here. Can anyone help?
>
> Regards
>
> David Stevens
>
> --
> David K Stevens, P.E., Ph.D.
> Professor and Head, Environmental Engineering
> Civil and Environmental Engineering
> Utah Water Research Laboratory
> 8200 Old Main Hill
> Logan, UT  84322-8200
> 435 797 3229 - voice
> 435 797 1363 - fax
> david.stev...@usu.edu
>
> __
> R-help@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.
>

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] Sample all possible contingency tables both margin fixed

2014-06-24 Thread Michael Peng
David gave a great solution. I think it is better to start from 0 to min(M)
instead of from min(M[c(1,3)]) to avoid negative values in the table. If
the minimum is in M[1] or M[3], a = 1:min(M). Otherwise, d=1:min(M).

Best,
Gang


2014-06-24 13:18 GMT-05:00 David L Carlson :

> Since a 2x2 table with fixed row and column margins has only one degree of
> freedom, it is pretty easy to enumerate them:
>
>AB
> A  acM[3]
> B  bdM[4]
>M[1] M[2]   N
>
> Create a vector of the margins:
> M <- c(a+b, c+d, a+c, b+d)
>
> The number of possible tables is min(M[c(1, 3)])+1 since a cannot be
> larger than the first column margin or the first row margin. The +1
> recognizes that a can be 0.
>
> Given any vector M of marginal values, the 2x2 tables are as follows:
>
> M <- c(8, 9, 9, 8) # For example
> M[1]+M[2]==M[3]+M[4] # Check to make sure these are valid margins
> TRUE
> all.2x2s <- t(sapply(0:min(M[c(1,3)]), function(i) c(a=i, b=M[1] - i,
> c=M[3] - i, d=M[4] - M[1] + i)))
> all.2x2s
>   a b c d
>  [1,] 0 8 9 0
>  [2,] 1 7 8 1
>  [3,] 2 6 7 2
>  [4,] 3 5 6 3
>  [5,] 4 4 5 4
>  [6,] 5 3 4 5
>  [7,] 6 2 3 6
>  [8,] 7 1 2 7
>  [9,] 8 0 1 8
>
> -
> David L Carlson
> Department of Anthropology
> Texas A&M University
> College Station, TX 77840-4352
>
> Original Message-
> From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org]
> On Behalf Of Gabor Grothendieck
> Sent: Tuesday, June 24, 2014 12:07 PM
> To: Tahira Jamil
> Cc: r-help@r-project.org
> Subject: Re: [R] Sample all possible contingency tables both margin fixed
>
> On Tue, Jun 24, 2014 at 10:41 AM, Tahira Jamil  wrote:
> > Hi,
> >
> > I am interested in generating all possible contingency table (2 by 2)
> with fixed row margins and column margins. Can anyone help me.
> >
>
> If the reason you want this is to sample them then r2dtable can do
> that directly.
>
> __
> R-help@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.
>
> __
> R-help@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.
>

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] How to avoid searching variables in global environment

2013-09-12 Thread Michael Peng
I thought '+' was defined in a more basic level. I usually use CPP. R is
very different from CPP in many basic concepts.

M.


2013/9/12 William Dunlap 

> Typo: in
> environment(fun) <- new.env(parent=parent.env())
> the parent.env() should have been emptyenv().  But, as
> I said, you do not want to do that.
>
> Bill Dunlap
> Spotfire, TIBCO Software
> wdunlap tibco.com
>
>
> > -Original Message-
> > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org]
> On Behalf
> > Of William Dunlap
> > Sent: Thursday, September 12, 2013 3:19 PM
> > To: Gang Peng
> > Cc: r-help@r-project.org
> > Subject: Re: [R] How to avoid searching variables in global environment
> >
> > You can do that with
> >environment(fun) <- new.env(parent=parent.env())
> > but then your function, function(b)a+b, will not be able
> > to find the "+" function.
> >
> > Putting this code into a package simplifies things a lot.
> >
> > E.g., I added a file containing the two lines
> >phi <- sum(1/(1:1e6)) - log(1e6) # 0.5772...
> >fun <- function(b) a + b + phi
> > to a package (and added phi and fun to the NAMESPACE file's export
> > command).  Then R CMD check myPackage reported
> >* checking R code for possible problems ... Note
> >fun: no visible binding for global variable 'a'
> > It did not complain about the "+" or "phi", since they are
> > either in the package or in some package required by my
> > package but it did complain about the "a".
> >
> > Furthermore, at runtime, fun will always get phi from the package,
> > even if I happen to have a phi in  my global environment.
> >   > fun(10)
> >   Error in fun(10) : object 'a' not found
> >   > a <- 1000
> >   > fun(10) # uses .GlobalEnv's a
> >   [1] 1010.577
> >   > phi <- 100
> >   > fun(10) # still uses myPackage's phi
> >   [1] 1010.577
> >
> > Bill Dunlap
> > Spotfire, TIBCO Software
> > wdunlap tibco.com
> >
> > From: Gang Peng [mailto:michael.gang.p...@gmail.com]
> > Sent: Thursday, September 12, 2013 2:54 PM
> > To: William Dunlap
> > Cc: Sarah Goslee; r-help@r-project.org
> > Subject: Re: [R] How to avoid searching variables in global environment
> >
> > Hi Bill,
> >
> > Thanks. I think we can define the function in the environment whose
> parent environment
> > is empty environment. But don't know how.
> > Best,
> > Mike
> >
> >
> > 2013/9/12 William Dunlap mailto:wdun...@tibco.com>>
> > If you want to find out if you've forgotten to make 'a' an argument
> > you can use codetools::findGlobals(func) to list the names that don't
> > refer to something already defined in the 'func':
> >   > fun <- function(b) a + b
> >   > library(codetools)
> >   > findGlobals(fun)
> >   [1] "+" "a"
> > You need to filter out things defined in some attached package (like the
> "+"
> > from the base package).  I think that when you run 'R CMD check' on a
> > package this sort of check is made on the functions in the package.
> > findGlobals does not run the function to make its checks, it just looks
> at
> > the function.
> >
> > A kludgy way to get an error message instead of having the function
> silently
> > use something from .GlobalEnv is to make the environment of the function
> > the parent of .GlobalEnv
> >>  a <- 1000
> >> environment(fun) <- parent.env(globalenv())
> >> fun(7)
> >Error in fun(7) : object 'a' not found
> > Since this is a run-time check it will not find problems in branches of
> the
> > code that are not run.  The parent environment of .GlobalEnv changes
> > every time you attach or detach a package.  For production code you will
> > want to use a package - the namespace mechanism and the check command
> > will take care of most of this sort of problem.
> >
> > Bill Dunlap
> > Spotfire, TIBCO Software
> > wdunlap tibco.com
> >
> >
> > > -Original Message-
> > > From: r-help-boun...@r-project.org
> [mailto:r-
> > help-boun...@r-project.org] On
> Behalf
> > > Of Sarah Goslee
> > > Sent: Thursday, September 12, 2013 2:09 PM
> > > To: Gang Peng
> > > Cc: r-help@r-project.org
> > > Subject: Re: [R] How to avoid searching variables in global environment
> > >
> > > Hi,
> > >
> > > You need to specify that a is an argument to the function:
> > >
> > > On Thu, Sep 12, 2013 at 3:56 PM, Gang Peng
> > mailto:michael.gang.p...@gmail.com>> wrote:
> > > > For example:
> > > >
> > > > a <- 1
> > > >
> > > > f <- function(b){
> > > > return(a+b)
> > > > }
> > > >
> > >
> > > f <- function(b, a) {
> > > return(a+b)
> > > }
> > >
> > > > when we call function f(2), r will search the local environment
> first, if
> > > > it cannot find a, it will search global environment, and return 3.
> How to
> > > > avoid r searching the global environment and return an error when we
> call
> > > > this function?
> > >
> > > The function will now give an error if a is not specified.
> > >
> > > Sarah
> >

Re: [R] How to avoid searching variables in global environment

2013-09-12 Thread Michael Peng
I see. Thanks a lot for your help.

M.


2013/9/12 Bert Gunter 

> No No! The parent environment is **not necessarily** the enclosing
> environment. The difference is crucial. R looks for free variables in the
> enclosing, **not** the parent environment (although they are often the
> same). Please read about lexical scoping in R in the "R language
> definition" manual, or in online resources. That will explain why R (and
> other functional programming languages) do things this way. There is a lot
> that you do not understand.
>
> Bill's clever solutions and cautions may also not make sense until you do
> your homework.
>
> Cheers,
> Bert
>
>
> On Thu, Sep 12, 2013 at 2:36 PM, Gang Peng wrote:
>
>> Hi Bert,
>>
>> Thanks for the explanation.
>>
>> In R, it first search the local environment and then the parent
>> environment until the root environment (empty environment). I have a
>> concern, when I write a function, I may write a variable name wrong by
>> typo. But by coincidence, this variable name is defined in the parent's
>> or the grandparent's environment, it is very hard to find this bug (It took
>> me almost a day to find out it).  Just don't know why R do it like this.
>>
>> Thanks,
>> Michael
>>
>>
>>
>> 2013/9/12 Bert Gunter 
>>
>>> Michael and Sarah:
>>>
>>> 1. Actually the original claim -- that R will search the global
>>> environment if it does not find a free variable in the function environment
>>> -- is not strictly true. It will search the function's enclosure and then
>>> on up the tree of enclosures. In this case, the enclosure was the global
>>> environment, but that is not always the case.
>>>
>>> 2. Sarah has answered one interpretation of your question. Another might
>>> be -- how can you do things to throw an error when free variables are
>>> encountered in a function. A qualified answer -- qualified, because there
>>> are probably some clever ways to set this up that I can't and won't try to
>>> think of -- is that you can't: you are defeating R's functional programming
>>> paradigm by requesting such behavior. Or to put it another way: don't
>>> do/expect this. Follow Sarah's recommendation instead.
>>>
>>> Cheers,
>>> Bert
>>>
>>>
>>> On Thu, Sep 12, 2013 at 2:08 PM, Sarah Goslee wrote:
>>>
 Hi,

 You need to specify that a is an argument to the function:

 On Thu, Sep 12, 2013 at 3:56 PM, Gang Peng 
 wrote:
 > For example:
 >
 > a <- 1
 >
 > f <- function(b){
 > return(a+b)
 > }
 >

 f <- function(b, a) {
 return(a+b)
 }

 > when we call function f(2), r will search the local environment
 first, if
 > it cannot find a, it will search global environment, and return 3.
 How to
 > avoid r searching the global environment and return an error when we
 call
 > this function?

 The function will now give an error if a is not specified.

 Sarah

 --
 Sarah Goslee
 http://www.functionaldiversity.org

 __
 R-help@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.

>>>
>>>
>>>
>>> --
>>>
>>> Bert Gunter
>>> Genentech Nonclinical Biostatistics
>>>
>>> Internal Contact Info:
>>> Phone: 467-7374
>>> Website:
>>>
>>> http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
>>>
>>>
>>
>>
>
>
> --
>
> Bert Gunter
> Genentech Nonclinical Biostatistics
>
> Internal Contact Info:
> Phone: 467-7374
> Website:
>
> http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
>
>

[[alternative HTML version deleted]]

__
R-help@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.