2

I have a script called function.R which contains a function called my_function that generates an output.

my_function <- function(a, b, c) {
  # code
}

How can I get the user to input values for a,b,c in a r shiny app and then these inputs can be delivered to my function.R script. I want the shiny app to take in the inputs for each of these values (a,b,c) and then send it to my function.R script so it can show the output of my function in the r shiny app.

My r shiny app is

library(shiny)

source("/Users/havekqnsc/Desktop/function.R")

ui <- fluidPage(
  titlePanel("Function Output"),
  numericInput("a", "a", value = ""),
  numericInput("b", "b", value = ""),
  textInput("c", "c", value = ""),
  actionButton("Submit", "RunFunction")
)

server <- function(input, output) {
  observeEvent(input$Submit, {
    my_function()
  })
}

shinyApp(ui, server)
6
  • You need to pass the input values when calling the function observeEvent(input$Submit, {myfunction(a=input$a, b=input$b, c=input$c)}) Commented Jul 31, 2022 at 19:07
  • @MrFlick thank you! And then do I also need to have like an output variable or something? After that Commented Jul 31, 2022 at 19:09
  • What kind of output does it return? Yes you'd want to have some sort of place to put the output in your UI. Commented Jul 31, 2022 at 19:11
  • @MrFlick the final output of the function is a data frame, data table Commented Jul 31, 2022 at 19:14
  • 1
    Then you’re better to use a reactive that references the submit button (and the other inputs) rather than an observeEvent. That way, you can simply pass the reactive to a renderTable to display it. Commented Jul 31, 2022 at 19:40

1 Answer 1

2
  1. Call the function inside a reactive/eventReactive and store the result.
  2. Call the reactive inside a renderTable function.
library(shiny)

#example function
my_function <- function(a, b, c) {
  data.frame(a = a, b = b, c = c)
}
 
ui <- fluidPage(
  titlePanel("Function Output"),
  numericInput("a", "a", value = ""),
  numericInput("b", "b", value = ""),
  textInput("c", "c", value = ""),
  actionButton("Submit", "RunFunction"),
  tableOutput("result")
)

server <- function(input, output) {
  result <- eventReactive(input$Submit, {
    my_function(input$a, input$b, input$c)
  })
  
  output$result <- renderTable({
    result()
  })
}

shinyApp(ui, server)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.