gpt4 book ai didi

R Shiny 仪表板: Upload data from both local file and the online database (such as Google Sheet)

转载 作者:行者123 更新时间:2023-12-02 19:16:26 25 4
gpt4 key购买 nike

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

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

但是,问题是:

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

有 friend 或者专家可以帮我解决这个问题吗?我真的非常感谢您的帮助!

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)

最佳答案

服务器中的两项更改将使本地文件正常工作,也可能使 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() 语句的老式方法来调试 Shiny 的应用程序。当您在应用程序中执行操作时,观察 R 控制台以查看代码中的哪些位置被访问/未被访问。您还可以使用 str() 将仅在应用运行时存在的对象结构打印到屏幕上,以便您了解如何处理它们。
  2. 这是正常行为 - 即使浏览器选项卡关闭,您的应用程序仍在运行。请注意,您可以关闭该选项卡并重新打开一个新选项卡(如果您从地址栏中复制了链接)。您还可以同时打开多个选项卡!要关闭应用程序,只需在 RStudio 中按几次转义键即可。

关于R Shiny 仪表板: Upload data from both local file and the online database (such as Google Sheet),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63651796/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com