Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6846823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:40:14+00:00 2026-05-27T00:40:14+00:00

I have written a function that creates a barplot. I would like to save

  • 0

I have written a function that creates a barplot. I would like to save this plot as a pdf as well as display it on my screen (x11) when applying this function. The code looks like this.

create.barplots <- function(vec)
 {
   x11()                                  # opens the window
   ### Here is a code that creates a barplot and works perfectly
   ### but irrelevant for my question
   dev.copy(pdf("barplots.table.2.pdf")) # is supposed to copy the plot in pdf
                                         # under the name "barplots.table.2.pdf"
   dev.off()                             # is supposed to close the pdf device
 }

This creates the following error: ‘device’ should be a function

When I modify the code to:

create.barplots <- function(vec)
 {
   x11()
   ### Here is a code that creates a barplot and works perfectly
   ### but irrelevant for my question
   dev.copy(pdf) # This is the only difference to the code above
   dev.off()
 }

R displays the plot and creates a file called Rplots.pdf. This is a problem because of several reasons.

I also tried to open the devices the other way around. First open the pdf device, than copy the content of the pdf device into the x11 device, than set the pdf device as active and than close the pdf device. The code here looks like this:

create.barplots <- function(vec)
 {
   pdf("barplots.table.2.pdf") # open the pdf device
   ### Here is a code that creates a barplot and works perfectly
   ### but irrelevant for my question
   dev.copy(x11)              # copy the content of the pdf device into the x11 device
   dev.set(which = 2)         # set the pdf device as actice
   dev.off()                  # close the pdf device
 }

The problem here is that the wondow that is supposed to display the plot is empty!

To sum up, I have two questions:
1) How to save a plot as pdf and display it in x11 simultaneously? And
2) How to save the plot not in the working directory somewhere else?

EDIT

The solutions above work great. But I still do not understand why

pdf("barplots.table.2")
barplot(something)
dev.copy(x11)

displays an empty grey window instead of copying the content of the pdf device in the window device! I also tried

pdf("barplots.table.2")
barplot(something)
dev.copy(window)

In which I failed as well…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:40:15+00:00Added an answer on May 27, 2026 at 12:40 am

    How about:

    create.barplots <- function(...) {
      x11()
      plot.barplots(...) # create the barplot
      dev.copy2pdf(file = "path/to/barplots.table.2.pdf")
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the view function in django that written like a dispatcher calling other
I have written a function to print database table to an array like this
I have written a program with C#, that creates a logfile and fills this
I have written a DLL which exports a function that creates a window using
I have written a function that gets a given number of random records from
I have written a function that positions a tooltip just above a textbox. The
I have written a javascript function that uses setInterval to manipulate a string every
I have written a PHP function to take a video embed code that has
I have a function I've written that was initially supposed to take a string
I have written a function for a multilevel wordpress menu, but I'd like it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.