【问题标题】:attempt to apply non-function尝试应用非功能
【发布时间】:2018-06-09 03:52:47
【问题描述】:

我正在尝试构建一个简单的应用程序,该应用程序根据其他输入过滤的子集绘制所选变量的直方图。我在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】:

    您将简单的应用程序复杂化了。

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

    下面是一个完整的工作应用程序


    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)
    

    【讨论】:

      猜你喜欢
      • 2013-04-27
      • 1970-01-01
      • 1970-01-01
      • 2018-10-22
      • 2020-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-16
      相关资源
      最近更新 更多