【问题标题】:Select dataframe in Shiny在 Shiny 中选择数据框
【发布时间】:2015-12-08 04:59:46
【问题描述】:

我对 Shiny 和 R 有一些运气,但我无法使用 selectInput 函数来更改数据框。我可能遗漏了一些明显的东西,但这是我的代码

require(shiny)

A <- data.frame(x=c(1,2,3),y=c(3,2,1))
B <- data.frame(x=c(1,1,5),y=c(3,5,0))

ui <- fluidPage(

selectInput("df", "Select dataframe", choices = c('A'='A','B'='B'), selected = 'A'),

plotOutput("Plot")

)

server <- function(input, output)
{

  df <- reactive({
 x <- as.data.frame(input$df)
  })

  output$Plot <- renderPlot({
  df <- df()
  plot(x=df$x, y=df$y)

})
}

shinyApp(ui = ui, server = server)

我错过了什么?

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    你不能使用as.data.frame和df的名字

    尝试使用get

    A <- data.frame(x=c(1,2,3),y=c(3,2,1))
    B <- data.frame(x=c(1,1,5),y=c(3,5,0))
    
    shinyServer(function(input, output) {
    
      df <- reactive({
        x <- get(input$df)
      })
    
      output$Plot <- renderPlot({
        df <- df()
        plot(x=df$x, y=df$y)
    
      })
    })
    

    【讨论】:

    • 就是这样!非常感谢!我很少在基础 R 中使用 get(),所以我完全忘记了。我试过 paste(),但不是 get()
    猜你喜欢
    • 1970-01-01
    • 2022-01-02
    • 2021-04-27
    • 1970-01-01
    • 2018-12-02
    • 2019-06-22
    • 1970-01-01
    • 1970-01-01
    • 2021-09-24
    相关资源
    最近更新 更多