aiorr
aiorr

Reputation: 599

Shiny - importing and exporting without user interaction by only specifying one file and its path

All the shiny tutorials I see import multiple data manually via fileInput() then export manually.

Currently, I just have a single R script files that I manually change the few variables each time I run it.

For example, at directory C:/Users/Users/Project/000-0000, I want to update 000-0000_result1 and 000-0000_result2 using information from 000-0000_NewData.

#### Variables I change
file_name <- "C:/Users/Users/Project/000-0000/000-0000_NewData.csv"
parameterNum <- 3

#### Rest of the codes that I never change
setwd(dirname(file_name)
projectID <- str_extract(file_name, "[^_]+") #would be 000-0000 in this case
dat0 <- read_csv(file_name)
prev_result1 <- read_csv(str_c(projectID, "_result1"))
prev_result2 <- read_csv(str_c(projectID, "_result2"))
... #data step using parameterNum
write_csv(new_result1, str_c(projectID, "_result1"))
write_csv(new_result2, str_c(projectID, "_result2"))

I want to create a Shiny app where I can just specify the file_name with fileInput("dat0","Upload a new data") and numericInput() then run the rest of the script. I do not want to manually select multiple files then export them, because I have a lot of _result files mixed with other files sharing the same filetypes.

I was looking at input$dat0$datapath but it seems that shiny creates a tmp folder with only files loaded through fileInput()

Is my plan possible using Shiny? I am using flexdashboard, but I also welcome and will try to adjust standard Shiny answer on my own.

Upvotes: 0

Views: 322

Answers (1)

jpdugo17
jpdugo17

Reputation: 7116

Perhaps something like this:

library(shiny)
library(tidyverse)

ui <- fluidPage(
  textInput('file_name', 'Path to filename', value = "C:/Users/Users/Project/000-0000/000-0000_NewData.csv"),
  numericInput('parameterNum', 'Insert Parameter Number',value = 3, min = 0),
  actionButton(inputId = 'save', label = 'Write csvs')
)

server <- function(input, output, session) {
  
  observe({
    setwd(dirname(input$file_name))
  })
  
  projectID <- reactive({
      str_extract(inpt$file_name, "[^_]+")
  })
  
  prev_result1 <- reactive({
    read_csv(str_c(projectID(), "_result1"))
    #some calculation
  })
  
  prev_result2 <- reactive({
    read_csv(str_c(projectID(), "_result2"))
    #some calculation
  })
  
  observeEvent(input$save, {
    write_csv(prev_result1(), str_c(projectID(), "_result1"))
    write_csv(prev_result2(), str_c(projectID(), "_result2"))
  })

}

shinyApp(ui, server)

Upvotes: 1

Related Questions