I'm fairly new to Rshiny and looking for some help to understand how to create a plot using slider values as input. The user selected slider values are displayed as a table, and used as inputs to calculate an equation. The resulting calculated values are stored in a table (if possible I'd like to be able to download the generated values as a csv file) and used to generate a simple plot. This is what I have so far:
ui <- fluidPage(titlePanel(p("title", style = "color:#3474A7"),
sidebarLayout(
sidebarPanel(
sliderInput("Max", "Max:",
min = 0, max = 1000,
value = 116.8, step=0.1),
sliderInput("Rate", "Rate:",
min = 0, max = 5,
value = 0.12, step=0.01),
sliderInput("Inflection", "Inflection:",
min = 0, max = 20,
value = 11.06, step=0.01),
sliderInput("Area", "Area:",
min = 0, max = 10000,
value = 180, step=20),
p("Made with", a("Shiny",
href = "http://shiny.rstudio.com"), "."),
),
mainPanel(
# Output: Table summarizing the values entered ----
tableOutput("values"),
plotOutput("plot")
)
)
)
#use the slider values to estimate growth for any given year using the equation
Growth = function(x, A, B, C, R)
{R *(A *exp(-B * C^x))}
#create a Table Ouptut with selected Slider values
server <- function(input, output){
# Reactive expression to create data frame of all input values ----
sliderValues <- reactive({
data.frame(
Name = c("Max",
"Inflection",
"Rate",
"Area"),
Value = as.character(c(input$Max,
input$Inflection,
input$Rate,
input$Area)),
stringsAsFactors = FALSE)
})
# Show the values in an HTML table ----
output$values <- renderTable({
sliderValues()
})
#reactive expression to let users download the data as a table
##restC <- reactive({
#run the code for all time with the selected parameters from the slider
mylist<-list(c(1:50))
mydata<-data.frame(lapply(mylist, Growth, A=values$Max, B=values$Rate, C=values$Inflection, R=values$Area),mylist)
names(mydata)[1]<-"Pop"
names(mydata)[2]<-"Time"
#output$my_table<-renderDataTable({
#restC()
# })
#plot the values in a graph
output$plot <- renderPlot({
ggplot(mydata, aes(Time,Pop)) + geom_line()
})
}
shinyApp(ui = ui, server = server)
Made some tweakings in your code; now it does what you want:
library(shiny)
library(ggplot2)
ui <- fluidPage(titlePanel(p("title", style = "color:#3474A7")),
sidebarLayout(
sidebarPanel(
sliderInput("Max", "Max:",
min = 0, max = 1000,
value = 116.8, step=0.1),
sliderInput("Rate", "Rate:",
min = 0, max = 5,
value = 0.12, step=0.01),
sliderInput("Inflection", "Inflection:",
min = 0, max = 20,
value = 11.06, step=0.01),
sliderInput("Area", "Area:",
min = 0, max = 10000,
value = 180, step=20),
downloadButton("download", "Download data"),
p("Made with", a("Shiny",
href = "http://shiny.rstudio.com"), "."),
),
mainPanel(
# Output: Table summarizing the values entered ----
tableOutput("values"),
plotOutput("plot")
)
)
)
#use the slider values to estimate growth for any given year using the equation
Growth = function(x, A, B, C, R)
{R *(A *exp(-B * C^x))}
#create a Table Ouptut with selected Slider values
server <- function(input, output){
# Reactive expression to create data frame of all input values ----
sliderValues <- reactive({
data.frame(
Name = c("Max",
"Inflection",
"Rate",
"Area"),
Value = as.character(c(input$Max,
input$Inflection,
input$Rate,
input$Area)),
stringsAsFactors = FALSE)
})
# Show the values in an HTML table ----
output$values <- renderTable({
sliderValues()
})
#reactive expression to let users download the data as a table
restC <- reactive({
#run the code for all time with the selected parameters from the slider
mylist<-list(c(1:50))
mydata<-data.frame(lapply(mylist, Growth, A=input$Max, B=input$Rate, C=input$Inflection, R=input$Area),mylist)
names(mydata)[1]<-"Pop"
names(mydata)[2]<-"Time"
mydata
})
#output$my_table<-renderDataTable({
#restC()
# })
#plot the values in a graph
output$plot <- renderPlot({
ggplot(restC(), aes(Time,Pop)) + geom_line()
})
output$download <- downloadHandler(
filename = function() {
paste("data-", Sys.Date(), ".csv", sep="")
},
content = function(file) {
write.csv(restC(), file)
}
)
}
shinyApp(ui = ui, server = server)