In his last State of the Union address, President Obama took shots at Republican presidential candidates, expressed one of his "few regrets," and said he's "as confident as I have ever been that the state of our union is strong." (Sarah Parnass/The Washington Post)

President Obama tried to use his final State of the Union address Tuesday to calm Americans’ economic and national security anxieties, tout his record and rebuke Republican presidential hopefuls for the vitriolic tone of their campaigns to replace him.

“And then, as frustration [with politics] grows, there will be voices urging us to fall back into our respective tribes, to scapegoat fellow citizens who don’t look like us, or pray like us, or vote like we do, or share the same background,” Obama said, in one of many not-so-subtle references to GOP front-runner Donald Trump. “We can’t afford to go down that path. It won’t deliver the economy we want or the security we want, but most of all, it contradicts everything that makes us the envy of the world.”

The president — his hair now flecked with gray — was greeted with cheers of “O-bama!” from enthusiastic Democrats but mostly stony silence from Republicans. They laughed louder at his allusion to many members’ eagerness to resume campaigning for president in Iowa than at his joke about how even as the United States has cut carbon emissions, “gas under two bucks a gallon ain’t bad, either.”

“We live in a time of extraordinary change — change that’s reshaping the way we live, the way we work, our planet and our place in the world,” said Obama, standing before Vice President Biden and House Speaker Paul D. Ryan. “It’s change that can broaden ­opportunity or widen inequality. And whether we like it or not, the pace of this change will only accelerate.”

Obama gave passing mention to a handful of policy priorities — including promoting trade, curbing prescription-drug abuse, reforming the criminal-justice system and curing cancer — but he devoted more of the speech to talking to the nation rather than the House and Senate members before him.

Obama’s speech came with more than a year — a full one-eighth of his term — still remaining in the White House. But he seemed to be already thinking of what the place would look like without him and trying to balance confidence in his achievements (“Ask Osama bin Laden,” he said at one point) with acknowledgments that many Americans don’t feel as good about the Obama era as he does.

“I believe in you,” Obama said, as he closed. “That’s why I stand here confident that the state of our union is strong.”

History shows that presidents delivering their final State of the Union addresses take the opportunity to frame their time in office and begin to cement their legacies.

Obama said “one of the few regrets” of his presidency was that — after he ran on a message of unity and healing — American politics has become more divided and resentful on his watch.

“It’s one of the few regrets of my presidency — that the rancor and suspicion between the parties has gotten worse instead of better,” Obama said. “There’s no doubt a president with the gifts of Lincoln or Roosevelt might have better bridged the divide, and I guarantee I’ll keep trying to be better so long as I hold this office.”

Obama offered a sunny assessment of the nation — “clear-eyed,” “big-hearted” and “strong.” At the same time, he acknowledged that the country is in the grip of wrenching and unsettling transitions.

“America has been through big changes before — wars and depression, the influx of immigrants, workers fighting for a fair deal, and movements to expand civil rights,” Obama said. “Each time, there have been those who told us to fear the future; who claimed we could slam the brakes on change, promising to restore past glory if we just got some group or idea that was threatening America under control. And each time, we overcame those fears.”

What we learned from analyzing applause patterns in Obama’s eight State of the Union speeches.

Without naming them directly, Obama ripped into several of the leading GOP presidential candidates in blunt terms, suggesting that they are stoking similar fears now. In an uncertain world, he said, “our answer needs to be more than tough talk or calls to carpet-bomb civilians.”

America has earned respect by fostering a tradition of tolerance, he said, before making a veiled reference to Trump and Sen. Ted Cruz (R-Tex.).

“That’s why we need to reject any politics — any politics,” he repeated for emphasis, “that targets people because of race or religion. Let me say this: This isn’t a matter of political correctness. It’s a matter of understanding just what it is that makes us strong.”

Obama also made the case for his foreign policy approach, calling for an American internationalism built on broad alliances and focused on post-Cold War issues such as trade, combating disease epidemics and limiting climate change as well as fighting terrorism. He said the U.S. mission should be to make the world safe without becoming the world’s policeman.

He said that the United States “can’t try to take over and rebuild every country that falls into crisis. That’s not leadership; that’s a recipe for quagmire, spilling American blood and treasure that ultimately weakens us. It’s the lesson of Vietnam, of Iraq — and we should have learned it by now.”

Presidents typically lay out the challenges they see the country facing in their farewell addresses, just before leaving office. But Obama — who was emailing revisions to his speechwriter as late as 3 a.m. Tuesday — chose to do that on Tuesday instead, declaring that many of his goals would be left for future administrations and future generations.

The White House took advantage of the digital tools at its disposal, using a split screen and at times superimposing graphics over the president’s image to add emphasis. When he spoke about how immigration had roiled U.S. politics in the past, an image of the Statue of Liberty flashed on the screen.

“This is, actually, his farewell address,” Rice University history professor Douglas Brinkley said in an interview. “He’s doing that a year early, because he wants to get a jump start on gun control and climate change while he still has the bully pulpit.”

And Obama used that bully pulpit to preach about faults in the political system that had led Americans to feel locked out and to see the system as “rigged.” The answer, Obama continued, was to stop gerrymandering, increase voter participation and undertake campaign finance reform.

Seven years after he took office vowing to bridge the partisan divide, Obama beseeched lawmakers to work together. “We just might surprise the cynics again,” Obama said.

Yet half an hour into Obama’s speech, Ryan’s office issued a release saying that “lofty platitudes and nostalgic rhetoric may make for nice sound bites, but they don’t explain how to defeat ISIS, fix our critical safety net programs, and get our economy back on track. ”

Later, in the official Republican response to Obama, South Carolina Gov. Nikki Haley criticized Obama for having been too passive in the face of growing debt and terrorist threats.

“The president’s record has often fallen far short of his soaring words. As he enters his final year in office, many Americans are still feeling the squeeze of an economy too weak to raise income levels,” Haley said. “Even worse, we are facing the most dangerous terrorist threat our nation has seen since September 11th, and this president appears either unwilling or unable to deal with it.”

Obama avoided details of any legislation he hopes to promote in what he has called “the fourth quarter” of his presidency, referring to the last and often frantic period of football games.

But the president mentioned a series of goals, including lifting the trade embargo on Cuba, passing the Authorization for the Use of Military Force, expanding the earned income tax credit, making college more affordable, raising the minimum wage, and fixing the immigration system.

He called for the closure of the Guantanamo Bay prison, where scores of terrorism suspects have languished without trial. Obama called it a “recruitment brochure for our enemies.”

With a chair in the gallery left vacant to represent victims of gun violence, Obama also mentioned the need for tighter gun control.

Obama’s efforts to stake out grounds for optimism — turf that Presidents Ronald Reagan and Bill Clinton claimed in the past — were also driven by concern about his stubbornly mediocre approval ratings. These ratings have not improved much even though the economy has created 14 million jobs, the unemployment rate has been cut in half and the deficit slashed by three-quarters since his first year in office.

A January Gallup poll found that 76 percent of Americans are dissatisfied with the way things are going in the country; last week, 65 percent in a CBS News/New York Times poll said the nation is seriously on the wrong track. In election years, such figures generally help the party out of power make the case for change.

Those figures are even more striking because Obama has brought home nearly all American troops from Iraq and Afghanistan. Yet Americans, especially in the wake of the recent shootings in San Bernardino, Calif., are worried about terrorism.

As he has in the past, however, Obama said that while terrorism poses the greatest and most direct danger to the American people, it should not be overstated and it is very different from the dangers of the Cold War, when the Soviet Union posed an existential threat.

When it comes to the Islamic State, he said, “over-the-top claims that this is World War III just play into their hands.”

Here, too, the president took aim at Republican presidential hopefuls who have criticized him.

“All the talk of America’s economic decline is political hot air,” Obama said. “Well, so is all the rhetoric you hear about our enemies getting stronger and America getting weaker. The United States of America is the most powerful nation on Earth. Period. It’s not even close.”

David A. Fahrenthold contributed to this report.