Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R:如何在ui.R中使用server.R中的dataframe变量_R_Shiny - Fatal编程技术网

R:如何在ui.R中使用server.R中的dataframe变量

R:如何在ui.R中使用server.R中的dataframe变量,r,shiny,R,Shiny,我试图使用ui.R中server.R的values$dfdataframe变量在侧面板中显示dataframe的所有字段名作为复选框。但是我得到一个错误,说错误:找不到对象“值” 以下是我在server.R文件中的内容: values<- reactiveValues() values$df<- data.frame() # creates an empty dataframe # actionButton mdf<- eventReactive(inpu

我试图使用ui.R中server.R的values$dfdataframe变量在侧面板中显示dataframe的所有字段名作为复选框。但是我得到一个错误,说错误:找不到对象“值”

以下是我在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))

我不太确定您何时分配
对象。但是,如果您还没有使用全局.R文件,我建议您使用一个。您可以在其中分配
,对象将在
server.R
ui.R
中都可用。将global.R与其他两个文件放在同一文件夹中

我不太确定您何时分配
对象。但是,如果您还没有使用全局.R文件,我建议您使用一个。您可以在其中分配
,对象将在
server.R
ui.R
中都可用。将global.R与其他两个文件放在同一文件夹中

让服务器呈现UI允许您继续处理数据帧,就像处理其他服务器端操作一样。如果没有可复制的数据框架,我不能肯定这个数据框架是否会工作,但我希望这能给你一个很好的推动

服务器.R:

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

让服务器呈现UI允许您继续处理数据帧,就像处理其他服务器端操作一样。如果没有可复制的数据框架,我不能肯定这个数据框架是否会工作,但我希望这能给你一个很好的推动

服务器.R:

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

您尚未在代码中的任何位置初始化
值,因此它无法运行
rbind(值$df,new_row)
我还建议您使用
renderUI()
checkboxGroupInput()
移动到服务器端,然后在用户界面中调用
htmlOutput(“nText2”)
。这将允许UI依赖于服务器端操作的结果。从UI中的服务器调用响应值作为
值$df
通常不是UI与服务器交互的方式。您试图访问在响应函数外部定义的
值。在UI端尝试
隔离(values$df)
您的代码中任何地方都没有初始化
,因此它不能运行
rbind(values$df,new_row)
我还建议您使用
renderUI()
复选框groupinput()
移动到服务器端,然后只需调用
htmlOutput(“nText2”)
在用户界面中。这将允许UI依赖于服务器端操作的结果。从UI中的服务器调用响应值作为
值$df
通常不是UI与服务器交互的方式。您试图访问在响应函数外部定义的
值。尝试在UI端隔离(值$df)
sidebarLayout(
  sidebarPanel(
   htmlOutput("nText2ui")
    )
  )