简体   繁体   中英

changing variables of a separate R script in a shiny app

I have a set of scripts which are run from below, with aspects of the final output influenced by lines 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)

I'd like to be able to change the input year, input month, dataType and the name of the workbook produced by the source() 1-5, from a shiny app, and then run the respective files and generate the excel file.

So far I have the following code, which does not produce any errors, but does not function as desired. I have only included the 'server' section of the code to save space, and this is the part I need help with if possible;

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)

How can I alter the server script to get the desired functionality?

edit: Full script added, sourced names removed

You'll somehow need to trigger the execution of your reactive code. Reactive code only is executed if it was invalidated. Please see this for further information.

In the following app the code will be executed once the Save Workbook button is clicked. I don't know your UI and sourced R-scripts, so you might want to replace here accordingly:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM