r - Sys.getenv() 在 Shiny Apps 中返回空

标签 r shiny

注意我编辑了这个问题,因为使用了 PG_HOST引起了困惑,但精神是一样的

我正在运行一个需要读取一些环境变量的 Shiny 应用程序。

该变量是在 Shiny 的服务器启动之前定义的。例如

export APPLE=apple
export PENCIL=pencil

global.R (或者在 server.R 的开头是一样的)我写了以下代码:
manzana <- Sys.getenv('APPLE')
lapiz <- Sys.getenv('PENCIL')

但这些变量是空的。

如果我在 R 控制台中运行该代码,两者都会返回正确的值。

为什么这不起作用? R 控制台和 Shiny 的应用程序有何不同?
我如何获得真实的环境变量(在这个假示例中 $APPLE$PENCIL )?
哪个是配置 Shiny 应用程序的正确方法?

最佳答案

第一步是了解 react 性。查看 Shiny 的教程。

使用你的例子......有点......这里有一个应用程序更新和设置可以通过多种方式调用的变量......

shiny_example <- function(){

  server <- shinyServer(function(session,input,output){
    the_slots <- list(Apple = 'apple',Green = 'green')

    make_globs <- function(new_var = NULL){
      if(!is.null(new_var)){
        the_slots <<- append(the_slots,new_var)
      }
    }

    glob_vals <- the_slots

    glob_vals <- eventReactive(input$saver, {
      set_new_vars <- list(input$new_var)
      names(set_new_vars) <- input$new_var_name
      the_slots <<- make_globs(new_var = set_new_vars)
      lapply(list('new_var','new_var_name'),function(i)updateTextInput(session,i, value = ""))
      return(the_slots)

    })

    output$envs <- renderPrint({

      glob_vals()

    })

    output$sels <- renderUI({
      vals <- 1:length(glob_vals())
      Opts <- unlist(lapply(vals,function(i)sprintf('<option value="%s">%s</option>',i,names(glob_vals()[i])))) %>% HTML
      HTML(
        paste(
          "<div class='shiny-input-container'>",
          "<label class='control-label' for='the_ups'></label>",
          "<div><select id='the_ups'>",Opts,"</select></div>",
          "</div>",sep=""))
    })

    output$sel_vals <- renderPrint({
      ref_cards <- lapply(1:length(glob_vals()),function(i)
        data.frame(the_names = names(glob_vals()[i]),the_vals = glob_vals()[[i]]))%>%
        rbind.pages

      ref_cards[input$the_ups,'the_vals']


    })

  })


  ui <- shinyUI(
    bootstrapPage(
      tags$div(class="container",
               fluidRow(
                 tags$h4(HTML('These inputs will update the variable list \n like a variable in Sys.getenv()')),
                 column(6,textInput(inputId = "new_var_name",label = "variable name")),
                 column(6,textInput(inputId = "new_var",label = 'variable value'))
               ),
               fluidRow(

                 column(6,
                        tags$h4(
                          HTML('Pressing the `add_new` button will load the variables and display the corresponding values below'),
                          actionButton(inputId = "saver",label = "add_new")
                        )),
                 column(6,tags$h4("You can even dynamically update a selection input with newly created paths or environment variables"),
                        uiOutput('sels'))
               ),
               fluidRow(
                 column(6,verbatimTextOutput('envs')),
                 column(6,verbatimTextOutput('sel_vals')))
      )))

  shinyApp(ui,server)

}

结果:
enter image description here

关于r - Sys.getenv() 在 Shiny Apps 中返回空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36853408/

相关文章:

r - 同一个ggplot中的多个线图

r - 创建数字序列,不包括某些数字

r - 求子集的均值

r - Shiny 的服务器崩溃

r - 根据用户输入选择fillColor

r - 如何在 Shiny 中隐藏条件面板?

c++ - R Makevars 文件覆盖 R CMD 的默认 g++ 选项?

r - 使用 lapply 更改数据框列表的列名

r - 我们如何配置shinyserver开源支持并发用户

r - 连接到特定的shiny session(实现跨浏览器登录)