【问题标题】:Use a variable taken from ShinyApp in a Rscript在 Rscript 中使用从 ShinyApp 获取的变量
【发布时间】:2021-09-12 18:55:17
【问题描述】:

我想在 Shiny 应用程序中插入变量的值并从 R 脚本中调用它,但我不知道如何修改脚本(“myscript.R”)以接收变量( “文本”)来自应用程序。

这是我的代码示例:

library(shiny)
library(shinyBS)

source("myscript.R", local = TRUE)

用户界面

ui <- fluidPage(
   
  wellPanel(
   fluidRow(
    textInput(inputId = "text", label = "Insert your name...", value = ""),
    actionButton("runScript", "Run")
    ),
  ),
)

服务器

server <- function(input, output, session) {
  
  mylist <- reactiveVal()
  
  observe({ 
    mylist(list(
      text = input$text))
    })
  
  observeEvent(input$runScript, { 
    source("myscript.R", local = list2env(mylist()))
  })
}

R 脚本

myscript <- function(text) 
{ 
  myname <- text
  print(myname)   # I have simplified my code...The use of the variable "text" is more complex
}

非常感谢您的帮助!

【问题讨论】:

    标签: r variables input shiny reactive


    【解决方案1】:

    在服务器外部获取你的 myscript.R 程序,然后调用observeEvent(...) 内部的函数,如下所示。

    ui <- fluidPage(
      
      wellPanel(
        fluidRow(
          textInput(inputId = "text", label = "Insert your name...", value = ""),
          actionButton("runScript", "Run")
        ),
      ),
    )
    
    source("myscript.R", local = TRUE)
    
    server <- function(input, output, session) {
      
      mylist <- reactiveVal()
      
      observe({ 
        mylist(list(
          text = input$text))
        #print(mylist())
      })
      
      observeEvent(input$runScript, { 
        myscript(mylist())
      })
    }
    
    shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 2019-06-18
      • 1970-01-01
      • 1970-01-01
      • 2016-04-15
      • 1970-01-01
      • 1970-01-01
      • 2016-05-13
      • 1970-01-01
      • 2019-11-20
      相关资源
      最近更新 更多