首页 > 解决方案 > 闪亮的帮助:发布和“运行应用程序”将“变灰”,但使用 shinyApp() 作品

问题描述

我正在开发我的第一个 Shiny 应用程序,当我在本地运行它时它看起来很棒,但如果使用 Rstudio“运行应用程序”按钮或通过 shinyapp.io 发布时会失败。

我确保在通过 shinyapp.io 发布时该目录不是本地目录,并且 csv 文件位于应用程序文件夹中。我使用 profvis 检查了应用程序的性能,该应用程序使用 352.9 MB 内存和 1060 毫秒。

这是ui文档:

library(shiny)
library(ggplot2)
library(readr)
library(tidyr)
library(data.table)
library(reshape2)
library(dplyr)
library(DT)
library(shinyWidgets)
library(ggrepel)
library(rsconnect)
library(devtools)


#master document. replaced with github data for sharing
df1 <- read_csv("https://raw.githubusercontent.com/KeithTStory/EnvironmentalAnalytics/master/Shiny/CABIN_explorer/20190522(2)_cabin_habitat_data_mda05_1987-present.csv",
                col_types = cols_only(Site = "c", Day = "i", Year = "i", Status = "c", Type = "c", Variable = "c", Unit = "c", Value  = "n"),
                locale = locale(encoding = "latin1")
                )

attach(df1) #odd that I am having to attach names. Seems to work with names attached though.

#Melt data to give each variable a column for selecting
df2 <- df1 %>%
  mutate(id = row_number()) %>%
  replace(., is.na(.), 0) %>%
  filter(Site != 0, Value > 0) %>%
  spread(key = Variable, value = Value) %>%
  group_by(Year) 

attach(df2)

#remove symbols that are causing issues. Tried to convert encoding but had no success.

colnames(df2) <- gsub(x = colnames(df2), pattern = "[+%-]", replacement = "")
colnames(df2) <- gsub(x = colnames(df2), pattern = " ", replacement = ".")  

df1$Variable <-gsub(x = df1$Variable, pattern = "[+%-]", replacement = "")
df1$Variable <-gsub(x = df1$Variable, pattern = " ", replacement = ".")

#This function is used to in the ui input choices for the y axis
FilterFunc <- function(condition, df) {
  subset.data <- df %>%
    filter(Type == condition)

  data_list <- unique(subset.data$Variable)
  return(as.character(data_list))
}

#useful variables for ui choices

types <- unique(as.factor(df1$Type))
df_sites <- unique(df2$Site)

### ui code_____________________________________________
ui <- fluidPage(

  titlePanel("CABIN Data Explorer (Nelson River)"),

  sidebarPanel(

    selectInput(inputId = "site",
                label = "Select Sampling Site:",
                choices = df_sites,
                selectize = TRUE,
                multiple = TRUE,
                selected = "521"
                ),

    selectInput(inputId = "y",
                label = "Y-axis:",
                choices = list(
                  "Physical Data"      = FilterFunc(types[1], df1), #pulling from df1 since it is pre-spread and simpler to work with
                  "Sediment Chemistry" = FilterFunc(types[2], df1),
                  "Water Chemistry"    = FilterFunc(types[3], df1),   
                  "Channel"            = FilterFunc(types[4], df1), 
                  "Climate"            = FilterFunc(types[5], df1), 
                  "Substrate Data"     = FilterFunc(types[6], df1), 
                  "Hydrology"          = FilterFunc(types[7], df1), 
                  "Topography"         = FilterFunc(types[8], df1), 
                  "Landcover"          = FilterFunc(types[9], df1), 
                  "Bedrock Geology "   = FilterFunc(types[10], df1)
                ),
                selectize = TRUE,
                selected = "Ca"
    )
  ),

  mainPanel(
    plotOutput('histplot'),
    DT::dataTableOutput(outputId = "site_table")
  )
)

和服务器文档:

# 
server <- function(input, output) {

  DF <- reactive({

    df2 %>%
      select(Site, Day, Year, Unit, Value = input$y) %>%
      filter(Site %in% input$site) %>%
      na.omit()

  })

  output$histplot <- renderPlot({
    req(input$site)
    req(input$y)


    p <- ggplot(data = DF(), aes(y = Value, x = Year, label = Site, color = Site)) + 
      geom_point(stat = "identity",
                 size = 3) +
      geom_label_repel(fill = "white",
                       size = 6) +
      scale_x_continuous(limits = c(2005, 2018)) +
      # scale_y_continuous(limits = c(0, max(Var))) +
      theme_bw() +
      theme(panel.grid.major = element_blank(),
            panel.grid.minor = element_blank(),
            legend.position = "none",
            panel.background = element_rect(fill = "floralwhite")

      ) +
      facet_wrap(~.Unit) +
      labs(ylab(input$y))

    print(p)

  }, height=300)

  output$site_table <- DT::renderDataTable({
    req(input$site)
    req(input$y)


    DT::datatable(data = DF(),
                  options = list(pageLength = 10),
                  rownames = FALSE)
  })

}

shinyApp(ui, server)

当我在本地运行 shinyApp(ui, server) 时,一切看起来都很棒。

当我在 RStudio 中使用“运行应用程序”按钮时,我收到以下消息:

Warning: Error in serverFuncSource: server.R returned an object of unexpected type: list
  [No stack trace available]
Error in serverFuncSource() : 
  server.R returned an object of unexpected type: list

当我使用 RStudio 中的发布按钮发布应用程序时,Web 文档开始加载,但随后“灰显”并显示“与服务器断开连接”,并可选择重新加载。

检查控制台输出会产生对我不知道如何解释的 js 错误的引用(也许您可以重现并指导我?)。

感谢您的时间和任何建议!(欢迎提供有关如何解释这些错误的信息)。

标签: rdeploymentshiny

解决方案


推荐阅读