如何根据多个用户输入对闪亮的输出进行编程

时间:2019-07-26 20:09:33

标签: r shiny reactive

我是新手,我很难根据多个用户输入来分配适当的输出。我试图创建一个应用程序,使用先前生成的矩阵以已知长度(输入2)分配鱼类(输入1)的重量(输出)。我还试图显示物种长度与重量之间的关系,并用点表示所选的长度/重量。我相信大部分ui都符合我的要求,但我在服务器代码和反应性方面一直处于挣扎状态:

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)

到目前为止,我还没有成功为选定长度的物种分配适当的权重。我还只生成了一个显示所有物种长度与重量关系的图。任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

在您的代码中,我可以看到2个输入:

"Species" "Total.Length..mm."

然后,在输出面板中,只需调用“ input $” +名称即可编写与它们相对应的函数:

input$Species  input$Total.Length..mm.

说,如果您有两个输入分别为“ Length”和“ Weight”,则可能需要以下输入内容:

output$plot1= renderPlot({
ggplot(MWRD_LW, aes(x= input$Length, y= input$Weight)) +...