我正在尝试在 shinyapps.io 上部署一个应用程序。它在本地工作没有任何问题。我最初将应用程序拆分为多个文件,包括数据文件夹、函数文件夹、global.R 文件,但为了排除故障和避免相对路径,我将所有内容合并到 app.R 文件中。我不确定错误是什么
这是代码
app.R
#Load required packages
pkgs <- c("DT", "plotly", "shiny", "data.table", "lubridate", "readr", "rsconnect")
lapply(pkgs , require, character.only = TRUE)
# Create data frame
# create a sequence of dates from Jan 1 2023 to Jan 31 2023 by minute intervals
dates <- seq(as.POSIXct("2023-01-01 00:00:00"), as.POSIXct("2023-01-05 23:59:00"), by = "min")
# format the dates as mm:dd:yy hh:mm
formatted_dates <- format(dates, "%m:%d:%y %H:%M")
# convert the formatted dates to Date objects
Time <- parse_date_time(formatted_dates, orders="mdy HM")
# set the seed for reproducibility
set.seed(123)
# create a data frame with 26 columns of random numbers
df <- data.frame(matrix(runif(length(Time)*26), nrow = length(Time)))
# name the columns of the data frame
colnames(df) <- LETTERS
# multiply each column by its position in the data frame
df <- mapply('+',df, seq(letters))
df <- cbind.data.frame(Time, df)
# functions
plotly_multi <- function(df, vars, x_var, mode, size, width, height){
uu <- df[vars]
setDT(uu)
longDF <- melt(uu, id.vars = x_var)
plot_ly(data = longDF, type = "scatter", mode = mode,
x = ~.data[[x_var]], y = ~value, split = ~variable,
marker = list(
size = size)
) %>%
layout(
xaxis = list(title = x_var),
width = width,
height = height
)
}
# ui.R ------------------------------------------------------------
ui <-
fluidPage(
titlePanel(title = "Interact with data"),
# sidebarLayout() ----------------------------------------------------------
sidebarLayout(
# sidebarPanel() ----------------------------------------------------------
sidebarPanel(
h4("Select X and Y datasets"),
fluidRow(
column(12,
selectizeInput(inputId = "x", label = "X data", choices = names(df))
)
),
fluidRow(
column(12,
selectizeInput(inputId = "y", label = "Y data", choices = names(df), multiple = T, selected = names(df)[2])
)
),
sliderInput("width", "Width", min = 300, max = 800, value = 500),
sliderInput("height", "Height", min = 300, max = 800, value = 500),
fluidRow(
column(12,
checkboxInput(inputId = "add_markers", label = "Display graph with markers")
)
),
conditionalPanel(condition =
"input.add_markers == 1",
sliderInput("marker_size", "Marker size", min = 1, max = 10, value = 5)
),
tags$hr(style="border-color: grey;"),
),
# mainPanel() -------------------------------------------------------------
mainPanel(
tabsetPanel(type = "tabs",
tabPanel("Results", value = 1,
plotlyOutput("plot")
), # tabPanel
id = "tabselected" ) # tabsetPanel
) # mainPanel()
) # sidebarLayout()
)
# server.R ------------------------------------------------------------
server <- function(input, output,session) {
# Results tab -------------------------------------------------------------
plot_mode <- reactive({
ifelse(input$add_markers, "lines+markers", "lines")
})
plot_obj <- reactive({
size = if(input$add_markers) input$marker_size else NULL
plotly_multi(df = df ,
vars = c(input$x, input$y),
x_var = input$x,
mode = plot_mode(),
size = size,
width = input$width,
height = input$height
)
})
output$plot <- renderPlotly({
plot_obj()
})
}
shinyApp( ui = ui, server = server)