我是 Shiny 仪表板的初学者,我有一个困扰我很久的问题。
我的最终目标是将数据分配给一个名为“myData”的变量,但我为用户提供了从本地文件或在线文件(在我的情况下为 GoogleSheet)上传数据的选项。
下面是我的应用程序的简化版本。为了实现目标,我做了:
- 在“数据”选项卡下,我创建了一个选择框“input_option”,以便用户可以选择上传本地数据(="local")或来自在线持久数据库的数据(="online);
- 我使用“eventReactive”根据“input_option”的值获取数据;
- 如果用户选择从在线数据库上传数据,则数据将显示在仪表板正文中;
- 如果用户选择从本地文件上传数据,在dashboard body中会显示“fileInput”框,引导用户选择本地文件。然后数据也将显示在仪表板主体的下方。
然而,问题是:
- 无论哪种方式,数据都无法显示在仪表板正文中。我什至不知道数据是否已成功获取;
- 当我选择上传在线数据然后关闭应用程序时,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)