I am a beginner of Shiny dashboard and I have a problem that bothered me a long time.
My final target is to assign data to a variable called "myData", but I give the users options to upload data either from local file or online file (in my case GoogleSheet).
Below is a simplified version of my app. To achieve the target, I did:
However, the problems are:
Could any friends or experts help me with the problems? I really really appreciate your help!
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)
Two changes in the server will make the local file work, and probably the googledrive one too.
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)
)
}
For you two questions at the end of your post:
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.