【问题标题】:Saving user defined variables and running R scipt in Shiny保存用户定义的变量并在 Shiny 中运行 R 脚本
【发布时间】:2021-06-03 08:51:38
【问题描述】:

我有一个闪亮的应用程序,它可以全局保存一些变量。我希望用户能够单击“运行”按钮,这将 1)全局保存变量,2)运行使用这些变量的 R 脚本。

下面是我所在的位置,但在点击按钮之前我无法保存变量。

library(shiny)

ui <- fluidPage(
      
column(4, wellPanel(dateInput('date', label = 'Date input: yyyy-mm-dd', value = Sys.Date()))),
column(4, wellPanel(numericInput('STD', 'STD', 1.2))),
  
actionButton("Run", "Run the tool")

)

server <- function(input, output) {
  
  observeEvent(input$STD, {
    STDShiny <<- input$STD1
  })

  observeEvent(input$date, {
    dateShiny <<- input$date
  })
  
  observeEvent(input$Run, {
    source("someScript.R")
  })
}

示例脚本:someScript.R

dir.create(paste(date,STD, sep = ''))

感谢任何帮助。

【问题讨论】:

  • 有什么理由不能source 使用闪亮的输入作为参数的函数吗?那会简单得多

标签: r shiny


【解决方案1】:

Somescript.R 代码:

dir.create(paste(.GlobalEnv$dateShiny, .GlobalEnv$STDShiny, sep = ''))

Shinyapp:

library(shiny)
library(tidyverse)

ui <- fluidPage(
    
    column(4, wellPanel(dateInput('date', label = 'Date input: yyyy-mm-dd', value = Sys.Date()))),
    column(4, wellPanel(numericInput('STD', 'STD', 1.2))),
    
    actionButton("Run", "Run the tool") #The button to trigger script
    
)

server <- function(input, output) {
 
 #Upon clicking in the button the following code gets executed  
 observeEvent(input$Run,{
     
        #declare as variables in the global env with the values of the inputs
        walk2(c('STDShiny', 'dateShiny'), c(input$STD, input$date), ~{
            assign(..1, ..2, envir = .GlobalEnv)
        })
    
        #Run the script
        exec(source, file = 'someScript.R')

})}


shinyApp(ui, server)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-17
    • 1970-01-01
    • 2019-07-01
    • 1970-01-01
    • 2014-04-10
    • 1970-01-01
    • 2014-09-14
    • 1970-01-01
    相关资源
    最近更新 更多