Mas*_*Shi 2 user-input r shiny shinydashboard googlesheets4
我是Shiny仪表板的初学者,我有一个困扰我很长时间的问题。
我的最终目标是将数据分配给名为“myData”的变量,但我为用户提供了从本地文件或在线文件(在我的例子中为 GoogleSheet)上传数据的选项。
下面是我的应用程序的简化版本。为了实现目标,我做了:
然而,问题是:
有朋友或专家可以帮助我解决这些问题吗?我真的非常感谢你的帮助!
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)
服务器中的两项更改将使本地文件正常工作,也可能使 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)
帖子最后问你两个问题:
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.