注意: 我编辑了问题,因为使用PG_HOST引起混淆,但实质是一样的
我正在运行一个需要读取一些环境变量的闪亮应用程序。
该变量在闪亮服务器启动之前定义。例如
export APPLE=apple
export PENCIL=pencil
Run Code Online (Sandbox Code Playgroud)
在global.R(或以server.R相同的开头)中,我编写了以下代码:
manzana <- Sys.getenv('APPLE')
lapiz <- Sys.getenv('PENCIL')
Run Code Online (Sandbox Code Playgroud)
但这些变量为空。
如果我在R控制台中运行该代码,则两者均返回正确的值。
这是行不通的吗?Whay与R控制台和闪亮的应用程序不同吗?我怎样才能得到真正的环境变量(在这个例子假冒$APPLE和$PENCIL)?哪种配置闪亮应用程序的正确方法?
Car*_*eri -1
第一步是了解反应性。查看闪亮的教程。
使用您的示例..有点..这是一个应用程序,可以更新和设置可以通过多种方式调用的变量....
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)
}
Run Code Online (Sandbox Code Playgroud)