R. Buckminster Fuller, the forward-looking inventor best known as the father of the geodesic dome, died of a heart attack Friday in Los Angeles at the Good Samaritan Hospital while visiting his wife, who is critically ill. He was 87 years old, and lived in Pacific Palisades, Calif.

As he put it himself, with his customary cheerful immodesty, Mr. Fuller was an ''an engineer, inventor, mathematician, architect, cartographer, philosopher, poet, cosmogonist, comprehensive designer and choreographer.''

He was also a thoroughgoing original, who for many years was dismissed by many as something of a crackpot. But by the 1950's, having stubbornly refused to abandon his beliefs that through technology ''man can do anything he needs to do'' and that ''man can create miracles,'' he had attracted a cultlike following.

Well into his 70's, Mr. Fuller yearly flew tens of thousands of miles, giving college lectures, some of which extended to a nonstop five or six hours. He was enormously popular among young people, whom he counseled to ''reform the environment instead of trying to reform man.''

''The young world,'' as he called modern youth, listened raptly as the somewhat disheveled, stockily built Mr. Fuller, his arms waving excitedly, his eyes flashing behind thick glasses, told of his dreams of a better tomorrow. Called Naive by Detractors

And he gave this advice to the young: ''Bite your tongue. Get a cinder in your eye. When you feel good, you feel nothing.'' He ignored detractors who called him a megalomaniac, ''an inspired child'' and a naive romanticist, garrulously proclaiming his vision that man, through technology and planning, could become superman.

''The entire population of the earth,'' he would say, ''could live compactly on a properly designed Haiti and comfortably on the British Isles.''

Mr. Fuller envisioned mile-high floating cities that could house tens of thousands in an environment free of air pollution and noise. Mid-Manhattan, he predicted, would one day be covered by a huge plastic dome, three miles in diameter. Some Ideas Practicable Now

Those, he admitted, were projects for the future, but independent architects said timidity and lack of imagination in others prevented immediate adoption of some Fuller ideas that were practicable, such as his proposal to cover 646 acres of the 1964-65 New York World's Fair with an air-conditioned dome.

Although he had neither an architect's formal training nor an architect's license, and his geodesic dome blueprints had to be signed by a licensed associate, he was acclaimed by architects and designers. Nathaniel Owings, of Skidmore, Owings & Merrill, called him ''the most creative man in our field.''

But Mr. Fuller was much more than just an architect and builder. His ideas took in the whole of man's environment and experience and potentialities, and he believed with crotchety single-mindedness that ''there is absolutely nothing that cannot be done.''

Frank Lloyd Wright called him ''a man with more absolute integrity than any man I have ever known.'' Held More Than 2,000 Patents

Mr. Fuller, who held more than 2,000 patents at his death, was elected in 1979 as one of the 50 members of the American Academy and Institute of Arts and Letters.

Earlier this year, President Reagan presented Mr. Fuller with the Presidential Medal of Freedom, saying he ''reminds us that America is a haven for innovators.'' He had also been awarded 39 honorary doctorates in fields ranging from the fine arts to engineering.

Mr. Fuller was the author of 25 books, including three acclaimed volumes of free verse and ''Operating Manual for Spaceship Earth,'' in which he compared the earth to a large mechanical device that, like an automobile, periodically must be tuned. He said each inhabitant must become familiar with its mechanics to keep it operating.

Two years ago, Mr. Fuller was given a surprise birthday party at the Science Museum of the Franklin Institute in Philadelphia. He had been drawn there under the pretext of taking Ellen Burstyn, the actress, on a private tour of a new exhibit.

When he opened the door, he was greeted by a crowd of journalists and friends, such as Merce Cunningham, the dancer; John Cage, the composer; Harris Wooford, the former president of Bryn Mawr, and Barbara Marx Hubbard, one of the country's leading futurists. Finding Fundamental Truths

During the celebration, which continued at the home of a curator of the museum, Mr. Fuller spoke of his discovery in his early 30's of certain fundamental truths that later guided his creativity, and said he had made a commitment never to use that knowledge for personal gain or power.

''My life has been, as a result, one miracle after another,'' he said. In 1979, after four Pennsylvania universities had announced they could no longer afford the $80,000 a year they had been chipping in to help support Mr. Fuller's research projects in Philadelphia, he said, ''I want people to get over the idea that I need some kind of charity.''

He said that while he had to scramble for support money, he had grossed $250,000 in the previous year and expected to do the same in 1979. A New England Heritage