R Shiny - 在调用应用程序之前加载参数和数据

时间:2017-01-24 16:56:52

标签: r shiny shinydashboard

我的应用程序结构如下:我想在调用应用程序之前加载参数(库,函数)和数据。当我运行此应用程序脚本时,它不起作用。因此,我目前的解决方案是在控制台中加载行直到df <- ...然后运行脚本。

TheFileDirectory <- "/.../"

# Libraries and functions
source(paste0(TheFileDirectory, "LibFun script.R"))

# Data
df <- readRDS(file = paste0(TheFileDirectory, "TheData.rds"))

# Call the app
source(paste0(TheFileDirectory, "server.R"))
source(paste0(TheFileDirectory, "ui.R"))

shinyApp(ui = Interface, server = Serveur)

我怎样才能避免这样做?我确信有一个干净的解决方案,但我找不到它。

以下是LibFun script.R

library(ggplot2)
library(shiny)
library(shinydashboard)

CountPlotFunction <- function(MyData)

    {
  MyPlot <- ggplot(data = MyData, aes(x = MyData)) +
    geom_bar(stat = "count", aes(fill = MyData)) +
    geom_text(stat = "count", aes(label = ..count..)) +
    scale_x_discrete(drop = FALSE) +
    scale_fill_discrete(drop = FALSE)
  return(MyPlot)
}

这是数据(执行TheData.rds的脚本):

var1 <- c("Russia","Canada","Australia","Australia","Russia","Australia","Canada","Germany","Australia","Canada","Canada")
var2 <- c("UnitedStates","France","SouthAfrica","SouthAfrica","UnitedStates","SouthAfrica","France","Norge","SouthAfrica","France","France")
var3 <- c("Brazil","Colombia","China","China","Brazil","China","Colombia","Belgium","China","Colombia","Colombia")
df <- data.frame(var1, var2, var3)

TheData <- saveRDS(df, file = paste0(TheFileDirectory, "TheData.rds"))

1 个答案:

答案 0 :(得分:2)

检查此链接: https://shiny.rstudio.com/articles/modules.html

部分:打包模块

您似乎有两个服务器和ui文件。因此,global.R文件会有所帮助,因为它将在服务器和ui之前调用。