Shiny 如何在R Shining应用程序中操作数据帧

Shiny 如何在R Shining应用程序中操作数据帧,shiny,shinydashboard,shiny-server,shiny-reactivity,shinyapps,Shiny,Shinydashboard,Shiny Server,Shiny Reactivity,Shinyapps,我需要一个关于闪亮代码的助手。我想通过将数据帧输入分离为列向量进行计算来操作数据帧输入,但我一直得到这个错误 Warning in <reactive>(...): NAs introduced by coercion (…)中的警告:强制引入的NAs 代码如下 library(shiny) ui <- fluidPage( # dataset data <- data.frame(e1 = c(3, 7, 2, 14, 66),

我需要一个关于闪亮代码的助手。我想通过将数据帧输入分离为列向量进行计算来操作数据帧输入,但我一直得到这个错误

Warning in <reactive>(...): NAs introduced by coercion
(…)中的警告:强制引入的NAs 代码如下

 library(shiny)
ui <- fluidPage(

  # dataset
  data <- data.frame(e1 = c(3, 7, 2, 14, 66),
             e2 = c(2, 16, 15, 66, 30),
             n1 = c(18, 25, 45, 62, 81), 
             n2= c(20, 30, 79, 64, 89))   
# Application title
titlePanel("Demo"),

# Sidebar with a slider input for number of bins 
sidebarLayout(
  sidebarPanel(
     # Input: Upload file
    fileInput(inputId = 'file', label = 'upload the file')
    ),

 # Display Output
  mainPanel(
    uiOutput("final")
    )
  )
 )
# Define server logic required to draw a histogram
server <- function(input, output) {

   # separating the dataframe into 4 column vectors
   e1 <- reactive(as.numeric(input$file[,1]))
   e2 <- reactive(as.numeric(input$file[,2]))
   n1 <- reactive(as.numeric(input$file[,3]))
   n2 <- reactive(as.numeric(input$file[,4]))

   # File Upload function
   data <- reactive({
   file1 <- input$file
   if(is.null(file1)){return()}
   read.table(file = file1$datapath, sep = ',', header = TRUE)
   })


   output$result <- renderUI({
     y <- (e1()/n1()) - (e2()/n2())
    lg_y <- log(y)
    v2 <- ((n1() - e1())/e1() * n1()) + ((n2() - e2())/e2() * n2())
    w <- 1/v2
    w1 <- sum(w)
    w2 <- sum(w^2)
    c <- w1 - (w2/w1)
    s2 <- w * lg_y
    ybar <- sum(s2)/sum(w)
    Q <- sum(w*((lg_y - ybar)^{2}))# Cochrane homogeneity test statistic
    Q.pval <- reactive(pchisq(Q, k() - 1,lower.tail = FALSE))
    Isqd <- max(100*((Q-(k()-1))/Q),0)
   })
 }
 # Run the application 
 shinyApp(ui = ui, server = server)
库(闪亮)

ui仍然无法运行上述代码,因为您没有定义函数
k()
。另外,您的
renderUI
设置为
“结果”
,但您的ui输出设置为
“最终”

您在(…):强制引入的NAs中得到警告
警告,因为您的真实数据集中可能包含一个非数字。我对您提供的上述数据集没有任何问题

前进的道路有两条:

1) 编写一个函数,在处理数据之前删除所有非数字。有关一些示例,请参见

2) 只需保留警告,它毕竟是一个警告,所以它不会阻止代码运行。目前,它将您的非数字转换为NA

3) 使用
suppressWarnings()
,但通常不建议这样做

不过,我有一个清理代码的建议:

   # File Upload function
   data <- reactive({
   file1 <- input$file
   if(is.null(file1)){return()}
   read.table(file = file1$datapath, sep = ',', header = TRUE, stringsAsFactors = FALSE)
   })

# separating the dataframe into 4 column vectors
   e1 <- reactive(as.numeric(data()[,1]))
   e2 <- reactive(as.numeric(data()[,2]))
   n1 <- reactive(as.numeric(data()[,3]))
   n2 <- reactive(as.numeric(data()[,4]))
#文件上传功能

数据您需要提供一个数据样本,否则很难帮助数据样本4 X 5数据集,即4列5行。如果您发布一个带有
dput(您的数据)
输出的代码片段,或者如果它是一个非常大的表,那么它的一个子集。@AndreaDodet我已经包含了一个假设数据集,我希望这有帮助?谢谢开尔文。我按照建议清理了代码,没问题!请随意单击绿色复选标记,确认我的答案是正确的,这样可以帮助其他观看的人知道它是有效的。