Reputation: 1
I am new to shiny and I am having difficulty assigning the proper output based on multiple user inputs. I am attempting to create an app to assign the weight (output) of a fish species (input 1) at a known length (input 2) using a previously generated matrix. I am also trying to display a plot for the species length ~ weight relationship with a point showing the length/ weight chosen. I believe I have most of the ui how I would like it, but I am struggling with the server code and reactivity in general:
ui= fluidPage(
titlePanel("CAWS Fish Length ~ Weight Calculator"),
br(),
fluidRow(
# Drop down menu for species of interest.
column(4, selectInput("Species", "Species",
c("Banded killifish",
"Bluegill",
"Bluntnose minnow",
"Carp",
"Emerald shiner",
"Fathead minnow",
"Gizzard shad",
"Golden shiner",
"Goldfish",
"Green sunfish",
"Largemouth bass",
"Mosquitofish",
"Pumpkinseed",
"Rockbass",
"Smallmouth bass",
"Spotfin shiner",
"Yellow bullhead",
"Yellow perch"))),
# Slide bar for total length of interest.
column(4, sliderInput("Total.Length..mm.", "Total Length(mm):",
min= 0, max= 1000, value= 0, step= 1)),
# Output box for estimated weight.
column(4, verbatimTextOutput("Weight"),
p("The estimated weight (g) of your species",
"at the chosen total length (mm).")),
# Plot for length ~ weight relationships
plotOutput("plot1"))
)
server= function(input, output, session) {
re= eventReactive(
input$Total.Length..mm., {input$Species}
)
output$Weight= renderText({
re()
})
# Creating a nice pretty plot to display regressions
output$plot1= renderPlot({
ggplot(MWRD_LW, aes(x= Total.Length..mm., y= Weight..g.)) +
geom_point(aes(color= factor(Species))) +
xlab("Total Length (mm)") +
ylab("Weight (g)") +
labs(color= "Species") +
scale_x_continuous(expand= c(0, 0), limits= c(0, 1001),
breaks= c(100, 200, 300, 400, 500, 600,
700, 800, 900, 1000)) +
scale_y_continuous(expand= c(0, 0), limits= c(0, 15100),
breaks= c(1000, 2000, 3000, 4000, 5000,
6000, 7000, 8000, 9000, 10000,
11000, 12000, 13000, 14000,
15000)) +
theme_bw() +
theme(plot.background= element_blank(),
panel.grid.major= element_blank(),
panel.grid.minor= element_blank(),
axis.title= element_text(size= 14),
axis.text= element_text(size= 10))
})
}
shinyApp(ui, server)
So far I have not been successful in assigning the proper weight for a species at the selected length. I also have only generated a plot displaying all species length ~ weight relationships. Any and all help is greatly appreciated.
Upvotes: 0
Views: 66
Reputation: 297
Here in your code I could see 2 inputs:
"Species" "Total.Length..mm."
Then in the output panel you could write functions corresponding to them just by calling "input$" + the names:
input$Species input$Total.Length..mm.
Say, if you have two inputs named "Length" and "Weight", here may be something you would like to have:
output$plot1= renderPlot({
ggplot(MWRD_LW, aes(x= input$Length, y= input$Weight)) +...
Upvotes: 0