In ui.R function, I upload a CSV file that has 10 rows. In server.R function, I want to apply my function (called myFun) to the input data and another data (read from "data2.csv" file which was created in advance using complicated R code) repeatedly. I need help in: (1) how to read the "data2.csv" file in server.R? (2) how to call function myFun repeatedly without writing the same function 10 times? I heard that global.R file could do the trick. But I don't know how to do it. Could someone help?
Here is the example:
library(shiny)
ui <- shinyUI(fluidPage(
titlePanel("upload your input data"),
sidebarLayout(
sidebarPanel(
shinyFeedback::useShinyFeedback(),
fileInput("file1","Upload CSV file", accept = c(".csv"), multiple = T), # fileinput() function is used to get the file upload control option
),
mainPanel(
uiOutput("tb"),
verbatimTextOutput(outputId = "warning1"),
textOutput("warning2")
)
)
))
server <- shinyServer(function(input,output) {
output$filedf1 <- renderTable({
input$file1 # the file input data frame object that contains the file attributes
})
data.chg <- reactive({
data.input<- read.csv(input$file1$datapath)
data.global<- read.csv("data2.csv")
data.output<- data.input
for (i in 1:nrow(data.input)){
data.output[i,1]<- myFun(data.input[i,1], data.output[i,1])
}
list(data.input= data.input, data.output=data.output)
})
output$data.origin <- renderDataTable({
req(input$file1)
dataset<- data.chg()$data.input
dataset
})
output$results <- renderDataTable({
req(input$file1)
dataset<- data.chg()$data.output
dataset
})
output$tb <- renderUI({
req(input$file1)
tabsetPanel(
tabPanel("input data", dataTableOutput("data.origin")),
tabPanel("Output", dataTableOutput("results"))
)
})
})
# Run the application
shinyApp(ui = ui, server = server)