在Shinyapp我有一个selectInput我可以选择一些值。然后我想绘制y~选择的值。我可以绘制一个定义的情节,如情节(mtcars $ mpg~mtcars $ wt),但我想绘制情节(mtcars $ mpg~选定值)
有谁能够帮我。我的代码是这样的:
library(shiny)
ui <- fluidPage(
titlePanel("MyPLot"),
sidebarLayout(
sidebarPanel(
selectInput("variable", "Variable:", c("Cylinders" = "cyl", "Transmission" = "am", "Gears" = "gear"))
),
mainPanel(
plotOutput("distPlot"),
plotOutput("secPlot")
)
)
)
server <- function(input, output) {
output$distPlot <- renderPlot({plot(mtcars$mpg~mtcars$wt) })
output$secPlot <- renderPlot({ plot(mtcars$mpg~input$variable) })
}
shinyApp(ui = ui, server = server)
也许您可以创建一个反应数据框,您可以在其中对mtcars进行子集化,然后使用renderPlot:
server <- function(input, output) {
output$distPlot <- renderPlot({plot(mtcars$mpg~mtcars$wt) })
df <- reactive({
df <- mtcars %>% select(mpg, input$variable)
})
output$secPlot <- renderPlot({
dfb <- df()
plot(dfb[, 1]~ dfb[, 2])
})
}
shinyApp(ui = ui, server = server)