I have an Excel file with several tabs. How can I run my shiny App looping through the tabs -- using one tab (one at a time) as input?
Here is a simple example. In reality, raw_input
is the Excel file with 3 tabs. In this code, out3() will only be run for tab1.
Final outcome I am looking for is Output.csv with 3 tabs:
Tab1 is out3() calculated using tab1
Tab2 is out3() calculated using tab2
Tab3 is out3() calculated using tab3
library(shiny)
ui <- fluidPage(
shiny::downloadButton('download_button', "Save")
)
server <- function(input, output, session) {
raw_input <- reactive({
df <- data.frame(tab1 = c(1, 2, 3),
tab2 = c(4, 5, 6),
tab3 = c(1, 1, 1)
})
# IN THIS EXAMPLE BELOW, IT ONLY RUNS FOR THE 1ST TAB
# I NEED TO RUN IT FOR EACH TAB
out1 <- reactive({
return(raw_input()[[1]]**2)
})
out2 <- reactive({
return(out1() * 5)
})
out3 <- reactive({
return(out1()+out2())
})
output$download_button <- shiny::downloadHandler(
filename = function() {paste("Output.csv")},
content = function(file) {
write.csv(out3(), file)
}
)
}
shinyApp(ui, server)
Do you need the results ouf out1 and out2 somewhere else or is this just to create out3.
In this case you can combine everything, instead creating a chain of reactives.
out3 <- reactive({
out1 = raw_input()[[1]]**2
out2= out1 * 5
out3= out1+out2
return(out3)
})
then you can repeat this for the 3 tabs:
out3_tab1 <- reactive({
out1 = raw_input()[[1]]**2
out2= out1 * 5
out3= out1+out2
return(out3)
})
out3_tab2 <- reactive({
out1 = raw_input()[[2]]**2
out2= out1 * 5
out3= out1+out2
return(out3)
})
As this would be an unnecessary duplication of code, better write this into a function that can be used multiple times.
calculate_out3 = function(data, tab {
out1 = data[[tab]]**2
out2= out1 * 5
out3= out1+out2
return(out3)
}
and call this in the reactive environment:
out3_tab1 = reactive({
calculate_out3(raw_input(), 1)
})
out3_tab2 = reactive({
calculate_out3(raw_input(), 2)
})
Something like that.
But with this solution your need to have 3 download buttons as well, one for each output.
In reality, the reactive objects have complex calculations and are use elsewhere (by many other functions/reactive objects).
Thank you for your input. If you / anyone have other suggestions, let me know. Thanks so much!
system
Closed
March 10, 2022, 10:44pm
4
This topic was automatically closed 54 days after the last reply. New replies are no longer allowed. If you have a query related to it or one of the replies, start a new topic and refer back with a link.