【问题标题】:Shiny app can't access data if runApp is run from a .Rprofile如果 runApp 从 .Rprofile 运行,闪亮的应用程序无法访问数据
【发布时间】:2019-09-29 01:51:13
【问题描述】:

我构建了一个使用从 excel 文件创建的列表变量(用于标签自定义)的 Shiny 应用程序。这个变量的名字是refvar

refvar 在 server.R 中创建到 shinyServer() 中,用于构建发送到 ui.R 的 UI 元素。 refvar 也被 load.data() 使用。 load.data() 被定义到 global.R 中并用于 shinyServer()。

奇怪的是,如果我手动运行应用程序(RStudio 中的“运行应用程序”按钮或 RStudio 控制台中的 runApp())它可以工作,而如果我将 shiny::runApp() 放在 .Rprofile 文件中,以启动应用程序打开我的 RStudio 项目后,我在浏览器中收到错误“错误:找不到对象 'refvars'”。

这是(据说)相关代码

.Rprofile

    shiny::runApp()

服务器.R

    shinyServer(function(input, output) {

        refvars <- read_excel('Labels.xlsx') %>% dplyr::select(1:2) %>% set_colnames(c('Var', 'Label')) %>% mutate_all(str_squish) %>% spread(Var, Label) %>% as.list


        Data <- load.data()

        output$UI.report.sel.text <- renderUI({helpText(refvars$report.sel.text)})
        output$UI.daterange.sel.text <- renderUI({helpText(refvars$daterange.sel.text)})


        #[...]
    })

全球.R

load.data <- function() {

    tryCatch(read_excel("Report_data.xlsx"), error = function(e) file.choose()) %>%
        rename(!!c(
            Anno = refvars$year,
            Mese = refvars$month,
            Indagine = refvars$report,
            Reparto = refvars$context,
            Note = refvars$notes,
            Problemi = refvars$problems,
            Sforamenti = refvars$non.compliance,
            Protocollo = refvars$id
        )) %>%
        mutate(
            Data = ymd(paste(Anno, Mese, '1', sep = '-'))
        )
}

更新:

我做了一些实验,将refvar 放入 global.R。该应用程序现在从 .Rprofile 开始运行良好,但有些东西被破坏了。例如,应该有一个 ggplot 我得到“错误:找不到对象'标题'”,但实际上我没有使用任何 title 对象。
我试图检查通过 capture.output(str(Data)) 传递给 ggplot 的数据的状态,并通过 uiOutput 指令将其输出到 ui.R。错误变成它可以找到函数str()和capture.output(); util包没有加载!

所以我认为问题在于 .Rprofile 中的 runApp() 运行得太早了。我该如何解决这个问题?

【问题讨论】:

    标签: r shiny initialization rprofile


    【解决方案1】:

    问题完全在于运行 .Rprofile 时仍然没有加载默认包。

    所以我不得不重新定义 .First.sys 函数,在那里添加我的 runApp 代码:

    .Rprofile

    pkg.require <- function(pkgs, load = T){
        installed <- utils::installed.packages()
    
        not.installed <- c()
    
        for (pkg in pkgs) {
            if (!(pkg %in% installed)) not.installed <- c(not.installed, pkg)
        }
    
        if (length(not.installed) > 0) utils::install.packages(not.installed)
    
        if (load) for (pkg in pkgs) library(pkg, character.only = TRUE)
    }
    
    
    .First.sys <- function () 
    {
        for (pkg in getOption("defaultPackages")) {
            res <- require(pkg, quietly = TRUE, warn.conflicts = FALSE, 
                character.only = TRUE)
            if (!res) 
                warning(gettextf("package %s in options(\"defaultPackages\") was not found", 
                    sQuote(pkg)), call. = FALSE, domain = NA)
        }
    
        pkg.require(c('shiny'))
    
        shiny::runApp(launch.browser = T)
    }
    

    【讨论】:

      猜你喜欢
      • 2017-09-09
      • 2013-07-08
      • 1970-01-01
      • 2015-03-28
      • 2016-02-09
      • 2016-06-15
      • 2018-02-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多