【问题标题】:Execute script after Shiny App inputs have been entered输入 Shiny App 输入后执行脚本
【发布时间】:2020-08-11 22:40:40
【问题描述】:

我对 Shiny 比较陌生,正在尝试创建一个应用程序来创建一个全局变量,然后我可以将其传递给 R 脚本。这是我的 Shiny App 的示例:

library(shiny)
 
ui <- fluidPage(
  titlePanel("Hello"),
  br(),
  h3("Welcome"),
  
  sidebarLayout(
    
    sidebarPanel(
      h1("Enter parameter value"),
      selectInput("datamodel", "Update Data Model",
                  choices = c("Yes", "No"),
                  selected = "No")
    ),
    
    mainPanel(
      h1("Output")
      
    )
  ))

# Define server logic
server <- function(input, output) {
  
  observe({
    Update_Data_Model <<- input$datamodel
  })  
  
  source("Authentication.R")
  
}


# Run the application shinyApp(ui = ui, server = server) 

UI 已创建,我可以创建一个名为 Update_Data_Model 的全局变量以传递给名为 Authentication.R 的 R 脚本strong>,但是在我有时间在 UI 中输入输入变量之前,R 脚本就会运行。

在 UI 中输入输入变量后,是否可以执行 R 脚本?

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    您可以在输入本身上使用 observeEvent,因此它只会在输入更改时执行:

    library(shiny)
    
    ui <- fluidPage(
      titlePanel("Hello"),
      br(),
      h3("Welcome"),
    
      sidebarLayout(
    
        sidebarPanel(
          h1("Enter parameter value"),
          selectInput("datamodel", "Update Data Model",
                      choices = c("Yes", "No"),
                      selected = "No")
        ),
    
        mainPanel(
          h1("Output")
    
        )
      ))
    
    # Define server logic
    server <- function(input, output) {
    
        observeEvent(input$datamodel, {
    
            Update_Data_Model <<- input$datamodel
    
            if(input$datamodel == 'Yes') {
    
                source("Authentication.R")
    
            }
    
        })
    
    }
    
    shinyApp(ui = ui, server = server)
    

    考虑使用操作按钮而不是下拉菜单,它对您和您的用户来说可能更直观:

    library(shiny)
    
    ui <- fluidPage(
    
        actionButton("mybutton", "Update Data Model")
    
    )
    
    server <- function(input, output) {
    
        observeEvent(input$mybutton, {
    
            source("Authentication.R")
    
        })
    
    }
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

    • 谢谢。我将如何使用操作按钮?你能像上面那样提供一个使用观察事件的例子吗?
    • @SteveM 我已经更新了上面的答案,包括一个使用操作按钮的答案。
    猜你喜欢
    • 2019-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-22
    • 2016-08-14
    • 2018-02-18
    • 1970-01-01
    • 2017-02-23
    相关资源
    最近更新 更多