【问题标题】:Shiny: calculating standard deviation from an uploaded excel fileShiny:计算上传的 excel 文件的标准差
【发布时间】:2020-03-05 03:12:27
【问题描述】:

我正在尝试计算加载到我的 Shiny 应用程序中的 Excel 文件的标准偏差。我似乎无法这样做。当我输入 sd(output$contents) 或 sd(file1) 时,它往往会使应用程序崩溃。这是我到目前为止所拥有的

供参考,我上传的数据是单变量时间序列 如果有人能够帮助我,我将非常感激 - 我认为这是一个初学者问题!也许我只是查看文件而不使用它?

编辑:excel 文件总是包含一列数字,但标题和标题可能会改变。因此,我想在不断变化的 excel 文件中引用 A 列。

  ui <- fluidPage(
    setBackgroundColor("ghostwhite"),
    titlePanel(h1("title", style = "color:navy")),
    p("subtitle"),

    headerPanel(h3("Input data here", style = "color:navy")),

    # Sidebar panel 
    sidebarLayout(
    sidebarPanel( position =c("left"),
        sliderInput("SL",
                    "ServiceScore",
                    min = 1.28, max = 3.09, value = 2.28),

        numericInput("LT", "LT: weeks",0, min=0, max = 30),
        fileInput('file1', 'MS, choose xlsx file',
                  accept = c(".xlsx")
        ),

     br(),            
     actionButton("action_Calc", label = "Refresh & Calculate"), ),

    # Main panel
    mainPanel(h3( textOutput("SL"), 
               textOutput("LT"),
               textOutput("SS"),

               tableOutput('contents')
              ))))

     server <- function(input, output) {

    output$SL <- renderText({ 
        paste("Your score", input$SL)})
    output$LT <- renderText ({ 
        paste( "Your LT is", input$LT)})
    output$contents <- renderTable({

        req(input$file1)

        inFile <- input$file1

        read_excel(inFile$datapath, 1)
    })

    values<- reactiveValues()
    observe({
        input$action_Calc
        values$int<- isolate({
        input$SL*sqrt(input$LT/4)*sd(**HERE IS WHERE I NEED THE SD of the EXCEL FILE**)
        })})
     output$SS <- renderText({paste("calculation is", values$int)})


      }
     shinyApp(ui, server)

【问题讨论】:

  • "...Excel 文件的标准偏差。"??你需要给 R 一个变量的名字,而不是一个文件的名字。
  • 对不起,这是有道理的,该文件是一个单一的数字时间序列 - 因此它的标准偏差。由于文件由单列组成,我将如何编写代码来引用第一列?

标签: r excel shiny


【解决方案1】:

在反应导体中读取 Excel 文件:

Data <- reactive({
  req(input$file1)
  inFile <- input$file1
  read_excel(inFile$datapath, 1)
})

output$contents <- renderTable({
  Data()
})

现在如果你想要第一列的标准差:

values<- reactiveValues()
observe({
  input$action_Calc
  values$int <- isolate({
    input$SL*sqrt(input$LT/4)*sd(Data()[[1]])
  })
})
output$SS <- renderText({paste("calculation is", values$int)})

【讨论】:

  • 非常感谢 Stephane,这解决了我的问题!
猜你喜欢
  • 2014-11-18
  • 1970-01-01
  • 2016-12-20
  • 2016-04-14
  • 2020-01-28
  • 1970-01-01
  • 2019-09-17
  • 2014-05-06
相关资源
最近更新 更多