R Shiny 数据分析:我正在努力从我上传的 .csv 文件中渲染一个图

R Shiny data analysis: I am struggling to render a plot from the .csv file I have uploaded

我正在用 R Shiny 构建可靠性数据分析工具。我是 R 的新手(几年前用过一次),也是 Shiny 的新手。我一直无法通过教程或论坛解决这个问题:(

基本上,我希望用户上传失败事件的 .csv 文件 table。 table 有四列(i、Xi、Ci 和 Ti)并且可以有任意数量的行。

首先,我想以table形式显示数据。我模型的这一部分目前有效。

然后,我想绘制 Ti vs i 的图。我已经为它编写了代码(我已经尝试了很多次迭代,当前代码在下面),但是我没有尝试过。

稍后,我将要使用数据执行统计操作(从拉普拉斯趋势检验开始),我也不确定该怎么做(例如,我如何指定 column/row/data 项目我在执行操作时使用?)。

下面是当前的R代码(我已经删除了不相关的部分):

library(shiny)
library(ggplot2)
library(tidyverse)

ui <- fluidPage(
  
  #App title
  titlePanel("Reliability Data Analysis Tool"),
  
  tabsetPanel(
    
    tabPanel("Model selection", fluid = TRUE,
             sidebarLayout(
               
               sidebarPanel(
                 
                 "Welcome to the RShiny Reliability Data Analysis tool. 
                 To begin, please upload a valid events table file.", HTML('<br/>'),HTML('<br/>'),
                 
                 
                 #Allow user to input events table in .csv format
                 fileInput(
                   inputId = "fileEventsTable",
                   label = "Upload an events table", 
                   accept = ".csv"),
                 
                 #Indicate whether or not data includes headers   
                 checkboxInput("header", "Header", TRUE),
                 
                 
               ),
               
               mainPanel(
                 
                 h4("Events table"),
                 
                 h5("Key"),
                 "i = Observation number,",
                 "Xi = Interarrival time,",
                 "Ci = Failure indication,",
                 "Ti = Global time",
                 
                 #Display events table
                 tableOutput("eventsTableContents"),
                 
                 HTML('<br/>'),
                 
                 h4("Cumulative failure plot"),
                 
                 #Display cumulative failure plot 
                 plotOutput("eventsTablePlot")
                 
               )))))
                 
server <- function(input, output) {
                   
                   #Read .csv file as a data frame
                   eventsdataframe<-reactive({
                     if (is.null(input$fileEventsTable))
                       return(NULL)                
                     data<-read.csv2(input$fileEventsTable$datapath)
                   })
                   
                   #Render table from events table data frame
                   output$eventsTableContents <- renderTable({
                     eventsdataframe()
                   })
                   
                   #Render plot from events table data frame
                   output$eventsTablePlot <- renderPlot({
                     ggplot(data = eventsdataframe()  +
                              geom_point(aes(x=eventsdataframe$Ti,y=eventsdataframe$i,size=10)))
                     
                   })
                 }
                 
shinyApp(ui = ui, server = server)              
                 

这是我在 运行 上传适当的 .csv 文件时得到的当前输出:

Screenshot

注意错误:二元运算符的非数字参数,我希望散点图位于此处。

任何 help/insight 将不胜感激!

尝试使用此代码。看起来像是反应数据的问题:

library(shiny)
library(ggplot2)
library(tidyverse)

ui <- fluidPage(
  
  #App title
  titlePanel("Reliability Data Analysis Tool"),
  
  tabsetPanel(
    
    tabPanel("Model selection", fluid = TRUE,
             sidebarLayout(
               
               sidebarPanel(
                 
                 "Welcome to the RShiny Reliability Data Analysis tool. 
                 To begin, please upload a valid events table file.", HTML('<br/>'),HTML('<br/>'),
                 
                 
                 #Allow user to input events table in .csv format
                 fileInput(
                   inputId = "fileEventsTable",
                   label = "Upload an events table", 
                   accept = ".csv"),
                 
                 #Indicate whether or not data includes headers   
                 checkboxInput("header", "Header", TRUE),
                 
                 
               ),
               
               mainPanel(
                 
                 h4("Events table"),
                 
                 h5("Key"),
                 "i = Observation number,",
                 "Xi = Interarrival time,",
                 "Ci = Failure indication,",
                 "Ti = Global time",
                 
                 #Display events table
                 tableOutput("eventsTableContents"),
                 
                 HTML('<br/>'),
                 
                 h4("Cumulative failure plot"),
                 
                 #Display cumulative failure plot 
                 plotOutput("eventsTablePlot")
                 
               )))))

server <- function(input, output) {
  
  #Read .csv file as a data frame
  eventsdataframe<-reactive({
    if (is.null(input$fileEventsTable))
      return(NULL)                
    data<-read.csv2(input$fileEventsTable$datapath)
  })
  
  #Render table from events table data frame
  output$eventsTableContents <- renderTable({
    eventsdataframe()
  })
  
  #Render plot from events table data frame
  output$eventsTablePlot <- renderPlot({
    ggplot(data = eventsdataframe()  +
             geom_point(aes(x=eventsdataframe()$Ti,y=eventsdataframe()$i,size=10)))
    
  })
}

看起来它可能只是在 ggplot 中放错了 ),并在 geom_point.

中将反应数据帧引用为 eventsdataframe()

而不是

ggplot(data = eventsdataframe()  +
                              geom_point(aes(x=eventsdataframe$Ti,y=eventsdataframe$i,size=10)))

试试这个

ggplot(data = eventsdataframe())  +
                              geom_point(aes(x=eventsdataframe()$Ti,y=eventsdataframe()$i,size=10))

我认为反应性数据框存在问题,所以我使用 observeEvent 作为反应值,然后 isolate 它也在 observeEvent 下,而不是使用 read.csv2 阅读我使用过的数据 read.csv 做了一些小改动,应用程序 运行 很好。

你试试这个,我认为它符合你的目的。

library(shiny)
library(ggplot2)
library(tidyverse)

ui <- fluidPage(
  
  #App title
  titlePanel("Reliability Data Analysis Tool"),
  
  tabsetPanel(
    
    tabPanel("Model selection", fluid = TRUE,
             sidebarLayout(
               
               sidebarPanel(
                 
                 "Welcome to the RShiny Reliability Data Analysis tool. 
                 To begin, please upload a valid events table file.", HTML('<br/>'),HTML('<br/>'),
                 
                 
                 #Allow user to input events table in .csv format
                 fileInput(
                   inputId = "fileEventsTable",
                   label = "Upload an events table", 
                   accept = ".csv"),
                 
                 #Indicate whether or not data includes headers   
                 checkboxInput("header", "Header", TRUE)),
               
               mainPanel(
                 
                 h4("Events table"),
                 
                 h5("Key"),
                 "i = Observation number,",
                 "Xi = Interarrival time,",
                 "Ci = Failure indication,",
                 "Ti = Global time",
                 
                 #Display events table
                 tableOutput("eventsTableContents"),
                 
                 HTML('<br/>'),
                 
                 h4("Cumulative failure plot"),
                 
                 #Display cumulative failure plot 
                 plotOutput("eventsTablePlot")
                 
               )))))

server <- function(input, output) {
  
  #Read .csv file as a data frame
  eventsdataframe<-reactive({
    req(input$fileEventsTable)
    data<-read.csv(input$fileEventsTable$datapath)
  })
  
  #df <- eventsdataframe()
  
  #Render table from events table data frame
  output$eventsTableContents <- renderTable({
    eventsdataframe()
  })
  
  #Render plot from events table data frame
  observeEvent(eventsdataframe(), {
    req(eventsdataframe())
    
    ddf <- isolate(eventsdataframe())
    
    output$eventsTablePlot <-renderPlot({
      
      ggplot(data =ddf, aes(x=ddf$Ti,y=ddf$i,size=10))+
        geom_point()
      
      })
  })
}

shinyApp(ui = ui, server = server)              

并且我已经用虚拟数据对其进行了测试