Reputation: 21
I am trying to pass an R script to Shiny, the whole script are functions. My goal is to have radiobottons in the sidebar, to know what function to execute and in the main panel a button to initialize the funtion, the signs of life of the function that is being executed and the description of the function.
The description of the function if I get it to show, but I can not execute the function
library(shiny)
setwd("F:/Aplication PM10")
source("Functions.R")
ui <- fluidPage(
titlePanel("Calculate PM10"),
sidebarLayout(
sidebarPanel(
radioButtons("Index", h3("Chose the funtion you want to execute"),
choices = list("Pre-process" = 1,
"MAke a graph" = 2,
"Create PM10 image" = 3),
selected = 1)
),
mainPanel(
# Show a description o the function
textOutput("Case")
# button to initialize the funtion
#HERE show the signs of life of FUNCTION
)
)
)
server <- function(input, output) {
index<-reactive({input$Index})
output$Case<-renderText({
if (index()==1) {
print("Description Funtion 1")
} else if (index()==2){
print("Description Funtion 2")
}else if (index()==3){
print("Description Funtion 3")
}
})
#HERE I NEED EXECUTE THE FUNCTION
#if (index()==1) { function_1()} *something like this
#note: the funtion is a loop an print signs of life, that's what I want to show in the main panel
}
shinyApp(ui = ui, server = server)
I want a function to run while showing signs of life in the main panel. So far I have not even managed to run the function Note: some of these functions change directories and create files
Upvotes: 2
Views: 1608
Reputation: 163
(I would put this in a comment first, but unfortunately I can only answer as of right now.)
So a few things, that I am wondering:
1) Why did you introduce a function that gives the input$Index value? Just make sure that everything you intend to do with an input value is within an reactive context, i.e. wrap it around an observe, observeEvent, or access it while rendering output.
2) Why are you changing the directory at beginning? You can just give a path to the Function.R script in the source command.
Not sure if this is what you are looking for, but I was able to get the functions running with the following code
library(shiny)
source("Functions.R")
ui <- fluidPage(
titlePanel("Calculate PM10"),
sidebarLayout(
sidebarPanel(
radioButtons("Index", h3("Chose the funtion you want to execute"),
choices = list("hello" = 1,
"bye" = 2,
"testing" = 3),
selected = 1)
),
mainPanel(
# Show a description o the function
textOutput("Case")
# button to initialize the funtion
#HERE show the signs of life of FUNCTION
)
)
)
server <- function(input, output) {
output$Case<-renderText({
if (input$Index==1) {
"Description Funtion 1"
} else if (input$Index==2){
"Description Funtion 2"
} else if (input$Index==3){
"Description Funtion 3"
}
})
#HERE I NEED EXECUTE THE FUNCTION
observe({
if (input$Index==1) {
print(hello())
}
if (input$Index==2) {
print(bye())
}
if (input$Index==3) {
print(testing())
}
})
#note: the funtion is a loop an print signs of life, that's what I want to show in the main panel
}
shinyApp(ui = ui, server = server)
with a simple script Functions.R containing
hello <- function() {
"Hello World!"
}
bye <- function() {
"Bye World!"
}
testing <- function() {
"Testing the World!"
}
Hope this helps.
Upvotes: 3