'How to incorporate user defined functions into R shiny app
I am making a R shiny app that will import a csv file, run the table through a user defined function, and displays a plot and table. I'm not able to share the actual function, but have put an example function below for reference in the code.
fun <- function(dataset){
p_plot <- plot(dataset)
mm_table <- lm(y~x, data=dataset)
return (list(p_plot, mm_table))
}
dataset <- data.frame(x= 1:10, y=1:10)
fun(dataset)
So far, I have been able to make a shell for the app that has all the tabs and buttons I want.
library(shiny)
library(shinythemes)
library(dplyr)
ui <- shinyUI(fluidPage(
theme = shinytheme("readable"),
titlePanel("Plot Generator"),
tabsetPanel(
tabPanel("Upload File",
titlePanel("Upload Deisgn"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose CSV File',
accept=c('text/csv',
'text/comma-separated-values,text/plain',
'.csv')),
tags$br(),
checkboxInput('header', 'Header', TRUE),
radioButtons('sep', 'Separator',
c(Comma=',',
Semicolon=';',
Tab='\t'),
','),
actionButton("action", "Evaluate Design")
),
mainPanel(
tableOutput('contents')
)
)
),
tabPanel("Plot",
mainPanel(
plotOutput('p_plot'),
tableOutput('mm_table'),
)
)
)
)
)
server <- shinyServer(function(input, output, session) {
data <- reactive({
req(input$file1)
inFile <- input$file1
df <- read.csv(inFile$datapath, header = input$header, sep = input$sep,
quote = input$quote)%>% mutate(across(where(is.character), as.factor))
return(df)
})
output$contents <- renderTable({
data()
})
output$mm_table <- renderTable({
mm_table
})
output$p_plot <- renderPlot({
p_plot
})
})
shinyApp(ui, server)
I don't understand how to incorporate the user defined function I created. I want the evaluate design button to run the imported table through my function. Where do I put my function and then how can I call the return objects? I eventually want to create buttons that can change the default arguments, but I don't understand how to interact with the user defined function.
The output$mm_table and output$p_plot are place holders and I know it isn't correct. I don't know how to call return objects from a user defined function in general.
Any help with code, feedback, or online resources would be appreciated. Thanks.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
