如果选择了侧边栏菜单项,如何使用Google表格

问题描述 投票:0回答:1

所以我有一个我正在构建的Shiny应用程序。我的目标是尝试引入已连接到Salesforce数据库的Google表格。我已将其抽象为Google表格,并创建了图表和数据表格,通过我们公司的salesforce动态刷新到Google套件集成。这使我可以更自由地引入数据并将其可视化,而无需在Salesforce中真正构建仪表板,因为Salesforce非常昂贵。所以目前我一直在用R studio构建闪亮的代码。我有一个非常好的布局。

  1. 我正在尝试使用谷歌图表库引入嵌入在网页中的谷歌图表
  2. 我试图以交互方式获取数据表以生成框图,显示UI中数据表下方的谷歌图表。

我已经尝试将其设置在侧栏菜单输出中作为数据框。但是,我没有成功。

#
# This is the user-interface definition of a Shiny web application. You can
# run the application by clicking 'Run App' above.
#
# Find out more about building applications with Shiny here:
# 
#    http://shiny.rstudio.com/
#

## app.R ##
library(shinydashboardPlus)
library(shinydashboard)
library(shiny)
library(rsconnect)
library(googlesheets)
library(RCurl)
library(httr)
library(dplyr)
library(mosaic)
library(DT)
library(googleCharts)
library(fontawesome)


# Google Sheets for Synced Keys with Data Master
gs_auth(new_user = FALSE)
handover <- gs_key("1Wu8gJyzw6o7BS4GoR7pM_NofHyXvOzDMK3O-VVHcB8c")
#cr_mw_data <- gs_key("Insert_Key")
sev3_sev4_data <- 
gs_key("1ga7s1vgMhYRNvr2WL6vjv_VRYtP5nI0aMoweLAjB6v4")
#all_alerts_data <- gs_key("Insert_Key")
#case_wo_key_5 <- gs_key("Insert_Key")
#sc_sev_1_cases <- gs_key("Insert_Key")
#subscriptios <- gs_key("Insert_Key")
bimonthly_ttr <- gs_key("1TiQeStsuwATHWxExV_Pdb2rSuOlOPB3KcYbPKFOj8VQ")


for_gs_sheet <- gs_read(handover)
str(for_gs_sheet)

for_gs_sheet <- gs_read(sev3_sev4_data)
str(for_gs_sheet)

for_gs_sheet <- gs_read(bimonthly_ttr)
str(for_gs_sheet)

## Identifies the Google Sheet in Question
## handover_sheet = gs_url("https://docs.google.com/spreadsheets/d/1Wu8gJyzw6o7BS4GoR7pM_NofHyXvOzDMK3O-VVHcB8c/edit#gid=0")
## bimonthly_ttr = gs_url("https://docs.google.com/spreadsheets/d/1TiQeStsuwATHWxExV_Pdb2rSuOlOPB3KcYbPKFOj8VQ/edit#gid=0")
#shinyApp(ui = ui, server = server, options = list(height = 1080))

ui <- dashboardPage(skin = "red",
                dashboardHeader(title = "Miradashboard",
                                # This drop-down menu offers user and system administration within the application
                                dropdownMenu(type = "messages",
                                             messageItem(
                                               from = "Sales Dept",
                                               message = "Sales are steady this month."
                                             ),
                                             messageItem(
                                               from = "New User",
                                               message = "How do I register?",
                                               icon = icon("question"),
                                               time = "13:45"
                                             ),
                                             messageItem(
                                               from = "Support",
                                               message = "The new server is ready.",
                                               icon = icon("life-ring"),
                                               time = "2014-12-01"
                                             )
                                ),
                                # This is a drop-down menu for checking notifications.
                                # This should alert users of alerts that have not been merged to a case in the last 15 days.
                                dropdownMenu(type = "notifications",
                                             notificationItem(
                                               text = "5 new users today",
                                               icon("users")
                                             ),
                                             notificationItem(
                                               text = "12 items delivered",
                                               icon("truck"),
                                               status = "success"
                                             ),
                                             notificationItem(
                                               text = "Server load at 86%",
                                               icon = icon("exclamation-triangle"),
                                               status = "warning"
                                             )
                                ),
                                # This is a drop-down menu for checking tasks.
                                # This drop-down menu will eventually offer suggestions based off of ML Algorithms.
                                dropdownMenu(type = "tasks", badgeStatus = "success",
                                             taskItem(value = 90, color = "green",
                                                      "Documentation"
                                             ),
                                             taskItem(value = 17, color = "aqua",
                                                      "Project X"
                                             ),
                                             taskItem(value = 75, color = "yellow",
                                                      "Server deployment"
                                             ),
                                             taskItem(value = 80, color = "red",
                                                      "Overall project"
                                             )
                                )
                ),

                dashboardSidebar(
                  ## Sidebar content
                  dashboardSidebar(
                    sidebarMenu(
                      menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
                      menuItem("Data", icon = icon("th"), href = "https://docs.google.com/spreadsheets/d/1ga7s1vgMhYRNvr2WL6vjv_VRYtP5nI0aMoweLAjB6v4/edit#gid=1342420852"),
                      menuItem("Widgets", icon = icon("th"), href = "https://shiny.rstudio.com/gallery/", newtab = FALSE),
                      menuItem("Mirantis Directory", icon = icon("sitemap") ,href = "https://directory.mirantis.com/#/resources/staffing"),
                      menuItem("Reports", tabName = "reports", icon = icon("chart-line"), startExpanded = FALSE,
                               menuSubItem("All Alerts", tabName = "All Alerts", icon = icon("dna")),
                               menuSubItem("All CR & MW", tabName = "All CR & MW", icon = icon("dna")),
                               menuSubItem("Sev3 & Sev4 Data", tabName = "Sev3 & Sev4 Data", icon = icon("dna")),
                               menuSubItem("SC Sev1 Cases", tabName = "SC Sev1 Cases", icon = icon("dna")),
                               menuSubItem("Cases with Key 5 Updates", tabName = "Cases with Key 5 Updates", icon = icon("dna")),
                               menuSubItem("Cases without Key 5 Updates", tabName = "Cases without Key 5 Updates", icon = icon("dna")),
                               menuSubItem("Auto Refresh Execution Log", tabName = "Auto Refresh Execution Log", icon = icon("dna")),
                               menuSubItem("L1 Oncall", tabName = "L1 Oncall", icon = icon("calendar-alt")),
                               menuSubItem("TET Oncall", tabName = "TET Oncall", icon = icon("calendar-alt")),
                               menuSubItem("SME Oncall", tabName = "SME Oncall", icon = icon("calendar-alt")),
                               menuSubItem("AT&T Oncall", tabName = "AT&T Oncall", icon = icon("calendar-alt"))
                               ),
                      menuItem("OpsCare Clients", tabName = "OpsCare Clients", icon = icon("bar-chart-o"), startExpanded = FALSE,
                               menuSubItem("All Cases", tabName = "All Cases", icon = icon("bezier-curve")),
                               menuSubItem("All Alerts", tabName = "All Alerts", icon = icon("bezier-curve")),
                               menuSubItem("TTR Metrics by Customer", tabName = "TTR Metrics by Customer", icon = icon("bezier-curve"))
                               ),
                      menuItem("Top 10 Clients", tabName = "Top 10 Clients", icon = icon("bar-chart-o"), startExpanded = FALSE,
                               menuSubItem("Adobe Systems", tabName = "Adobe Systems", icon = icon("address-card")),
                               menuSubItem("Apple Inc", tabName = "Apple Inc", icon = icon("address-card")),
                               menuSubItem("AT&T Inc", tabName = "AT&T Inc", icon = icon("address-card")),
                               menuSubItem("Cox Communications", tabName = "Cox Communications", icon = icon("address-card")),
                               menuSubItem("Edge Gravity by Ericsson", tabName = "Edge Gravity by Ericsson", icon = icon("address-card")),
                               menuSubItem("Ericsson - Mediakind", tabName = "Ericsson - Mediakind", icon = icon("address-card")),
                               menuSubItem("Ericcson Telefonakatiebolaget LM Ericsson", tabName = "Ericcson Telefonakatiebolaget LM Ericsson", icon = icon("address-card")),
                               menuSubItem("Inspur", tabName = "Inspur", icon = icon("address-card")),
                               menuSubItem("Reliance", tabName = "Reliance", icon = icon("address-card")),
                               menuSubItem("Shanghai Xietong (Sharetome)", tabName = "Shanghai Xietong (Sharetome)", icon = icon("address-card")),
                               menuSubItem("State Street Corporation", tabName = "State Street Corporation", icon = icon("address-card")),
                               menuSubItem("Volkswaggen", tabName = "Volkswaggen", icon = icon("address-card")),
                               menuSubItem("Other Clients", tabName = "Other Clients", icon = icon("address-card"))
                      ),
                      menuItem("ProdCare Clients", tabName = "ProdCare Clients", icon = icon("bar-chart-o")),
                      menuItem("Alerts", tabName = "Alerts", icon = icon("bar-chart-o")),
                      menuItem("Change Requests", tabName = "Change Requests", icon = icon("list-alt")),
                      menuItem("Maintenance Windows", tabName = "Maintenance Windows", icon = icon("list-alt")),
                      menuItem("Rundeck", icon = icon("code"), 
                               href = "https://rundeck.suplab01.snv.mirantis.net/user/login"),
                      menuItem("Salesforce", icon = icon("database"), 
                               href = "https://mirantis.my.salesforce.com/"),
                      menuItem("Handovers", icon = icon("google"), 
                               href = "https://docs.google.com/spreadsheets/d/1Wu8gJyzw6o7BS4GoR7pM_NofHyXvOzDMK3O-VVHcB8c/edit#gid=0"),
                      menuItem("Jump-Host Access", tabName = "Jump-Host Access", icon = icon("bars")),
                      menuItem("Mirantis HT Wiki", icon = icon("bars"), 
                               href = "https://mirantis.jira.com/wiki/spaces/2S/pages/1254621239/L1+-+General+Queue+Help+Desk+Team"),
                      menuItem("Slack", icon = icon("slack"), href = "https://miracloud.slack.com"),
                      menuItem("Source code", icon = icon("github"), 
                               href = "https://github.com/Richard-Barrett/Miradashboard")
                    )
                  )
                ),
                dashboardBody(
                  # Boxes need to be put in a row (or column)
                  fluidRow(
                    #box(plotOutput("plot1", height = 250)),
                    #box(plotOutput("plot2", height = 250)),
                    #box(plotOutput("plot3", height = 250)),
                    #box(plotOutput("plot4", height = 250)),
                    h1("Handovers"),
                    DT::dataTableOutput("mytable", width = "auto", height = "auto"),
                    #box(plotOutput("plot1", height = 250)),
                    #box(gs_read(ss, ws = "handover")),
                    #box(plotOutput("plot3", height = 250)),
                    #box(plotOutput("plot4", height = 250)),

                    #Example Attachment Box 
                    #box(
                    #  title = "Attachment example",
                    #  attachmentBlock(
                    #    src = "http://kiev.carpediem.cd/data/afisha/o/2d/c7/2dc7670333.jpg",
                    #    title = "Test",
                    #    title_url = "http://google.com",
                    #    "This is the content"
                    #  )
                    #),
                    box(plotOutput("plot1", height = 250)),
                    box(plotOutput("plot2", height = 250)),
                    #box(dataTableOutput("DT1", height = 250))
                    box(
                      title = "Controls",
                      sliderInput("slider", "Number of observations:", 2, 200, 50)
                    )
                  )
                )
)

server <- function(input, output) {
  set.seed(122)
  histdata <- rnorm(500)


  output$mytable = DT::renderDataTable({
df <- gs_read(handover)
  })

  # List Server Output whereby plot[1-#] is the plot box output in UI above.
  # Server Output occurs and is defined by data variables
  # histdata[seq_len(input$slider)] defines slider utilization
  # hist(data) defines histogram off of "data"
  output$plot1 <- renderPlot({
data <- histdata[seq_len(input$slider)]
hist(data)
  })
  output$plot2 <- renderPlot({
data <- histdata[seq_len(input$slider)]
hist(data)
  })
  output$plot3 <- renderPlot({
data <- histdata[seq_len(input$slider)]
hist(data)
  })
  output$plot4 <- renderPlot({
data <- histdata[seq_len(input$slider)]
hist(data)
  })
  output$plot5 <- renderPlot({
data <- histdata[seq_len(input$slider)]
hist(data)
  })

}

shinyApp(ui, server)

实际结果导致我在运行应用程序时将切换电子表格作为数据表放在主屏幕上。但是,我想如果一个人选择了它指向的实际侧边栏菜单项并从另一个工作表加载不同的数据表.enter image description here

r user-interface google-sheets shiny shinydashboard
1个回答
0
投票

所以我终于把脑筋缠在上面。首先,您需要在R会话中安装并加载googlesheets包。

library(googlesheets)

在此之后,您需要掌握基础知识以引入工作表。因此,从此处将工作表指定为数据框。

google_sheet_df <- gs_key("<insert_key_after_the_/d/###forkey_id>")

从这里你可以获得闪亮应用的UI,BODY和SERVER代码。在里面

sidebarMenu(menuItem("ITEM NAME", tabName = "NAME_TAG", icon = icon("dna"))),
dashboardBody(tabItems(tabItem(tabName = "NAME_TAG",fluidRow(
DT::dataTableOutput("mytable",width = "auto, height = "auto"))))),
server <- function(input, output) {
set.seed(122)
histdata <- rnorm(500)


## Data Table Outputs from Google Sheets
output$mytable = DT::renderDataTable({
df <- gs_read(google_sheet_df) ##NAME OF GOOGLE SHEET DF
})

shinyApp(ui, server)

最重要的部分是您在服务器输出中呈现dataTableOutput,然后引用嵌入在您希望它显示的指定tabItem中的主体中的DT(如果单击它)。

© www.soinside.com 2019 - 2024. All rights reserved.