【问题标题】:R shinyapps plot according to choice in SelectInputR shinyapps 根据 SelectInput 中的选择进行绘图
【发布时间】:2018-05-30 14:16:24
【问题描述】:

在 Shinyapp 中,我有一个 selectInput,我可以在其中选择一些值。然后我想绘制 y~selected 值。 我可以绘制像 plot(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)

【问题讨论】:

标签: r variables plot shiny


【解决方案1】:

也许您可以创建一个反应式数据框,您可以在其中对 mtcar 进行子集化,然后使用 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-20
    • 2019-10-07
    • 2016-01-10
    • 2014-10-27
    • 2014-12-22
    • 2021-11-26
    • 1970-01-01
    相关资源
    最近更新 更多