Reputation: 2531
I was trying to follow suggestions given by a commenter (named: warmoverflow) in a question I posted yesterday: How to pass values from input boxes of shiny UI back into the variables in an R script and run it?
I decided to try the approach he proposed firstly in a short example. So, I created an R script mathops.R
containing a function with minor math operations. It is as follows:
mathops.R:
mathops <- function(a,b) {
print(paste0("Addition of two number is: ", a+b))
print(paste0("Multiplication of two number is: ", a*b))
}
I developed a UI with two text boxes from which input will taken for the above mentioned varaibles a
and b
and also an action button to display the output. It is as follows:
ui.R:
library(shiny)
shinyUI(fluidPage(
headerPanel("Inputting from Interface to Script"),
sidebarPanel(
#'a' input
textInput(inputId = "a",
label = h4("Enter a:"),
value = ""),
textInput(inputId = "b",
label = h4("Enter b:"),
value = ""),
actionButton(inputId = "input_action", label = "Show Inputs")),
mainPanel(
h2("Input Elements"))
textOutput("td"))
))
and now as he suggested, I was trying to develop the code for server.R
file:
server.R:
library(shiny)
source(mathops.R)
shinyServer(function(input, output) {
a <- eventReactive( input$input_action, {
input$a
})
b <- eventReactive( input$input_action, {
input$b
})
output$td <- renderDataTable({
mathops()
})
}
But here is where I'm facing an impasse. I just couldn't think of how to connect that mathops.R
script into server.R
file so that it will take the input coming from the input boxes on the UI and pass those values into the variables a
and b
of the mathops()
function in the R script shown above.
I'm new to shiny. What am I missing or misunderstanding here? How to solve this situation?
Please help!
Thanks.
Upvotes: 0
Views: 1617
Reputation: 12087
Here is a working version with some changes from your original code. As pointed out in the other answer, there are a few errors in your code. In addition, I changed textOutput to htmlOutput. In server.R, I put all code inside the observeEvent
environment.
In general, your method in your R script needs to return something suitable for process in server.R
. For example, in this case, it returns a string, which server.R
renders as text, and ui.R
in turn renders as HTML.
Any variable/data that is accessible in server.R
(including any files that it source
) can be displayed in the ui. What you need is (1) a suitable render method to render this data in server.R
(2) a suitable output container to hold the output display in ui.R
ui.R
library(shiny)
shinyUI(fluidPage(
headerPanel("Inputting from Interface to Script"),
sidebarPanel(
#'a' input
numericInput(inputId = "a",
label = h4("Enter a:"),
value = 3),
numericInput(inputId = "b",
label = h4("Enter b:"),
value = 4),
actionButton(inputId = "input_action", label = "Show Inputs")),
mainPanel(
h2("Input Elements"),
htmlOutput("td")),
# use dataTableOuput to display the result of renderDataTable. Just like the render methods, there are many output methods available for different kind of outputs. See Shiny documentation.
dataTableOutput("table")
))
server.R
library(shiny)
source("mathops.R")
shinyServer(function(input, output) {
observeEvent(input$input_action, {
a = input$a
b = input$b
output$td <- renderText({
mathops(a,b)
})
})
# Use renderDataTable to convert mat2 to a displayable object. There are many other render methods for different kind of data and output, you can find them in Shiny documentation
output$table <- renderDataTable(data.frame(mat2))
})
mathops.R
mathops <- function(a,b) {
return(paste0("Addition of two number is: ", a+b, br(), "Multiplication of two number is: ", a*b))
}
mat1 <- matrix(sample(1:16), ncol=4)
mat2 <- matrix(sample(1:25), ncol=5)
Upvotes: 2
Reputation: 6913
You had to do a couple things:
textInput
to numericInput
in your ui.R
mainPanel
arguments were not being parsed correctlyrenderPrint
instead of renderDataTable
if you are going to be using printed texta
and b
as reactive objects, but mathops
is just a regular function:Note: I moved the mathops
function definition into the server.R
for simplicity
ui.R
library(shiny)
shinyUI(fluidPage(
headerPanel("Inputting from Interface to Script"),
sidebarPanel(
numericInput(inputId = "a",
label = h4("Enter a:"),
value = ""),
numericInput(inputId = "b",
label = h4("Enter b:"),
value = ""),
actionButton(inputId = "input_action", label = "Show Inputs")),
mainPanel(
h2("Input Elements"),
textOutput("td")
)
))
server.R
library(shiny)
mathops <- function(a, b) {
print(paste0("Addition of two number is: ", a + b))
print(paste0("Multiplication of two number is: ", a * b))
}
shinyServer(function(input, output) {
a <- eventReactive( input$input_action, {
input$a
})
b <- eventReactive( input$input_action, {
input$b
})
output$td <- renderPrint({
mathops(a(), b())
})
})
Upvotes: 1