R Shiny Dashboard:从本地文件和在线数据库例如Google表格上传数据

问题描述

我是Shiny仪表板的初学者,遇到了困扰我很长时间的问题。
我的最终目标是将数据分配给名为“ myData”的变量,但我为用户提供了从本地文件或在线文件(在我的情况下为GoogleSheet)上传数据的选项。
以下是我的应用程序的简化版本。为了达到目标,我做到了:

  1. 在“数据”标签下,创建一个选择框“ input_option”,以便用户可以选择上传本地数据(=“ local”)还是从在线持久数据库(=“ online);
  2. 我使用“ eventReactive”来获取以“ input_option”值为条件的数据;
  3. 如果用户选择从在线数据库上传数据,则数据将显示在仪表板主体中;
  4. 如果用户选择从本地文件上载数据,则在仪表板主体中,它将显示“ fileInput”框,以指导用户选择本地文件。然后,数据也将显示在仪表板主体的下方。

但是,问题是:

  1. 无论哪种方式,数据都无法显示在仪表板主体中。我什至不知道数据是否已经成功获取
  2. 当我选择上传在线数据然后关闭应用程序时,R控制台不会暂停而是保持运行。

有什么朋友或专家可以帮助我解决这些问题吗?非常感谢您的帮助!

@H_404_25@library(shiny)
library(shinydashboard)
library(googlesheets4)
library(googledrive)

server = function(session,input,output)
{
  # "input_option" is used to select whether input data from local or online
  input_option = reactive(
    input$select_upload
  )
  
  # Upload the data
  myData = eventReactive(
    input$select_upload,if(input$select_upload == "local")
    {
      req(input$file_myData)
      read.csv(
        input$file_myData$datapath,header = T,stringsAsFactors = F,sep = input$sep_file_myData)
    }
    else if(input_option() == "online")
    {
      as.data.frame(gs4_find("myData_sample") %>% range_read())
    }
  )
  
  # display the myData data uplaoded ---
  output$display_myData = eventReactive(
    myData(),DT::renderDataTable(
      myData(),options = list(scrollX = T)
    )
  )
}

ui = dashboardPage(
  dashboardHeader(title = "My dashboard"),dashboardSidebar(
    sidebarMenu(
      id = "sidebarmenu",menuItem("Data upload",tabName = "data",icon = icon("database")),conditionalPanel(
        "input.sidebarmenu === 'data'",selectInput(
          inputId = "select_upload",label = "please select an option",choices = c("local","online"),selected = "local"
        )
      )
    )
  ),dashboardBody(
    tabItems(
      tabItem(
        tabName = "data",conditionalPanel(
          condition = "input.select_upload === 'local'",fileInput(inputId = "file_myData",label = "Choose csv file",accept = c("text/csv","text/comma-separated-values","text/plain",".csv")),radioButtons(inputId = "sep_file_myData","Separator",choices = c(Comma = ",",Semicolon = ";",Tab = "\t"),selected = ",")
        ),fluidRow(
          Box(
            title = "myData @R_98_4045@ion uploaded",solidHeader = T,status = "primary",width = 12,DT::dataTableOutput(outputId = "display_myData")
          )
        )
      )
    )
  )
)

shinyApp(ui,server)

解决方法

服务器中的两项更改将使本地文件正常工作,而googledrive也可能会更改。

server = function(session,input,output)
{
  # "input_option" is used to select whether input data from local or online
  input_option = reactive(
    input$select_upload
  )
  
  # Upload the data
  myData = eventReactive(
    input$file_myData,# HERE!
    if(input$select_upload == "local")
    {
      req(input$file_myData)
      read.csv(
        input$file_myData$datapath,header = T,stringsAsFactors = F,sep = input$sep_file_myData)
    }
    else if(input_option() == "online")
    {
      as.data.frame(gs4_find("myData_sample") %>% range_read())
    }
  )
  
  # display the myData data uplaoded --- # AND HERE!
  output$display_myData = DT::renderDataTable(
    myData(),options = list(scrollX = T)
  )
}

在帖子末尾有两个问题要问:

  1. 您可以使用在代码中添加print()语句的老式方法来调试有光泽的应用程序。观看R控制台,查看在应用程序中执行操作时,代码是否到达/未到达代码的何处。您还可以使用str()在屏幕上将仅在应用程序运行时存在的对象结构打印到屏幕上,以便确定如何处理它们。
  2. 这是正常现象-即使关闭了浏览器标签,您的应用仍在运行。请注意,您可以关闭选项卡并重新打开一个新选项卡(如果您从地址栏中复制了链接)。您也可以同时打开多个标签!要关闭该应用程序,只需在RStudio中点击几次逃脱即可。