layout: true background-image: url(https://user-images.githubusercontent.com/6179259/60290723-50002480-9954-11e9-96fe-3fbd4d7d11d9.png) background-size: cover --- class: center, middle, title-slide ## R로 하는 업무 자동화 ### <https://mrchypark.github.io/autoR/> #### [[의견 및 오류 신고]](https://github.com/mrchypark/autoR/issues) ### 박찬엽 [<svg style="height:0.8em;top:.04em;position:relative;fill:#FF1B70;" viewBox="0 0 512 512"><path d="M256 8C118.941 8 8 118.919 8 256c0 137.059 110.919 248 248 248 48.154 0 95.342-14.14 135.408-40.223 12.005-7.815 14.625-24.288 5.552-35.372l-10.177-12.433c-7.671-9.371-21.179-11.667-31.373-5.129C325.92 429.757 291.314 440 256 440c-101.458 0-184-82.542-184-184S154.542 72 256 72c100.139 0 184 57.619 184 160 0 38.786-21.093 79.742-58.17 83.693-17.349-.454-16.91-12.857-13.476-30.024l23.433-121.11C394.653 149.75 383.308 136 368.225 136h-44.981a13.518 13.518 0 0 0-13.432 11.993l-.01.092c-14.697-17.901-40.448-21.775-59.971-21.775-74.58 0-137.831 62.234-137.831 151.46 0 65.303 36.785 105.87 96 105.87 26.984 0 57.369-15.637 74.991-38.333 9.522 34.104 40.613 34.103 70.71 34.103C462.609 379.41 504 307.798 504 232 504 95.653 394.023 8 256 8zm-21.68 304.43c-22.249 0-36.07-15.623-36.07-40.771 0-44.993 30.779-72.729 58.63-72.729 22.292 0 35.601 15.241 35.601 40.77 0 45.061-33.875 72.73-58.161 72.73z"/></svg>][mail] [<svg style="height:0.8em;top:.04em;position:relative;fill:#FF1B70;" viewBox="0 0 264 512"><path d="M76.7 512V283H0v-91h76.7v-71.7C76.7 42.4 124.3 0 193.8 0c33.3 0 61.9 2.5 70.2 3.6V85h-48.2c-37.8 0-45.1 18-45.1 44.3V192H256l-11.7 91h-73.6v229"/></svg>][fb] [<svg style="height:0.8em;top:.04em;position:relative;fill:#FF1B70;" viewBox="0 0 496 512"><path d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6zm-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3zm44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9zM244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8zM97.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1zm-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7zm32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1zm-11.4-14.7c-1.6 1-1.6 3.6 0 5.9 1.6 2.3 4.3 3.3 5.6 2.3 1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2z"/></svg>][github] ### .small[2019년 7월 22일] .tiny[update 2019년 7월 22일] --- ## 목차 1. 마우스, 키보드 자동화 - [KeyboardSimulator][KeyboardSimulator] 1. 작업 결과 알림 - [Rpushbullet][Rpushbullet] 1. 스케줄러 등록 및 관리 - [taskscheduleR][taskscheduleR] --- ## 마우스, 키보드 자동화 - [KeyboardSimulator][KeyboardSimulator] 코드로 마우스와 키보드의 동작을 조절 윈도우 한정 패키지 ```r install.packages("KeyboardSimulator") ``` --- ## 키보드 자동화 `keydb.press()` 함수로 키보드 입력 대소구분 없이 소문자로 동작함 `+`로 연결할 수 있음 ```r library(KeyboardSimulator) keybd.press("enter") keybd.press("alt+f4") ``` --- ## 키보드 자동화 `keydb.press()` 함수에 `hold` 옵션이 있음 `keybd.release()` 함수로 `hold`한 키를 뗄 수 있음 ```r keybd.press("Alt", hold = TRUE) keybd.press('Tab') keybd.release('Alt') ``` --- ## 키보드 자동화 `keyboard_value`는 사용할 수 있는 키 리스트를 출력 ```r keyboard_value ``` --- ## 마우스 자동화 마우스의 움직임을 아래 코드로 조절할 수 있음 ```r mouse.get_cursor() # 현재 마우스 위치를 출력 mouse.click() # 왼쪽 클릭 mouse.click("right") # 오른쪽 클릭 mouse.move(960, 540) # 960, 540 위치로 커서 이동 mouse.move(960, 540, duration=3) # 3초 동안 커서 이동 ``` --- ## 마우스 자동화 키보드와 같이 `hold` 옵션과 `mouse.release()` 함수를 제공함 ```r mouse.click(button = "left", hold = TRUE) mouse.move(960, 540) mouse.release() ``` --- ## 클립보드 상호작용 - [clipr][clipr] `Ctrl+c`, `Ctrl+v` 하는 값을 R과 함께 사용할 수 있게 해주는 패키지 ```r install.packages("clipr") ``` ```r library(clipr) dr_clipr() ``` ``` ## clipr has read/write access to the system clipboard! ``` --- ## 클립보드 상호작용 - [clipr][clipr] ```r clear_clip() write_clip("hello") read_clip() ``` ``` ## [1] "hello" ``` ```r write_clip(c("hello","world!")) read_clip() ``` ``` ## [1] "hello" "world!" ``` --- ## 비밀 번호를 저장하고 사용하기 - [keyring][keyring] 1. `key_set()` 비밀 번호의 이름과 함께 입력하면, 비밀번호를 묻는 창이 뜨고, 등록할 수 있음 1. `key_get()` 함수를 이용해 비밀 번호의 이름으로 비밀번호를 가져옴 ```r library(keyring) key_set("key_name") key_get("key_name") ``` --- ## 함께 쓰면 좋은 함수 ```r # 1초 동안 대기 Sys.sleep(1) # 뒤에 명령을 4번 반복 replicate(4, keybd.press("enter")) # 경로로 지정하여 프로그램 실행 system2("C:/Program Files (x86)/Google/Chrome/chrome.exe") # 조건이 만족하는 동안은 계속 실행 while (condition) { ## 실행할 코드 } ``` --- ## while 문 설명 --- ## 작업 결과 알림 - [Rpushbullet][Rpushbullet] [pushbullet][pushbullet]을 이용해 알림을 받을 수 있음  --- class: middle  --- ## 패키지 설치 `pbGetPosts()` 함수를 사용하기 위해서는 아직 개발버전을 사용해야 함. ```r install.packages("RPushbullet") remotes::install_github("eddelbuettel/rpushbullet") ``` --- ## 회원가입 진행 구글 아이디와 페이스북 아이디로 진행할 수 있음  --- ## api key 생성 `Setting` 탭을 클릭하고 Access Tokens 부분에 Create Access Token 클릭  --- ## api key 생성 다시는 볼 수 없는 비밀키가 생김  --- ## api key 등록 `pbSetup()` 함수를 이용해서 api key 등록 ```r library(RPushbullet) pbSetup("XXXXXXXXXXXXXXXXXXXXXXXXXX") ``` `pbValidateConf()` 함수로 동작하는 키가 잘 등록되었는지 확인 ```r library(RPushbullet) pbValidateConf() ``` ``` ## No configuration specified. Assuming user meant: ~/.rpushbullet.json ``` ``` ## key is VALID ``` ``` ## device ujAFTeGzAHIsjxmBRVc0qq is VALID ``` ``` ## device ujAFTeGzAHIsjCxUGFi43E is VALID ``` ``` ## device ujAFTeGzAHIsjAiVsKnSTs is VALID ``` ``` ## [1] TRUE ``` --- ## post 보내기 post의 종류는 `note`, `link`, `file`로 동작합니다. ```r # note 보내기 pbPost(type = "note", title = "안녕하세요", body = "본문입니다.") # link 보내기 pbPost(type = "link", url = "https://mrchypark.github.io/") # file 보내기 pbPost(type = "file", url = "example.txt") ``` --- ## 보낸 post 확인하기 `pbGetPosts()` 함수는 그동안 보낸 post를 확인할 수 있음 ```r suppressMessages(library(dplyr)) pbGetPosts(limit = 4) ``` ``` ## active iden created modified type dismissed ## 1 TRUE ujAFTeGzAHIsjwJHwr1g3U 1563727395 1563767215 note TRUE ## direction sender_iden sender_email sender_email_normalized ## 1 self ujAFTeGzAHI mrchypark@gmail.com mrchypark@gmail.com ## sender_name receiver_iden receiver_email ## 1 chan yub park ujAFTeGzAHI mrchypark@gmail.com ## receiver_email_normalized title ## 1 mrchypark@gmail.com 안녕ㅎ세요 ``` --- ## 스케줄러 - [taskscheduleR][taskscheduleR]  --- ## 스케줄러 등록한 시간에 동작을 수행하도록 등록 ```r # mac이나 linux라면 cronR 설치 install.packages("taskscheduleR") ``` --- ## 작업 위치와 경로 1. 절대 경로 : "C://"으로 시작하는 전체의 경로를 모두 작성하는 것을 뜻함 1. 상대 경로 : `.`, `~` 등 특정한 위치를 나타내는 글자를 이용해서 경로를 표시 1. 작업 폴더 : `working diretory`의 번역이며 스크립트 파일이 실행되는 위치를 뜻함 - `.` : 현재 폴더 - `~` : HOME 폴더. 각 os, 유저 상황에 따라 유동적임. 윈도우 사용자 + R의 경우보통 문서 폴더를 뜻함. --- ## `try()` 함수로 상태 확인 `try()` 함수는 에러가 발생하는 것은 조절 할 수 있게 해줌. ```r res <- try(1+"1", silent = T) res ``` ``` ## [1] "Error in 1 + \"1\" : non-numeric argument to binary operator\n" ## attr(,"class") ## [1] "try-error" ## attr(,"condition") ## <simpleError in 1 + "1": non-numeric argument to binary operator> ``` ```r class(res) ``` ``` ## [1] "try-error" ``` [keyring]: https://github.com/r-lib/keyring [pushbullet]: https://www.pushbullet.com/ [clipr]: https://github.com/mdlincoln/clipr [taskscheduleR]: https://github.com/bnosac/taskscheduleR [KeyboardSimulator]: https://github.com/ChiHangChen/KeyboardSimulator [Rpushbullet]: https://github.com/eddelbuettel/rpushbullet [mail]: mailto:mrchypark@gmail.com [fb]: https://www.facebook.com/mrchypark [github]: https://github.com/mrchypark