【问题标题】:Dynamic select input not working with loaded data动态选择输入不适用于加载的数据
【发布时间】:2016-04-01 18:09:55
【问题描述】:

使用 R shiny,我正在开发一个简单的应用程序,允许用户从 Rdata 文件输入数据。我希望应用程序加载数据,在选择输入字段中显示数字变量的名称,并在用户选择其中一个变量后进行一些分析。但我无法让它工作。在提供的代码中,我获得了两个输出:摘要,可以正常工作,以及 我无法工作的所选变量的平均值

服务器.R

library(shiny)
library(shinydashboard)
library(data.table)
library(DT)

shinyServer(function(input, output) {


####   DATA LOAD
df <- reactive({
 df <- input$datafile
 if (is.null(df)) {
  # User has not uploaded a file yet
  return(NULL)
 }
 objectsLoaded <- load(input$datafile$name) 
 # the above returns a char vector with names of objects loaded
 df <- eval(parse(text=objectsLoaded[1])) 
 # the above finds the first object and returns it
 df<-data.table(df)

})


####   SELECTS

num <- reactive({  
 num <- sapply(df(),is.numeric)
 num <- names(num)
})

output$var_num <- renderUI({
 vector.num <- as.vector(num())
 selectInput("var_num", "Select Variables :", as.list(vector.num),  multiple = FALSE)
 }) 



####   OUTPUTS 
### SUMMARY 
output$summary_num <-renderDataTable({
  x<-t(sapply(df(), summary))
  x<-as.data.frame(x)
  x<-setDT(x, keep.rownames = TRUE)[]
  colnames(x) <- c("Variable","Mínimo","1er Quartil", "Mediana", "Media", "3er Quartil","Máximo")
  datatable(x)

 })

### MEAN OF SELECTED VAR  
output$test <-renderPrint ({
  if(is.null(df()))
  return()
  dat<- df()
  dat <- dat[,num(), drop = FALSE]
  mean(dat[,input$var_num])
  })  
})

UI.R

dashboardPage(
  dashboardHeader(title = "TITLE", titleWidth = 500),
  dashboardSidebar(disable = TRUE), #---> fin SIDEBAR
  dashboardBody(
    fluidRow(
      box(width=12,  status = "primary",
        tabsetPanel(
          tabPanel("Test",
                   fileInput("datafile", label = h3("File input")),
                   uiOutput("var_num"),
                   br(),hr(),br(),

                   fluidRow(column(width=4, uiOutput("var_caracter"),textOutput("test"))),
                   br(),hr(),br(),

                   fluidRow(column(width=8, "Variables Numericas", dataTableOutput("summary_num")))
                   )

            ) # fin tabsetPanel
          ) # fin box
      )# fin fluidRow
    )# fin dashboardBody
 )# fin dashboardPage

当我运行应用程序时,一切都很好(选择输入、摘要等),除了计算和打印所选变量的平均值。我猜出于某种原因,子集数据框是空的,但我不知道为什么...... 任何帮助都会很棒!提前致谢。

【问题讨论】:

  • 这并没有说明您的问题,但是您为什么要使用 is.numeric 然后只取名字?然后反应式实际上并没有返回任何东西......这令人困惑。
  • 但是使用 sapply 可以正常工作,正如仅用数字变量填充的选择输入所示...问题是传递选择用于其他分析的变量名称...是的令人困惑。
  • 我在说这个:num &lt;- reactive({ num &lt;- sapply(df(),is.numeric) num &lt;- names(num) }) 这不会返回任何东西,操作本身没有意义。
  • 我在 R 或 Shiny 方面没有那么丰富的经验,但我试图用那几行代码来获取加载数据的数值变量的名称。我在选择输入“var_num”中使用它并在 UI 中呈现并且工作正常。事实上它不是空的,所以它正在工作。我不明白你为什么说它正在返回任何东西......
  • 好的,如果你想要那个,那么你想要的是names(df())[sapply(df(),is.numeric)]

标签: r shiny


【解决方案1】:

我得到它的工作。 解决方案是定义我使用的数据集as.data.frame

### MEAN OF SELECTED VAR  
  output$test <-renderPrint ({
    if(is.null(df()))
      return()
    dat<- as.data.frame(df()) ## THIS IS THE CORRECTION
    dat <- dat[,num(), drop = FALSE]
    mean(dat[,input$var_num])
 })  

我真的不明白为什么...反应文件 df() 被定义为 data.table 并且 dat 应该继承它,但由于某种原因,它需要明确定义为数据帧。

【讨论】:

  • data.table 的语法与 data.frame 不同。 dat[,num(), drop = false] 表示两者完全不同。
  • 感谢您的解释!
猜你喜欢
  • 1970-01-01
  • 2023-03-11
  • 2016-10-20
  • 1970-01-01
  • 2015-03-23
  • 1970-01-01
  • 2021-12-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多