【问题标题】:Run R script after input in Shiny在 Shiny 中输入后运行 R 脚本
【发布时间】:2019-07-01 09:15:25
【问题描述】:

大家早上好,

我有一个 Shiny 应用程序,它收集来自用户的 5 个输入并将它们存储到变量中。

然后,我将能够使用另一个基于用户提供的信息运行的 R 脚本。

这是我的 Shiny App 的示例:

jscode <- "shinyjs.closeWindow = function() { window.close(); }"

#Define UI for application

ui <- pageWithSidebar(
  #App title
  headerPanel("Filters applied for Powerpoints"),

   #Panel to display the filters
  sidebarPanel(
    #Select dates
    dateInput(inputId = "startDate", label = "Start date : ", value = "2018-12-01", format = "yyyy/mm/dd"),
    dateInput(inputId = "endDate", label = "End date : ", value = "2018-12-31", format = "yyyy/mm/dd"),

    #Select brand template
    selectInput("Brand", label = "Select brand : ", choices = list("Carat" = "Carat", "Amplifi" = "Amplifi", "iProspect" = "iProspect", "Isobar" = "Isobar")),

    #Select medium type
    selectInput("Medium", label = "Select medium type : ", choices = list("Social Post" = "Social Post", "Display" = "Display", "Programmatic" = "Programmatic", "SEA" = "SEA")),

    #Enter the plan ID of your campaign
    textInput("Camp", label = "Enter the plan ID of your campaign : ", value = ""),

    #Button to close the window, then run script
    useShinyjs(),
    extendShinyjs(text = jscode, functions = c("closeWindow")),
    actionButton("close", "Close and run")
  ),
  mainPanel()
)

#Define server logic
server <- function(input, output, session){
  observe({
    startDate <<- input$startDate
    endDate <<- input$endDate
    brand <<- input$Brand
    medium <<- input$Medium
    campaign <<- input$Camp
  })
  observeEvent(input$close, {
    js$closeWindow()
    stopApp()
  })
  source("C:/Users/RPeete01/Desktop/Automated powerpoints/Datorama R/Datorama reporting R/DatoramaSocial.R")
}


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

我使用了源函数,但它不起作用。

如果有人有想法,请告诉我。

非常感谢,

雷米

【问题讨论】:

  • 你能把你的源放在顶部而不是服务器上试试吗?
  • 在你的脚本末尾写上source("C:/Users/RPeete01/Desktop/Automated powerpoints/Datorama R/Datorama reporting R/DatoramaSocial.R"),在shinyApp(ui = ui, server = server)之后

标签: r shiny


【解决方案1】:

您应该利用闪亮的内置onStop函数在stopApp()调用之前执行一些函数

library(shiny)
if (interactive()) {
  # Open this application in multiple browsers, then close the browsers.
  shinyApp(
    ui = basicPage("onStop demo",actionButton("close", "Close and run")),

    server = function(input, output, session) {
      onStop(function() cat("Session stopped\n"))

      observeEvent(input$close, {
        stopApp()
      })
    },

    onStart = function() {
      cat("Doing application setup\n")

      onStop(function() {
        cat("Doing application cleanup, your functions go here\n")
      })
    }
  )
}

【讨论】:

    【解决方案2】:

    您的脚本DatoramaSocial.R 应该被表述为一个将您的 5 个输入值作为参数的函数。至于返回值,你还没有告诉我们你想用它做什么。通过将其公式化为一个函数,我的意思是将 DatoramaSocial.R 中的所有内容包装在一个函数(或几个子函数)中。该函数的代码可以轻松地驻留在外部脚本文件中,也可以粘贴在闪亮应用程序中的uiserver 语句之前。如果是前者,只需在您的 uiserver 语句中调用 source('DatoramaSocial.R')before 来包含定义。

    现在,在您的 server 函数中,您可以简单地调用它作为对输入变化的反应:

    observe({
      DatoramaSocial(input$startDate, input$endDate, input$Brand, input$Medium, input$Camp)
    })
    

    尽管在这种情况下,我建议插入 actionbuttonInput 并让用户在选择所有输入后单击它。在这种情况下,更新为:

    observeEvent(input$actionbutton, ignoreInit=TRUE, {
      DatoramaSocial(input$startDate, input$endDate, input$Brand, input$Medium, input$Camp)
    })
    

    actionbutton 是操作按钮的 inputId。

    【讨论】:

    • 感谢您的回答!我创建了函数和按钮,但随后出现错误提示“找不到对象响应”。你知道这可能来自哪里吗?谢谢!
    【解决方案3】:

    您可以通过为local 选项提供环境来获取脚本,而不是创建一个函数来替换您的脚本。此环境必须包含脚本所需的对象。类似的东西:

    mylist <- reactiveVal() # we will store the inputs in a reactive list
    
    observe({ # create the list
      mylist(list(
        startDate = input$startDate,
        endDate = input$endDate,
        brand = input$Brand,
        medium = input$Medium,
        campaign = input$Camp))
    })
    
    observeEvent(input$runScript, { # "runScript" is an action button
      source("myscript.R", local = list2env(mylist()))
    })
    

    编辑

    这是一个完整的例子。

    library(shiny)
    
    ui <- fluidPage(
      textInput("text", "Enter text", value = "test"),
      actionButton("runScript", "Run")
    )
    
    server <- function(input, output, session) {
    
      mylist <- reactiveVal() # we will store the inputs in a reactive list
    
      observe({ # create the list
        mylist(list(
          text = input$text))
      })
    
      observeEvent(input$runScript, { # "runScript" is an action button
        source("myscript.R", local = list2env(mylist()))
      })
    
    }
    
    shinyApp(ui, server)
    

    文件 myscript.R:

    writeLines(text, "output.txt")
    

    当我运行应用程序并单击按钮时,文件 output.txt 已正确创建(即脚本来源正确)。

    【讨论】:

    • 感谢您的回答!我尝试了您的解决方案,但是单击操作按钮时,没有任何反应。仍然需要调查。
    • @RémiPts 很奇怪。并使用eventReactive 而不是observeEvent ?
    • 一样,没什么新意 :(
    • @RémiPts 我在回答中添加了一个示例。对我来说,它非常有效。你的脚本应该做什么?
    • @StéphaneLaurent 我有同样的问题,非常感谢您的帮助。提前感谢stackoverflow.com/questions/59129562/…
    猜你喜欢
    • 1970-01-01
    • 2016-09-06
    • 2014-09-14
    • 2018-02-26
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-05
    相关资源
    最近更新 更多