在闪亮的应用程序中更改单独的R脚本的变量

问题描述 投票:0回答:1

我有一组脚本,这些脚本是从下面运行的,最终输出的各个方面受第2-4行的影响

setwd() 
inputyear = ""
inputmonth = ""
dataType = ""

source("1.R")
source("2.R")
source("3.R") 
source("4.R")
source("5.R")

#input required file name
saveWorkbook(wb, "Workbook.xlsx", overwrite = TRUE)

我希望能够从一个闪亮的应用程序中更改输入年份,输入月份,数据类型以及source()1-5生成的工作簿的名称,然后运行相应的文件并生成excel文件。

到目前为止,我有以下代码,该代码不会产生任何错误,但是不能按预期运行。我仅包括代码的“服务器”部分以节省空间,如果可能的话,这是我需要帮助的部分;

ui<-shinyUI(fluidPage(theme = shinytheme("flatly"),

                      tags$head(
                        tags$style(HTML(
                          ".shiny-output-error-validation {
                          color; green;
                          }
                          "))
                        ),

                      basicPage(
                        headerPanel("Workbook"),
                        sidebarPanel(
                          selectInput("inputmonth","Select Publication Month",c("JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC")),
                          selectInput("inputyear","Select Year",c("2018/19","2019/20","2020/21")),
                          selectInput("dataType","Select Version",c("provisional","final"))),
                        textInput("WorkBookName","Enter File Name (include .xlsx)"),
                        actionButton("Generate", "Generate Workbook"))
                        ))


server <- function(input, output, session){
  observeEvent(input$Generate, {
    validate(need(input$WorkBookName != "", "Please enter file name"))

    req(input$inputmonth, input$inputyear, input$dataType, input$WorkBookName)

    inputyear = input$inputmonth
    inputmonth = input$inputyear
    dataType = input$dataType

    source("1.R",local = TRUE)
    source("2.R", local = TRUE)
    source("3.R", local = TRUE) 
    source("4.R", local = TRUE)
    source("5.R", local = TRUE)


    saveWorkbook(wb, paste0(input$WorkBookName, ".xlsx"), overwrite = TRUE)
  })
}

shinyApp(ui, server)

如何更改服务器脚本以获得所需的功能?

编辑:已添加完整脚本,已删除源名称

r shiny
1个回答
0
投票

您将以某种方式触发您的反应式代码的执行。仅在无效代码时才执行反应性代码。有关更多信息,请参见this

在以下应用中,一旦单击Save Workbook按钮,将执行代码。我不知道您的UI和源R脚本,因此您可能需要在此处进行相应的替换:

library(shiny)
library(openxlsx)
library(shinythemes)

ui <- shinyUI(fluidPage(
  theme = shinytheme("flatly"),

  tags$head(tags$style(
    HTML(".shiny-output-error-validation {
                          color; green;
                          }
                          ")
  )),
  basicPage(
    headerPanel("Workbook"),
    sidebarPanel(
      selectInput(
        "inputmonth",
        "Select Publication Month",
        toupper(month.abb)
      ),
      selectInput("inputyear", "Select Year", c("2018/19", "2019/20", "2020/21")),
      selectInput("dataType", "Select Version", c("provisional", "final"))
    ),
    textInput("WorkBookName", "Enter File Name (include .xlsx)"),
    actionButton("Generate", "Generate Workbook"),
    uiOutput("test")
  )
))

server <- function(input, output, session) {
  observeEvent(input$Generate, {
    req(input$inputmonth,
        input$inputyear,
        input$dataType,
        input$WorkBookName)

    inputyear = input$inputmonth
    inputmonth = input$inputyear
    dataType = input$dataType

    # source("1.R", local = TRUE)
    # source("2.R", local = TRUE)
    # source("3.R", local = TRUE)
    # source("4.R", local = TRUE)
    # source("5.R", local = TRUE)
    # 
    # saveWorkbook(wb, paste0(input$WorkBookName, ".xlsx"), overwrite = TRUE)
    output$test <- renderUI("Everything fine...")
  })
}

shinyApp(ui, server)
© www.soinside.com 2019 - 2024. All rights reserved.