Estoy tratando de usar el paquete LaTeX gensymb en mi documento de rebajas de R (salida PDF), pero parece que no funciona. Estos dos ejemplos funcionan: EN LaTeX como un archivo.tex compilado con. Turn your analyses into high quality documents, reports, presentations and dashboards with R Markdown. Use a productive notebook interface to weave together narrative text and code to produce elegantly formatted output. Use multiple languages including R, Python, and SQL. R Markdown supports a reproducible workflow for dozens of static and dynamic output formats including HTML, PDF, MS. LaTeX is rich with options and, fortunately, most things that we want to do can be represented with R Markdown and automatically converted to LaTeX via Pandoc. Since LaTeX is a powerful typesetting tool, it is possible to do things with it for which there is no R Markdown equivalent. TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. As per this page on the R Markdown.
One of the neat tools available via a variety of packages in R is the creation of beautiful tables using data frames stored in R. In what follows, I’ll discuss these different options using data on departing flights from Seattle and Portland in 2014. (More information and the source code for this R package is available at https://github.com/ismayc/pnwflights14.)
We begin by ensuring the needed packages are installed and then load them into our R session.
The dataset provides for the development of a lot of interesting questions. Here I will delve further into some of the questions I addressed in two recent workshops I led in the Fall 2015 Data @ Reed Research Skills Workshop Series. (Slides available at http://rpubs.com/cismay.)
The questions I will analyze by creating tables are
1. Which destinations had the worst arrival delays (on average) from the two PNW airports?
2. How does the maximum departure delay vary by month for each of the two airports?
3. How many flights departed for each airline from each of the airports?
The kable
function in the knitr
package
To address the first question, we will use the dplyr
package written by Hadley Wickham as below. We’ll use the top_n
function to isolate the 5 worst mean arrival delays.
This information is helpful but you may not necessarily know to which airport each of these FAA airport codes refers. One of the other data sets included in the pnwflights14
package is airports
that lists the names. Here we will do a match to identify the names of these airports using the inner_join
function in dplyr
.
Lastly we output this table cleanly using the kable
function.
Airport Name | Airport Code | Mean Arrival Delay |
---|---|---|
Cleveland Hopkins Intl | CLE | 26.150000 |
William P Hobby | HOU | 10.250000 |
Metropolitan Oakland Intl | OAK | 10.067460 |
San Francisco Intl | SFO | 8.864937 |
Bellingham Intl | BLI | 8.673913 |
Oddly enough, flights to Cleveland (from PDX and SEA) had the worst arrival delays in 2014. Houston also had around a 10 minute delay on average. Surprisingly, the airport in Bellingham, WA (only around 100 miles north of SEA) had the fifth largest mean arrival delay.
The DT package
In order to answer the second question, we’ll again make use of the various functions in the dplyr
package.
The DT
package provides a nice interface for viewing data frames in R. I’ve specified a few extra options here to show all 12 months by default and to automatically set the width. Go ahead and play around with the filter boxes at the top of each column too. (An excellent tutorial on DT
is available at https://rstudio.github.io/DT/.)
The created table in HTML is available here.
If you click on the max_delay
column header, you should see that the maximum departure delay for PDX was in March and for Seattle was in May.
The xtable
package to produce nice tables in a PDF
Again, we find ourselves using the extremely helpful dplyr
package to answer this question and to create the underpinnings of our table to display. We merge the flights data with the airlines
data to get the names of the airlines from the two letter carrier code.
The xtable
package and its xtable
function (and also the kable
function you saw earlier) provide the functionality to generate HTML code or (LaTeX) code to produce a table. We will focus on producing the (LaTeX) code in this example.
begin{table}[ht]
centering
begin{tabular}{rllrl}
hline
& origin & carrier & count & name
hline
1 & PDX & AS & 12844 & Alaska Airlines Inc.
2 & PDX & WN & 11193 & Southwest Airlines Co.
3 & PDX & OO & 9841 & SkyWest Airlines Inc.
4 & PDX & UA & 6061 & United Air Lines Inc.
5 & PDX & DL & 5168 & Delta Air Lines Inc.
6 & PDX & US & 2361 & US Airways Inc.
7 & PDX & AA & 2187 & American Airlines Inc.
8 & PDX & F9 & 1362 & Frontier Airlines Inc.
9 & PDX & B6 & 1287 & JetBlue Airways
10 & PDX & VX & 666 & Virgin America
11 & PDX & HA & 365 & Hawaiian Airlines Inc.
12 & SEA & AS & 49616 & Alaska Airlines Inc.
13 & SEA & WN & 12162 & Southwest Airlines Co.
14 & SEA & DL & 11548 & Delta Air Lines Inc.
15 & SEA & UA & 10610 & United Air Lines Inc.
16 & SEA & OO & 8869 & SkyWest Airlines Inc.
17 & SEA & AA & 5399 & American Airlines Inc.
18 & SEA & US & 3585 & US Airways Inc.
19 & SEA & VX & 2606 & Virgin America
20 & SEA & B6 & 2253 & JetBlue Airways
21 & SEA & F9 & 1336 & Frontier Airlines Inc.
22 & SEA & HA & 730 & Hawaiian Airlines Inc.
hline
end{tabular}
end{table}
If you don’t know (LaTeX), I’ve also duplicated a similar table using kable
for you to compare:
origin | carrier | count | name |
---|---|---|---|
PDX | AS | 12844 | Alaska Airlines Inc. |
PDX | WN | 11193 | Southwest Airlines Co. |
PDX | OO | 9841 | SkyWest Airlines Inc. |
PDX | UA | 6061 | United Air Lines Inc. |
PDX | DL | 5168 | Delta Air Lines Inc. |
PDX | US | 2361 | US Airways Inc. |
PDX | AA | 2187 | American Airlines Inc. |
PDX | F9 | 1362 | Frontier Airlines Inc. |
PDX | B6 | 1287 | JetBlue Airways |
PDX | VX | 666 | Virgin America |
PDX | HA | 365 | Hawaiian Airlines Inc. |
SEA | AS | 49616 | Alaska Airlines Inc. |
SEA | WN | 12162 | Southwest Airlines Co. |
SEA | DL | 11548 | Delta Air Lines Inc. |
SEA | UA | 10610 | United Air Lines Inc. |
SEA | OO | 8869 | SkyWest Airlines Inc. |
SEA | AA | 5399 | American Airlines Inc. |
SEA | US | 3585 | US Airways Inc. |
SEA | VX | 2606 | Virgin America |
SEA | B6 | 2253 | JetBlue Airways |
SEA | F9 | 1336 | Frontier Airlines Inc. |
SEA | HA | 730 | Hawaiian Airlines Inc. |
With the originating airport duplicating across all of the airlines, it would be nice if we could reduce this duplication and just bold PDX or SEA and have each appear once. Awesomely enough, the rle
function in R will be of great help to us in this endeavor. It counts how many times a value is repeated in a table. We will then make a call to the multirow
function in (LaTeX) in a sneaky way of pasting the appropriate text in addition to using the force
option for sanitizing the text into (LaTeX).
We add in a few options to make the output of the table a little nicer by specifying horizontal lines and removing the default rownames.
The resulting table produced by (LaTeX) can be found at Overleaf.com at https://www.overleaf.com/read/wvrpxpwrbvnk.
We see that Alaska Airlines had the most flights out of both airports with Southwest coming in second at both airports.
(The generating R Markdown file for this HTML document—saved in the .Rmd extension—is available here.)
This document is for people who are unfamiliar with command line tools. Command-line experts can go straight to the User’s Guide or the pandoc man page.
First, install pandoc, following the instructions for your platform.
Pandoc is a command-line tool. There is no graphic user interface. So, to use it, you’ll need to open a terminal window:
On OS X, the Terminal application can be found in
/Applications/Utilities
. Open a Finder window and go toApplications
, thenUtilities
. Then double click onTerminal
. (Or, click the spotlight icon in the upper right hand corner of your screen and typeTerminal
– you should seeTerminal
underApplications
.)On Windows, you can use either the classic command prompt or the more modern PowerShell terminal. If you use Windows in desktop mode, run the
cmd
orpowershell
command from the Start menu. If you use the Windows 8 start screen instead, simply typecmd
orpowershell
, and then run either the “Command Prompt” or “Windows Powershell” application. If you are usingcmd
, typechcp 65001
before using pandoc, to set the encoding to UTF-8.On Linux, there are many possible configurations, depending on what desktop environment you’re using:
- In Unity, use the search function on the
Dash
, and search forTerminal
. Or, use the keyboard shortcutCtrl-Alt-T
. - In Gnome, go to
Applications
, thenAccessories
, and selectTerminal
, or useCtrl-Alt-T
. - In XFCE, go to
Applications
, thenSystem
, thenTerminal
, or useSuper-T
. - In KDE, go to
KMenu
, thenSystem
, thenTerminal Program (Konsole)
.
- In Unity, use the search function on the
You should now see a rectangle with a “prompt” (possibly just a symbol like %
, but probably including more information, such as your username and directory), and a blinking cursor.
Let’s verify that pandoc is installed. Type
and hit enter. You should see a message telling you which version of pandoc is installed, and giving you some additional information.
First, let’s see where we are. Type
on Linux or OSX, or
on Windows, and hit enter. Your terminal should print your current working directory. (Guess what pwd
stands for?) This should be your home directory.
Let’s navigate now to our Documents
directory: type
and hit enter. Now type
(or echo %cd%
on Windows) again. You should be in the Documents
subdirectory of your home directory. To go back to your home directory, you could type
The ..
means “one level up.”
Go back to your Documents
directory if you’re not there already. Let’s try creating a subdirectory called pandoc-test
:
Now change to the pandoc-test
directory:
If the prompt doesn’t tell you what directory you’re in, you can confirm that you’re there by doing
(or echo %cd%
) again.
OK, that’s all you need to know for now about using the terminal. But here’s a secret that will save you a lot of typing. You can always type the up-arrow key to go back through your history of commands. So if you want to use a command you typed earlier, you don’t need to type it again: just use up-arrow until it comes up. Try this. (You can use down-arrow as well, to go the other direction.) Once you have the command, you can also use the left and right arrows and the backspace/delete key to edit it.
Most terminals also support tab completion of directories and filenames. To try this, let’s first go back up to our Documents
directory:
Now, type
and hit the tab key instead of enter. Your terminal should fill in the rest (test
), and then you can hit enter.
To review:
pwd
(orecho %cd%
on Windows) to see what the current working directory is.cd foo
to change to thefoo
subdirectory of your working directory.cd ..
to move up to the parent of the working directory.mkdir foo
to create a subdirectory calledfoo
in the working directory.- up-arrow to go back through your command history.
- tab to complete directories and file names.
Type
and hit enter. You should see the cursor just sitting there, waiting for you to type something. Type this:
R Markdown To Latex File
When you’re finished (the cursor should be at the beginning of the line), type Ctrl-D
on OS X or Linux, or Ctrl-Z
followed by Enter
on Windows. You should now see your text converted to HTML!
R Markdown To Latex Code
What just happened? When pandoc is invoked without specifying any input files, it operates as a “filter,” taking input from the terminal and sending its output back to the terminal. You can use this feature to play around with pandoc.
By default, input is interpreted as pandoc markdown, and output is HTML. But we can change that. Let’s try converting from HTML to markdown:
Now type:
and hit Ctrl-D
(or Ctrl-Z
followed by Enter
on Windows). You should see:
Now try converting something from markdown to LaTeX. What command do you think you should use?
You’ll probably want to use pandoc to convert a file, not to read text from the terminal. That’s easy, but first we need to create a text file in our pandoc-test
subdirectory.
Important: To create a text file, you’ll need to use a text editor, not a word processor like Microsoft Word. On Windows, you can use Notepad (in Accessories
). On OS X, you can use TextEdit
(in Applications
). On Linux, different platforms come with different text editors: Gnome has GEdit
, and KDE has Kate
.
Start up your text editor. Type the following:
Now save your file as test1.md
in the directory Documents/pandoc-test
.
Note: If you use plain text a lot, you’ll want a better editor than Notepad
or TextEdit
. You might want to look at Sublime Text or (if you’re willing to put in some time learning an unfamiliar interface) Vim or Emacs.
Go back to your terminal. We should still be in the Documents/pandoc-test
directory. Verify that with pwd
.
Now type
(or dir
if you’re on Windows). This will list the files in the current directory. You should see the file you created, test1.md
.
To convert it to HTML, use this command:
The filename test1.md
tells pandoc which file to convert. The -s
option says to create a “standalone” file, with a header and footer, not just a fragment. And the -o test1.html
says to put the output in the file test1.html
. Note that we could have omitted -f markdown
and -t html
, since the default is to convert from markdown to HTML, but it doesn’t hurt to include them.
Check that the file was created by typing ls
again. You should see test1.html
. Now open this in a browser. On OS X, you can type
On Windows, type
You should see a browser window with your document.
To create a LaTeX document, you just need to change the command slightly:
Try opening test1.tex
in your text editor.
Pandoc can often figure out the input and output formats from the filename extensions. So, you could have just used:
Pandoc knows you’re trying to create a LaTeX document, because of the .tex
extension.
Now try creating a Word document (with extension docx
).
R Markdown Latex Table
If you want to create a PDF, you’ll need to have LaTeX installed. (See MacTeX on OS X, MiKTeX on Windows, or install the texlive package on Linux.) Then do
You now know the basics. Pandoc has a lot of options. At this point you can start to learn more about them by reading the User’s Guide.
R Markdown Tex
Here’s an example. The --mathml
option causes pandoc to convert TeX math into MathML. Type
then enter this text, followed by Ctrl-D
(Ctrl-Z
followed by Enter
on Windows):
Now try the same thing without --mathml
. See the difference in output?
If you forget an option, or forget which formats are supported, you can always do
R Markdown To Latex Code
to get a list of all the supported options.
Use Latex Packages In R Markdown
On OS X or Linux systems, you can also do
to get the pandoc manual page. All of this information is also in the User’s Guide.
If you get stuck, you can always ask questions on the pandoc-discuss mailing list. But be sure to check the FAQs first, and search through the mailing list to see if your question has been answered before.