【问题标题】:R Shiny: how to use a dataframe variable from server.R in ui.RR Shiny:如何在 ui.R 中使用 server.R 中的数据框变量
【发布时间】:2017-03-02 01:04:12
【问题描述】:

我正在尝试使用 ui.R 中 server.R 的 values$df 数据框变量将数据框的所有字段名称显示为侧面板中的复选框。但我收到一条错误消息,提示 错误:找不到对象“值”

这是我在 server.R 文件中的内容:

  values<- reactiveValues() 
  values$df<- data.frame() # creates an empty dataframe

  # actionButton 
  mdf<- eventReactive(input$click_counter, {
    name<- input$name
    gender<- input$gender
    college<- input$college
    team<- input$team
    score<- input$score

    new_row<- data.frame(name,college,gender,team,score)

    return(new_row)
  })

  observeEvent(input$click_counter, {
    name<- input$name
    gender<- input$gender
    college<- input$college
    team<- input$team
    score<- as.numeric(input$score) # convert to numeric here to make sorting possible
    rank<- 0


    new_row<- data.frame(rank,name,college,gender,team,score)


    values$df<- rbind(values$df, new_row)
    values$df<- values$df[order(-values$df$score),]
    values$df$rank<- 1:nrow(values$df)
  })

  output$nText<- renderDataTable({
    mdf()
  })

  output$nText2<- renderDataTable({
    values$df
  }, options = list(orderClasses = TRUE,lengthMenu = c(5, 10, 30), pageLength = 5))

这就是我在 ui.R 文件中的内容:

sidebarLayout(
  sidebarPanel(
    checkboxGroupInput('nText2',
                       'Columns in players to show:',
                       names(values$df),
                       selected = names(values$df))
  ),

【问题讨论】:

  • 你没有在你的代码中初始化values,所以它不能运行rbind(values$df, new_row)
  • 我还建议您使用renderUI()checkboxGroupInput() 移动到服务器端,然后在用户界面中调用htmlOutput("nText2")。这将允许 UI 依赖于服务器端操作的结果。在 UI 中从服务器调用响应值 values$df 通常不是 UI 与服务器交互的方式。
  • 您正在尝试访问在反应函数之外定义的values。在 UI 端尝试isolate(values$df)

标签: r shiny


【解决方案1】:

我不太确定您何时分配 values 对象。但是,如果您还没有使用 global.R 文件,我建议使用一个。您可以在其中指定values,并且该对象将在server.Rui.R 中可用。将 global.R 放在与其他两个文件相同的文件夹中。

【讨论】:

    【解决方案2】:

    让服务器呈现 UI 使您可以像处理其他服务器端操作一样继续使用数据框。如果没有可重现的数据框,我不能肯定这个数据框是否会起作用,但我希望这会给您一个很好的推动。

    服务器.R:

    output$nText2ui <- renderUI({checkboxGroupInput('nText2',
                              'Columns in players to show:',
                              names(values$df),
                              selected = names(values$df))
                             })
    

    ui.R:

    sidebarLayout(
      sidebarPanel(
       htmlOutput("nText2ui")
        )
      )
    

    【讨论】:

      猜你喜欢
      • 2017-03-06
      • 2014-07-13
      • 2014-06-05
      • 1970-01-01
      • 1970-01-01
      • 2014-07-12
      • 2017-10-22
      • 2016-07-15
      • 2013-04-01
      相关资源
      最近更新 更多