R_Language 板


LINE

[关键字]: google 试算表 [重点摘要]: 网页废话版: http://yichunsung.github.io/r_notebook/notebook_for_goolesheets 因为我工作时有很多工作夥伴不懂coding、资料库这些东西。 唯一会的就是goole试算表和execl,後来便想到乾脆用R连接google试算表 方便大家需要资料的人自己上去google试算表中抓,省去很多电话打来打去的时间 网页版的是我写给一些会用R的工作夥伴的笔记,想说顺道分享上来。 1. 前言 直接利用R存取google试算表的资料,简化一些不必要的操作 2. 套件安装 利用googlesheets套件可以轻松地串接R与存取在云端上的google试算表。 ※ 范例: install.packages('googlesheets') library(googlesheets) 3. 连结google帐户 安装好套件後,首先第一步骤是要连结你的google帐户,在此之前你可以先去随意於网页 上建立一个测试用资料表,但也可以不用,googlesheets套件好用之处在於你可以完全利 用R去建立、存取和删除你帐户中的任何试算表。 利用这个函数googlesheets::gs_auth()建立与google帐户的授权 ※ 范例: gs_auth(new_user = TRUE) # 与帐户建立新的受权 gs_user() # 查看user资料 此时会自动帮你开启浏览器,你必须於浏览器中登入你的google帐号。登入成功後浏览器 会显示讯息,之後便可回到R的执行环境之下。 gs_user()这个函数可以查看该帐户的资 讯。 4. 查看google试算表 使用gs_ls()这个函数可以直接查看该帐户中所有可用的google试算表,如果该帐户中无 任何可存取的试算表他就会显示讯息告知你无任何试算表可使用 ※ 范例: gs_ls() # 查看帐户中的google试算表list 5. 读取 gs_tittle() : 注册帐户中可使用的Spreadsheet进入R,function内的参数为该 Spreadsheet名称。 gs_read() : 读取选择的spreadsheet中的某个Worksheet。 利用gs_title() 注册帐户中一个Spreadsheets,再利用gs_read()去读取Spreadsheet中 的其中一个worksheet,读取後他将存成一个data frame。 ※ 范例: test_gs <- gs_title('testFile') # 读取帐户中名为"testFile"的 Spreadsheet UBspot <- gs_read(ss=test_gs, ws = "工作表1", skip=0) # ss = spreadSheet, ws = worksheet gs_url() : 利用连结注册,function内的参数为该Spreadsheet的公开连结。 gs_read() : 读取选择的spreadsheet中的某个Worksheet。 可利用gs_url()注册的spreadsheet分为两类,一种是已发布的公开spreadsheet,另一种 是一般私人spreadsheet。 要利用私人的Spreadsheet,一样需要取得帐户授权,步骤如上面相同,只是注册 spreadsheet时由gs_title()改成gs_url(),而已经发布且公开的Spreadsheet则不需要帐 户授权的步骤,因为是公开发布的资料,所以任何人都可以透过该URL去注册这个 Spreadsheet。 但通常公开发布的资料表并不会开放权限给外人写入,只有读取功能而已。但相对地,如 果你有一份资料想要提供给超多人使用,这就是一个很好的途径,不然私人资料表你就得 透过google的共用功能去分享给别人。 注: 网页版有图教学如何公开发布goole试算表 http://yichunsung.github.io/r_notebook/notebook_for_goolesheets 发布成功後,便可以取得连结,在不需要帐户登入或授权情况下可以注册该spreadsheet 。除此之外,对R来说使用gs_url()最重要的场合就是让shinyapps去读取这个资料表,这 部分後面会有详尽的范例。 ※ 范例: Purl <- "https://docs.google.com/spreadsheets/d/1aoXyR7foNOsSergX39cEalNGOJNBMrWbIog0d ASocpw/edit?usp=sharing" sibi_spreadsheet <- gs_url(Purl) sibidf <- gs_read(ss=sibi_spreadsheet, ws = "K3_station_list", skip=0) 6. 写入新的资料 新增资料 ※ 范例: newdata <- data.frame(name=c("Ulaanbaatar Railway Station", "Ulaanbaatar Airport"), eng = c("Ulaanbaatar Railway Station", "Ulaanbaatar Airport"), lat = c(47.908592, 47.840388), lng = c(106.883938, 106.769976) ) gs_add_row(ss=test_gs, ws = "工作表1", input = newdata) # 新增资料 在你已授权的帐户中建立一个新的spreadsheet。 ※ 范例: gs_new("CWB-data", ws_title = "Beiliao", input = Beiliao, trim = TRUE, verbose = FALSE) 在已注册的Spreadsheet中新增一个新的worksheet。 ※ 范例: CWB_gs <- gs_title('CWB-data') gs_ws_new(ss = test_gs, ws_title = "Zhudong", input = test2, trim = TRUE, verbose = FALSE) 7. Shinyapps应用 把google sheets当作资料库,而shinnyapps的server.R可以连结进去 程式码在下面,Demo: https://yichunsung.shinyapps.io/testgooglesheets/ 这样的东西方便性在於,未来你要更新资料,你不必在把server.R打开来重新更新code, 你可以直接上去Google Sheet增加资料就好,介面简单直观,对於不用R的工作夥伴而言 也非常轻松可以协助更新资料。 另外注意,gs_read()千万记得放在shinyServer(function(input, output){}里面,才会 自动更新资料 server.R: library(shiny) library(googlesheets) library(plotly) GTPurl <- "https://docs.google.com/spreadsheets/d/11VFUdSCfZahqIuaO5eAvtCyyIjpnnYkG4w7KliiY_fc/edit?usp=sharing" GTPtest <- gs_url(GTPurl) shinyServer(function(input, output) { GTPtestdf <- gs_read(ss=GTPtest, ws = "工作表1", skip=0) GTPtestdf$date <- as.Date(GTPtestdf$date) output$the_data <- renderDataTable({ GTPtestdf }) output$plotlyData <- renderPlotly({ GTPdataplotly <- plot_ly( data = GTPtestdf, x = GTPtestdf$date, y = GTPtestdf$TA_Day, type = "scatter", mode = "liners+markers" ) GTPdataplotly }) }) ui.R: library(shiny) library(googlesheets) library(plotly) shinyUI(fluidPage( titlePanel("Quick demo"), sidebarLayout( sidebarPanel( dateRangeInput("dates", label = h3("date"), start = "2017-01-01", end = "2017-01-01", format = "yyyy-mm-dd"), selectInput("title", label = h3("station"), choices = c("title1", "title2", "title3"), selected = "title1"), submitButton("Submit"), br() ), mainPanel( tabsetPanel( tabPanel("result", dataTableOutput("the_data")), tabPanel("Plot", plotlyOutput("plotlyData")) ) ) ) )) Reference https://shiny.rstudio.com/articles/persistent-data-storage.html#gsheets https://github.com/jennybc/googlesheets/issues/126 https://www.r-bloggers.com/reading-data-from-google-sheets-into-r/ https://is.gd/t7yHAm --



※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 39.9.162.252
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/R_Language/M.1500538749.A.549.html ※ 编辑: KKbiger (39.9.162.252), 07/20/2017 16:33:06
1F:→ clansoda: 这个package算好用,唯一的缺陷是如果你要当资料库用 07/20 17:24
2F:→ clansoda: 在大型资料上他会慢得吓死人,顶多是几个MB的资料好用 07/20 17:25
3F:→ Wush978: 读取大型的资料还OK, 但是写入超级超级慢... 07/20 18:35
4F:→ clansoda: 而且他overwrite这块没有做出来,只能append跟delete 07/20 20:26
5F:→ KKbiger: 是的 写入相当慢 07/20 22:19
6F:推 cywhale: 没试过这样的方式~也许有机会应用~~ 07/20 22:57







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Boy-Girl站内搜寻

TOP