试图申请非功能性

问题描述 投票:0回答:1

我正在尝试构建一个简单的应用程序,该应用程序根据由另一个输入过滤的子集绘制所选变量的直方图。我得到hist(dataX()$datasetInput())行中的错误,该错误应返回dataX$mpg。我该如何解决?完整代码:

library(shiny)
u <- shinyUI(pageWithSidebar(
  headerPanel("Staz w bezrobociu"),
  sidebarPanel(

    selectInput("variable", "Variable:",
                list("Milles/gallon",
                     "Horse power")
    ),
    textInput("nc","Number of cylinders",value = 6)
  ),

  mainPanel(
    plotOutput("Plot")
  )

))

s <- shinyServer(function(input, output) 
{
  dataX <- reactive({mtcars[mtcars$cyl==input$nc,,drop = FALSE]})

  datasetInput <- reactive({
    switch(input$variable,
           "Milles/gallon" = mpg,
           "Horse power" = hp)
  })

  output$Plot <- renderPlot({

    hist(dataX()$datasetInput())
  })

})
shinyApp(u,s)
r shiny
1个回答
0
投票

你复杂的简单应用程序。

  1. 您不需要列出selectInput中的所有列。您可以从服务器端渲染它。
  2. 同样适用于气缸
  3. usare这样的快捷方式是可以接受的,但只是坚持命名惯例。它让你的生活变得轻松

以下是一个完整的应用程序


library(shiny)
ui <- shinyUI(pageWithSidebar(
  headerPanel("Staz w bezrobociu"),
  sidebarPanel(uiOutput("SelectColname"),
               uiOutput("Cylinders")),
  mainPanel(plotOutput("Plot"))
))

server <- shinyServer(function(input, output){
  # Create a reactive dataset
  dataX <- reactive({
    mtcars
  })

  # Output number cylinders as select box
  output$Cylinders <- renderUI({
    selectInput("cylinders", "cylinders:", unique(dataX()$cyl))
  })

  # Output column names as selectbox
  output$SelectColname <- renderUI({
    selectInput("variable", "Variable:", colnames(dataX()[,c(1,4)]))
  })

  # Based on the selection by user, create an eventreactive plotdata object
  plotdata <- eventReactive(input$cylinders, {
    plotdata = dataX()[dataX()$cyl == input$cylinders, , drop = FALSE]
  })

  # Render the plot, the plot changes when new cylinder is selected
  output$Plot <- renderPlot({
    if (is.null(plotdata()))
      return(NULL)
    hist(
      plotdata()[, input$variable],
      xlab = input$variable,
      main = paste(
        "Histogram of" ,
        input$variable
      )
    )
  })

})
shinyApp(ui, server)
© www.soinside.com 2019 - 2024. All rights reserved.