我是Shiny仪表板的初学者,有一个问题困扰了我很长时间
我的最终目标是将数据分配给一个名为";myData";,但我为用户提供了从本地文件或在线文件(在我的案例中是GoogleSheet(上传数据的选项
下面是我的应用程序的简化版本。为了实现目标,我做了:
- 在";数据";选项卡,我创建一个选择框";input_option";,以便用户可以选择上传本地数据(="本地"(或从在线持久数据库(="在线"(上传数据
- 我使用";eventReactive;以"0"的值为条件来获取数据;input_option">
- 如果用户选择从在线数据库上传数据,数据将显示在仪表板主体中
- 如果用户选择从本地文件上传数据,则在仪表板主体中;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)
服务器中的两个更改将使本地文件工作,可能还有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)
)
}
在你的文章末尾给你两个问题:
- 您可以使用在代码中粘贴
print()
语句的老派方法调试闪亮的应用程序。观察R控制台,查看当您在应用程序中执行操作时,代码中的哪些位置被访问/未被访问。您还可以使用str()
在屏幕上打印只有在应用程序运行时才存在的对象的结构,这样您就可以找出如何处理它们 - 这是正常行为-即使浏览器选项卡关闭,您的应用程序也在运行。请注意,您可以关闭选项卡并重新打开一个新选项卡(如果您从地址栏复制了链接(。您还可以同时打开多个选项卡!要关闭该应用程序,只需在RStudio中点击几次escape即可