gpt4 book ai didi

r - 在 Shiny 中保存用户定义的变量并运行 R 脚本

转载 作者:行者123 更新时间:2023-12-04 07:34:17 24 4
gpt4 key购买 nike

我有一个 Shiny 的应用程序,可以全局保存一些变量。我希望用户能够单击“运行”按钮,这将 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 = ''))
任何帮助表示赞赏。

最佳答案

Somescript.R 代码:

dir.create(paste(.GlobalEnv$dateShiny, .GlobalEnv$STDShiny, sep = ''))
新应用:
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)

关于r - 在 Shiny 中保存用户定义的变量并运行 R 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67813619/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com