아래 첨부파일 /Users/justin/idea/R 에 저장
RStudio에서
> setwd("/Users/justin/idea/R")
> install.packages("KoNLP")
> install.packages("wordcloud")
> library(KoNLP)
에러: package or namespace load failed for ‘KoNLP’: .onLoad가 loadNamespace()에서 'rJava'때문에 실패했습니다: 호출: dyn.load(file, DLLpath = DLLpath, ...) 에러: 공유된 객체 '/Library/Frameworks/R.framework/Versions/3.6/Resources/library/rJava/libs/rJava.so'를 로드 할 수 없습니다:
#Java 8 환경 변수 설정
> Sys.setenv(JAVA_HOME='/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre')
> dyn.load('/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre/lib/server/libjvm.dylib')
Sys.setenv('DYLD_FALLBACK_LIBRARY_PATH','/Library/Frameworks/R.framework/Resources/lib:/Users/justin/lib:/usr/local/lib:/usr/lib:::/lib:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/lib/server')
Console에서
❯ sudo R CMD javareconf
Java interpreter : /usr/bin/java
Java version : 1.8.0_202
Java home path : /Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre
Java compiler : /usr/bin/javac
Java headers gen.: /usr/bin/javah
Java archive tool: /usr/bin/jar
trying to compile and link a JNI program
detected JNI cpp flags : -I$(JAVA_HOME)/../include -I$(JAVA_HOME)/../include/darwin
detected JNI linker flags : -L$(JAVA_HOME)/lib/server -ljvm
clang -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre/../include -I/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre/../include/darwin -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -I/usr/local/include -fPIC -Wall -g -O2 -c conftest.c -o conftest.o
clang -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/Library/Frameworks/R.framework/Resources/lib -L/usr/local/lib -o conftest.so conftest.o -L/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre/lib/server -ljvm -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
JAVA_HOME : /Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre
Java library path: $(JAVA_HOME)/lib/server
JNI cpp flags : -I$(JAVA_HOME)/../include -I$(JAVA_HOME)/../include/darwin
JNI linker flags : -L$(JAVA_HOME)/lib/server -ljvm
Updating Java configuration in /Library/Frameworks/R.framework/Resources
Done.
R 및 R Studio 재기동
> library(KoNLP)
>library(wordcloud)
> useSejongDic()
> setwd("/Users/justin/idea/R")
> data1<-readLines("seoul_new.txt")
#data1 값 화면 출력
> data1
#data2에 대소문자 구분 명사 추출
> data2<-sapply(data1, extractNoun, USE.NAMES = F)
#추출된 명사를 30개만 출력
> head(unlist(data2),30)
> data3<-unlist(data2)
#모든 숫자 없애기
> data3<-gsub("\\d+","",data3)
> data3
> data3<-gsub("서울시","", data3)
> data3<-gsub("서울","", data3)
> data3<-gsub("요청","", data3)
> data3<-gsub("제안","", data3)
> data3<-gsub(" ","", data3)
> data3<-gsub("-","", data3)
#작업 디렉토리에 현재까지 작업된 파일 저장
> write(unlist(data3),"seoul_2.txt")
> data4<-read.table("seoul_2.txt")
#변수에 저장된 데이터 갯수 확인
> nrow(data4)
>wordcount<-table(data4)
>wordcount
#자주 등장하는 단어 순으로 정렬해서 20개 표시
> head(sort(wordcount,decreasing=T), 20)
#자주 등장하는 단어 중에서 분석에 불필요한 단어 정리
> data3<-gsub("개선","",data3)
> data3<-gsub("문제","",data3)
> data3<-gsub("관리","",data3)
> data3<-gsub("민원","",data3)
> data3<-gsub("이용","",data3)
> data3<-gsub("관련","",data3)
> data3<-gsub("시장","",data3)
# 3번 파일로 저장
> write(unlist(data3),"seoul_3.txt")
> data4<-read.table("seoul_3.txt")
> wordcount<-table(data4)
> head(sort(wordcount,decreasing=T), 20)
> library(RColorBrewer)
> palete<-brewer.pal(9,"Set3")
> wordcloud(names(wordcount),freq=wordcount,scale=c(5,1),rot.per = 0.25, min.freq = 1,
+ random.order = F, random.color = T, colors=palete)
#한글 깨지는 경우 폰트 지정으로 해결
> wordcloud(names(wordcount),freq=wordcount,scale=c(5,1),rot.per=0.25,min.freq=1,
+ random.order=F,random.color=T,colors=palete ,family="AppleGothic")
# 제목 텍스트 표시(한글 깨짐)
> legend(0.3,1,"빅데이터 시각화",cex = 0.8, fill = NA, border=NA, bg="white",text.col = "blue", text.font = 2, box.col = "red")
> legend(0.3,1,"R-Big Data Sample",cex = 0.8, fill = NA, border=NA, bg="white",text.col = "blue", box.col = "red")
https://m.blog.naver.com/twilight_teatime/221185918535
https://m.blog.naver.com/twilight_teatime/221185940907
'프로그래밍' 카테고리의 다른 글
성능 최적화 기법(성능 최적화 패턴) (1) | 2023.10.04 |
---|---|
스태프 엔지니어(Staff Engineer)란 무엇인가? (0) | 2023.06.23 |
Anti cross-site scripting (XSS) filter (0) | 2019.05.21 |