【问题标题】:How to access a column of a DataFrame from a passed variable in Shiny?如何从 Shiny 中传递的变量访问 DataFrame 的列?
【发布时间】:2015-07-30 23:56:55
【问题描述】:

我正在尝试创建一个简单的闪亮应用程序,用户可以在其中从下拉菜单中选择一个变量,然后生成一个图。所选变量在服务器函数中被视为 input$Feature,但在尝试将数据框的列作为 df$input$Feature 访问时出现错误。我不知道该怎么做。

bw <- read.xls('filename')

ui <- fluidPage(
  selectInput(inputId = 'Feature',
               label = 'Select a feature to plot:',
               c(colnames(bw))),
  plotOutput('graph')
)

server <- function(input, output){
  output$graph <- renderPlot({
    p <- ggplot(bw, aes(bw$Date))
    p <- p + geom_line(aes(y=bw$input$Feature, colour='red', group=1))
    p <- p + labs(x = 'Date', y = 'Feature Name')
    print(p)
  })
}

shinyApp(ui=ui, server=server)

【问题讨论】:

    标签: r ggplot2 shiny


    【解决方案1】:

    尝试在 geom_line 中使用它:

    bw[,input$Feature]
    

    如果没有可重现的示例,则无法提供更多帮助。

    编辑: 这对我有用:

    bw <- mtcars
    library(shiny)
    
    ui <- fluidPage(
      selectInput(inputId = 'Feature',
                   label = 'Select a feature to plot:',
                   c(colnames(bw)),
                   selected=colnames(bw)[1]),
      verbatimTextOutput('value'),
      plotOutput('graph')
    )
    
    server <- function(input, output){
    
      output$value <- renderPrint(columname())
      columname <- reactive({input$Feature})
      output$graph <- renderPlot({
        p <- ggplot(bw, aes(bw$qsec))+ geom_line(aes_string(y=bw[, input$Feature]))
        p <- p + labs(x = 'Date', y = 'Feature Name')
        p
      })
    }
    
    
    shinyApp(ui=ui, server=server)
    

    【讨论】:

    • “'[.data.frame'(bw, , input$Feature) 中的错误:找不到对象'input'”
    • 是的,你也拼错了“selecteInput”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-06
    • 2022-07-13
    • 1970-01-01
    • 2019-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多