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

Mas*_*Shi 2 user-input r shiny shinydashboard googlesheets4

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

  1. 在“data”选项卡下,我创建了一个选择框“input_option”,以便用户可以选择上传本地数据(=“local”)或来自在线持久数据库(=“online”)的数据;
  2. 我使用“eventReactive”来获取以“input_option”的值为条件的数据;
  3. 如果用户选择从在线数据库上传数据,则数据将显示在仪表板主体中;
  4. 如果用户选择从本地文件上传数据,则仪表板主体中会显示“文件输入”框,引导用户选择本地文件。然后数据也会显示在仪表板主体的下方。

然而,问题是:

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

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

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 information uploaded", solidHeader = T, status = "primary",
            width = 12,
            DT::dataTableOutput(outputId = "display_myData")
          )
        )
      )
    )
  )
)

shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)

pse*_*pin 5

服务器中的两项更改将使本地文件正常工作,也可能使 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)
  )
}
Run Code Online (Sandbox Code Playgroud)

帖子最后问你两个问题:

  1. You can debug shiny apps with the old school method of sticking print() statements in your code. Watch the R console to see where in your code is/isn't being reached when you perform actions in your app. You can also use str() to print to the screen the structure of objects which only exist when the app is running so you can work out how to deal with them.
  2. This is normal behaviour - your app is running even with the browser tab closed. Note you can close the tab and reopen a new one (if you copied the link from the address bar). Also you can open multiple tabs at the same time! To close the app, just hit escape a couple of times in RStudio.