Hi, I am trying to write a function which defines some arguments, then uses those arguments as arguments of other function calls. It's a bit tricky to explain, so a simple example will have to suffice. I imagine this has a simple solution, but perusing through environments and other help lists has not helped. Suppose I have two functions: f1 = function(a) { b = a + 1 b } f2 = function(x, z) { y = x*z -2 y } Where I am running into trouble is when I want to call function f1 within function f2: f2(x = 3, z = f1(x)) This returns the error: "Error in f1(x) : object 'x' not found" I'm not sure how to define environments within the functions so that the just-defined 'x' may be passed as an argument to f1 within f2. Any help would be greatly appreciated! Ian Renner [[alternative HTML version deleted]]
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On 12/02/13 08:30, Ian Renner wrote:> Hi, > > I am trying to write a function which defines some arguments, then uses those arguments as > arguments of other function calls. It's a bit tricky to explain, so a simple example will have > to suffice. I imagine this has a simple solution, but perusing through environments and other > help lists has not helped. Suppose I have two functions: > > > f1 = function(a) { b = a + 1 b } f2 = function(x, z) { y = x*z -2 y } > > Where I am running into trouble is when I want to call function f1 within function f2: > > f2(x = 3, z = f1(x)) > > This returns the error: > > "Error in f1(x) : object 'x' not found"Obviously easiest: X <- 3 f2(X=x, Z=f1(X)) Your solution does not work, as the f1(x) is evaluated and the value is passed on to f1, and your x is an argument and *only in the function f2* available (= in this context *no* assignment). I remember something similar, and the solution had to do with eval() and quote() and friends - i.e. you have to only evaluate f(x) *inside* the function f2 - but unfortunately I do not remember details. Cheers, Rainer> > I'm not sure how to define environments within the functions so that the just-defined 'x' may > be passed as an argument to f1 within f2. > > Any help would be greatly appreciated! > > Ian Renner [[alternative HTML version deleted]] > > >-----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (GNU/Linux) Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ iQEcBAEBAgAGBQJRGf97AAoJENvXNx4PUvmCn0EIALjKZK4XsBkELCKJtWU7AXPd 7Qs5Sk2gQF64Jc1uHYm17hwgonZIFIj9IHE05lRP4dl2F6onMACfIEpzz+GF9cO5 qS2Kq3Oe/+bglBZIJ1oBGSxs2YEh5gGIykP+PZcWr4xP8QPQ23wnnTbafxTa7PaU BkkNrmNBbre5f+wYBbFaxSlSdlslDFg9c6b5OgLzwBLB0o9tr7KA6POCa8HrX7H4 UBsPfwSEkOfyIEwq5drKjXF853nUNRVtd0cPA+mpo+5y/qIkGTiehMRlEGwcBBg7 6uNA8wpTuJI49tdY7rkVEIEGH34atwvBA1kwFYh1UfBzzIg+oRikHm1ZJ4UeJCM=h9ju -----END PGP SIGNATURE-----
Hi Ian, The remark of Rainer is correct, but I think the solution is very simpe: f1 = function(a) { b = a + 1 b } f2 = function(x, z) { y = x*z(x) -2 y }> f2(x = 3, z = f1)[1] 10 Or are you intending something else? Best wishes, Frans -----Oorspronkelijk bericht----- Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Namens Ian Renner Verzonden: dinsdag 12 februari 2013 8:30 Aan: r-help at r-project.org Onderwerp: [R] Help with functions as arguments Hi, I am trying to write a function which defines some arguments, then uses those arguments as arguments of other function calls. It's a bit tricky to explain, so a simple example will have to suffice. I imagine this has a simple solution, but perusing through environments and other help lists has not helped. Suppose I have two functions: f1 = function(a) { b = a + 1 b } f2 = function(x, z) { y = x*z -2 y } Where I am running into trouble is when I want to call function f1 within function f2: f2(x = 3, z = f1(x)) This returns the error: "Error in f1(x) : object 'x' not found" I'm not sure how to define environments within the functions so that the just-defined 'x' may be passed as an argument to f1 within f2. Any help would be greatly appreciated! Ian Renner [[alternative HTML version deleted]]
On Tue, Feb 12, 2013 at 7:30 AM, Ian Renner <ian_renner at yahoo.com> wrote:> Where I am running into trouble is when I want to call function f1 within function f2: > > f2(x = 3, z = f1(x)) > > This returns the error: > > "Error in f1(x) : object 'x' not found" > > I'm not sure how to define environments within the functions so that the just-defined 'x' may be passed as an argument to f1 within f2.I think this is a bit like the joke where a man goes to the doctor, raises his arm up and says "It hurts when I do that", and the doctor says "well, don't do that then. Next patient!". Whatever is calling f2 knows what it is passing as the first argument. It should then know that it wants to pass f1(the first argument) and so can explicitly put that into the first argument of the f1 call. ie. your example should be; f2(x=3, z=f1(3)) What you are trying to do is akin to saying: f2(x=3, z=f1(that thing I passed to the x parameter, you expect me to type it in again? I thought computers were supposed to save effort!)) Now, this is different to *defining* f2 to take f1(x) as the default second parameter to f2: f2 = function(x,z=f1(x)){y=x*z-2;y} which is saying that unless the user says otherwise when they call this function, the second parameter is f1(the first parameter). This does work as expected. But the crux is that the terms inside function calls are not executed as assignments like you thought they were. For one thing, named function parameters can appear in any order - would you expect x be defined in the f1(x) if you called f2(z=f1(x),x=9)? Barry
Hi, I am trying to save a plot as a PDF with different line types. In the example below, R displays the plot correctly with 2 curves in solid lines, 2 curves in dashed lines and 1 curve as a dotted line. However, when I save the image as PDF (as attached), the dashed lines become solid. This also happens if I use the pdf command directly (by removing the # symbols). Is there any way around this? Saving the image as a JPEG or PNG file works fine but the image quality is not desirable. b.hat = 6 a.1 = -12 a.2 = 0 a.3 = 200 b = seq(-10, 10, 0.0002) l = a.1*(b - b.hat)^2 + a.2*(b - b.hat) + a.3 lambda = 20 p = -lambda*abs(b) pen.like = l + p y.min = 3*min(p) y.max = max(c(l, p, pen.like)) #pdf(file = "TestPlot.pdf", 6, 6) #{ plot(b, l, type = "l", ylim = c(y.min, y.max), lwd = 2, xlab = expression(beta), ylab = "", col = "green", yaxt = "n", xaxt = "n") points(b, p, type = "l", lty = "dotted", lwd = 2, col = "red") points(b, pen.like, type = "l", lwd = 2, lty = "dashed", col = "green") axis(1, at = c(0)) axis(2, at = c(0)) lambda.hat = which.max(pen.like) lambda.glm = which(b == b.hat) points(b[lambda.glm], l[lambda.glm], pch = 16, cex = 1.5) points(b[lambda.hat], l[lambda.hat], pch = 17, cex = 1.5) b.hat = -3 a.1 = -1.5 a.2 = 0 a.3 = 120 l = a.1*(b - b.hat)^2 + a.2*(b - b.hat) + a.3 pen.like = l + p points(b, l, type = "l", lwd = 2, col = "blue") points(b, pen.like, type = "l", lwd = 2, lty = "dashed", col = "blue") lambda.hat = which.max(pen.like) lambda.glm = which(b == b.hat) points(b[lambda.glm], l[lambda.glm], pch = 16, cex = 1.5) points(b[lambda.hat], l[lambda.hat], pch = 17, cex = 1.5) abline(h = 0) abline(v = 0) #} #dev.off() Thanks, Ian -------------- next part -------------- A non-text attachment was scrubbed... Name: TestPlot.pdf Type: application/pdf Size: 502139 bytes Desc: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130219/92be7b6c/attachment-0001.pdf>
Hi, On Tue, Feb 19, 2013 at 7:09 PM, Ian Renner <ian_renner at yahoo.com> wrote:> Hi, > > I am trying to save a plot as a PDF with different line types. In the example below, R displays the plot correctly with 2 curves in solid lines, 2 curves in dashed lines and 1 curve as a dotted line. However, when I save the image as PDF (as attached), the dashed lines become solid.I see two solid lines, two dashed lines, and one dotted, using okular version 0.16.0. I suspect your pdf viewer is buggy. What are you using to view the pdf? Best, Ista This also happens if I use the pdf command directly (by removing the # symbols).> > Is there any way around this? Saving the image as a JPEG or PNG file works fine but the image quality is not desirable. > > b.hat = 6 > a.1 = -12 > a.2 = 0 > a.3 = 200 > > b = seq(-10, 10, 0.0002) > l = a.1*(b - b.hat)^2 + a.2*(b - b.hat) + a.3 > > lambda = 20 > > p = -lambda*abs(b) > > pen.like = l + p > > y.min = 3*min(p) > y.max = max(c(l, p, pen.like)) > > #pdf(file = "TestPlot.pdf", 6, 6) > #{ > plot(b, l, type = "l", ylim = c(y.min, y.max), lwd = 2, xlab = expression(beta), ylab = "", col = "green", yaxt = "n", xaxt = "n") > points(b, p, type = "l", lty = "dotted", lwd = 2, col = "red") > points(b, pen.like, type = "l", lwd = 2, lty = "dashed", col = "green") > > axis(1, at = c(0)) > axis(2, at = c(0)) > > lambda.hat = which.max(pen.like) > lambda.glm = which(b == b.hat) > > points(b[lambda.glm], l[lambda.glm], pch = 16, cex = 1.5) > points(b[lambda.hat], l[lambda.hat], pch = 17, cex = 1.5) > > b.hat = -3 > a.1 = -1.5 > a.2 = 0 > a.3 = 120 > > l = a.1*(b - b.hat)^2 + a.2*(b - b.hat) + a.3 > > pen.like = l + p > > points(b, l, type = "l", lwd = 2, col = "blue") > points(b, pen.like, type = "l", lwd = 2, lty = "dashed", col = "blue") > > lambda.hat = which.max(pen.like) > lambda.glm = which(b == b.hat) > > points(b[lambda.glm], l[lambda.glm], pch = 16, cex = 1.5) > points(b[lambda.hat], l[lambda.hat], pch = 17, cex = 1.5) > > abline(h = 0) > abline(v = 0) > > #} > > #dev.off() > > > Thanks, > > Ian > > ______________________________________________ > 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. >
Pascal Oettli
2013-Feb-20 03:56 UTC
[R] Problems with line types in plots saved as PDF files
Hi, I also see the expected lines on your pdf file with Evince (3.2.1) using poppler/cairo (0.18.0). Regards, Pascal Le 20/02/2013 09:09, Ian Renner a ?crit :> Hi, > > I am trying to save a plot as a PDF with different line types. In the example below, R displays the plot correctly with 2 curves in solid lines, 2 curves in dashed lines and 1 curve as a dotted line. However, when I save the image as PDF (as attached), the dashed lines become solid. This also happens if I use the pdf command directly (by removing the # symbols). > > Is there any way around this? Saving the image as a JPEG or PNG file works fine but the image quality is not desirable. > > b.hat = 6 > a.1 = -12 > a.2 = 0 > a.3 = 200 > > b = seq(-10, 10, 0.0002) > l = a.1*(b - b.hat)^2 + a.2*(b - b.hat) + a.3 > > lambda = 20 > > p = -lambda*abs(b) > > pen.like = l + p > > y.min = 3*min(p) > y.max = max(c(l, p, pen.like)) > > #pdf(file = "TestPlot.pdf", 6, 6) > #{ > plot(b, l, type = "l", ylim = c(y.min, y.max), lwd = 2, xlab = expression(beta), ylab = "", col = "green", yaxt = "n", xaxt = "n") > points(b, p, type = "l", lty = "dotted", lwd = 2, col = "red") > points(b, pen.like, type = "l", lwd = 2, lty = "dashed", col = "green") > > axis(1, at = c(0)) > axis(2, at = c(0)) > > lambda.hat = which.max(pen.like) > lambda.glm = which(b == b.hat) > > points(b[lambda.glm], l[lambda.glm], pch = 16, cex = 1.5) > points(b[lambda.hat], l[lambda.hat], pch = 17, cex = 1.5) > > b.hat = -3 > a.1 = -1.5 > a.2 = 0 > a.3 = 120 > > l = a.1*(b - b.hat)^2 + a.2*(b - b.hat) + a.3 > > pen.like = l + p > > points(b, l, type = "l", lwd = 2, col = "blue") > points(b, pen.like, type = "l", lwd = 2, lty = "dashed", col = "blue") > > lambda.hat = which.max(pen.like) > lambda.glm = which(b == b.hat) > > points(b[lambda.glm], l[lambda.glm], pch = 16, cex = 1.5) > points(b[lambda.hat], l[lambda.hat], pch = 17, cex = 1.5) > > abline(h = 0) > abline(v = 0) > > #} > > #dev.off() > > > Thanks, > > Ian > > > > ______________________________________________ > 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. >
Eik Vettorazzi
2013-Feb-20 11:58 UTC
[R] Problems with line types in plots saved as PDF files
Hi Ian, besides encouraging alternative pdf viewers, you just could thin out your interpolation grid, such as b = seq(-10, 10, 0.02) #instead of 0.0002 and everything goes fine even with AcroRead XI. Cheers Am 20.02.2013 01:09, schrieb Ian Renner:> Hi, > > I am trying to save a plot as a PDF with different line types. In the example below, R displays the plot correctly with 2 curves in solid lines, 2 curves in dashed lines and 1 curve as a dotted line. However, when I save the image as PDF (as attached), the dashed lines become solid. This also happens if I use the pdf command directly (by removing the # symbols). > > Is there any way around this? Saving the image as a JPEG or PNG file works fine but the image quality is not desirable. > > b.hat = 6 > a.1 = -12 > a.2 = 0 > a.3 = 200 > > b = seq(-10, 10, 0.0002) > l = a.1*(b - b.hat)^2 + a.2*(b - b.hat) + a.3 > > lambda = 20 > > p = -lambda*abs(b) > > pen.like = l + p > > y.min = 3*min(p) > y.max = max(c(l, p, pen.like)) > > #pdf(file = "TestPlot.pdf", 6, 6) > #{ > plot(b, l, type = "l", ylim = c(y.min, y.max), lwd = 2, xlab = expression(beta), ylab = "", col = "green", yaxt = "n", xaxt = "n") > points(b, p, type = "l", lty = "dotted", lwd = 2, col = "red") > points(b, pen.like, type = "l", lwd = 2, lty = "dashed", col = "green") > > axis(1, at = c(0)) > axis(2, at = c(0)) > > lambda.hat = which.max(pen.like) > lambda.glm = which(b == b.hat) > > points(b[lambda.glm], l[lambda.glm], pch = 16, cex = 1.5) > points(b[lambda.hat], l[lambda.hat], pch = 17, cex = 1.5) > > b.hat = -3 > a.1 = -1.5 > a.2 = 0 > a.3 = 120 > > l = a.1*(b - b.hat)^2 + a.2*(b - b.hat) + a.3 > > pen.like = l + p > > points(b, l, type = "l", lwd = 2, col = "blue") > points(b, pen.like, type = "l", lwd = 2, lty = "dashed", col = "blue") > > lambda.hat = which.max(pen.like) > lambda.glm = which(b == b.hat) > > points(b[lambda.glm], l[lambda.glm], pch = 16, cex = 1.5) > points(b[lambda.hat], l[lambda.hat], pch = 17, cex = 1.5) > > abline(h = 0) > abline(v = 0) > > #} > > #dev.off() > > > Thanks, > > Ian > > > > ______________________________________________ > 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. >-- Eik Vettorazzi Department of Medical Biometry and Epidemiology University Medical Center Hamburg-Eppendorf Martinistr. 52 20246 Hamburg T ++49/40/7410-58243 F ++49/40/7410-57790 -- Pflichtangaben gem?? Gesetz ?ber elektronische Handelsregister und Genossenschaftsregister sowie das Unternehmensregister (EHUG): Universit?tsklinikum Hamburg-Eppendorf; K?rperschaft des ?ffentlichen Rechts; Gerichtsstand: Hamburg Vorstandsmitglieder: Prof. Dr. Martin Zeitz (Vorsitzender), Dr. Alexander Kirstein, Joachim Pr?l?, Prof. Dr. Dr. Uwe Koch-Gromus
Berend Hasselman
2013-Feb-20 12:36 UTC
[R] Problems with line types in plots saved as PDF files
On 20-02-2013, at 12:58, Eik Vettorazzi <E.Vettorazzi at uke.de> wrote:> Hi Ian, > besides encouraging alternative pdf viewers, you just could thin out > your interpolation grid, such as > b = seq(-10, 10, 0.02) #instead of 0.0002 > and everything goes fine even with AcroRead XI. >When I received the original mail this morning Mail.app on OS X 10.8.2 locked up completely when I tried to access the mail. Both Preview and QuickLook became totally unresponsive. With the above change a(0.02 instead of 0.0002) all goes smoothly now. Berend> Cheers > > Am 20.02.2013 01:09, schrieb Ian Renner: >> Hi, >> >> I am trying to save a plot as a PDF with different line types. In the example below, R displays the plot correctly with 2 curves in solid lines, 2 curves in dashed lines and 1 curve as a dotted line. However, when I save the image as PDF (as attached), the dashed lines become solid. This also happens if I use the pdf command directly (by removing the # symbols). >> >> Is there any way around this? Saving the image as a JPEG or PNG file works fine but the image quality is not desirable. >> >> b.hat = 6 >> a.1 = -12 >> a.2 = 0 >> a.3 = 200 >> >> b = seq(-10, 10, 0.0002) >> l = a.1*(b - b.hat)^2 + a.2*(b - b.hat) + a.3 >> >> lambda = 20 >> >> p = -lambda*abs(b) >> >> pen.like = l + p >> >> y.min = 3*min(p) >> y.max = max(c(l, p, pen.like)) >> >> #pdf(file = "TestPlot.pdf", 6, 6) >> #{ >> plot(b, l, type = "l", ylim = c(y.min, y.max), lwd = 2, xlab = expression(beta), ylab = "", col = "green", yaxt = "n", xaxt = "n") >> points(b, p, type = "l", lty = "dotted", lwd = 2, col = "red") >> points(b, pen.like, type = "l", lwd = 2, lty = "dashed", col = "green") >> >> axis(1, at = c(0)) >> axis(2, at = c(0)) >> >> lambda.hat = which.max(pen.like) >> lambda.glm = which(b == b.hat) >> >> points(b[lambda.glm], l[lambda.glm], pch = 16, cex = 1.5) >> points(b[lambda.hat], l[lambda.hat], pch = 17, cex = 1.5) >> >> b.hat = -3 >> a.1 = -1.5 >> a.2 = 0 >> a.3 = 120 >> >> l = a.1*(b - b.hat)^2 + a.2*(b - b.hat) + a.3 >> >> pen.like = l + p >> >> points(b, l, type = "l", lwd = 2, col = "blue") >> points(b, pen.like, type = "l", lwd = 2, lty = "dashed", col = "blue") >> >> lambda.hat = which.max(pen.like) >> lambda.glm = which(b == b.hat) >> >> points(b[lambda.glm], l[lambda.glm], pch = 16, cex = 1.5) >> points(b[lambda.hat], l[lambda.hat], pch = 17, cex = 1.5) >> >> abline(h = 0) >> abline(v = 0) >> >> #} >> >> #dev.off() >> >> >> Thanks, >> >> Ian >> >> >> >> ______________________________________________ >> 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. >> > > > -- > Eik Vettorazzi > > Department of Medical Biometry and Epidemiology > University Medical Center Hamburg-Eppendorf > > Martinistr. 52 > 20246 Hamburg > > T ++49/40/7410-58243 > F ++49/40/7410-57790 > -- > Pflichtangaben gem?? Gesetz ?ber elektronische Handelsregister und Genossenschaftsregister sowie das Unternehmensregister (EHUG): > > Universit?tsklinikum Hamburg-Eppendorf; K?rperschaft des ?ffentlichen Rechts; Gerichtsstand: Hamburg > > Vorstandsmitglieder: Prof. Dr. Martin Zeitz (Vorsitzender), Dr. Alexander Kirstein, Joachim Pr?l?, Prof. Dr. Dr. Uwe Koch-Gromus > > ______________________________________________ > 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.