闪亮的初始缩放

时间:2017-08-31 09:54:59

标签: r shiny

我在使用R中的闪亮包时遇到了问题。我正试图插入收入。在我已经内置到原始数据集中的滑块中输入的债务和奖金,因此算法可以缩放,即规范化数据以进行计算。现在我只能按原样进行计算,即当你输入计算它的数据时,正如我所指出的,我希望输入的数据回到原始数据集中,这样我就可以在进行计算之前进行缩放。我有以下代码用于闪亮的包。

library(shiny)
shinyUI(fluidPage(
          headerPanel("Calculating fwbs"),
          sidebarPanel(
            sliderInput("Income", "Please Select Income: ", min=0, max=5000, 
                        value=2500, step=100),
            sliderInput("Debt", "Please Select Debt: ", min = 0, max=2500, 
                        value=1250, step = 100),
            sliderInput("Bonus", "Have you received any bonus: ", min=0, max= 1000, 
                        value=500, step =100)
          ),
          mainPanel(
            textOutput("fwbi")
          )
        ))

shinyServer(function(input, output, session){
  output$fwbi <- renderText({
    income2<- input$Income
    debt2<- input$Debt
    bonus2<- input$Bonus
    paste("Your fwbi is: ", income2 + debt2 + bonus2)
  })
})

非常感谢

2 个答案:

答案 0 :(得分:0)

我们可以将代码更改为

library(shiny)
library(DT

- 创建一个函数

fscale <- function(data  = NULL) {



ui <- fluidPage(

          headerPanel(title = "Calculating fwbs"),

          sidebarPanel(

            sliderInput("Income", "Please Select Income: ", min=0, max=5000, 
                        value=2500, step=100),
            sliderInput("Debt", "Please Select Debt: ", min = 0, max=2500, 
                        value=1250, step = 100),
            sliderInput("Bonus", "Have you received any bonus: ", min=0, max= 1000, 
                        value=500, step =100),
            numericInput("n", "Number of rows to scale:", 10, min = 1, max = 100)


          ),

          mainPanel(

              textOutput("fwbi"),
              dataTableOutput("data"))
          )



server <- function(input, output, session){

  dat_scale <- reactive({

    n1 <- seq_len(input$n)
    data[["Income"]][n1] <-  (data$Income[n1] - input$Income)/data$Income[n1]
    data[["Debt"]][n1] <- (data$Debt[n1] - input$Debt)/data$Debt[n1]
    data[["Bonus"]][n1] <- (data$Bonus[n1] - input$Bonus)/data$Bonus[n1]
    data
  })  
  output$data <- renderDataTable({
    dat_scale()

  })

  output$fwbi <- renderText({
    income2<- input$Income
    debt2<- input$Debt
    bonus2<- input$Bonus
    paste("Your fwbi is: ", income2 + debt2 + bonus2)
  })


}

shinyApp(ui= ui, server = server)

}

- 运行功能

fscale(df1)

-output enter image description here

数据

set.seed(24)
df1 <- data.frame(Income  = sample(0:5000, 25, replace = TRUE),
              Debt = sample(0:2500, 25, replace = TRUE),
               Bonus = sample(0:1000, 25, replace = TRUE))

答案 1 :(得分:0)

如果您希望能够向原始数据添加任意数量的行,则可以使用reactiveValues列表:

Dframe <- data.frame(income = rnorm(5),
                     debt = rnorm(5),
                     bonus = rnorm(5))

library(shiny)

shinyApp(
  ui = 
    shinyUI(
      fluidPage(
        headerPanel("Calculating fwbs"),
        sidebarPanel(
          sliderInput("Income", "Please Select Income: ", min=0, max=5000, 
                      value=2500, step=100),
          sliderInput("Debt", "Please Select Debt: ", min = 0, max=2500, 
                      value=1250, step = 100),
          sliderInput("Bonus", "Have you received any bonus: ", min=0, max= 1000, 
                      value=500, step =100),
          actionButton(inputId = "btn_add_to_data",
                       label = "Add To Data")
        ),
        mainPanel(
          textOutput("fwbi"),
          tableOutput("data")
        )
      )
    ),

  server = 
    shinyServer(function(input, output, session){

      # Store the original data at start up.
      Data <- reactiveValues(
        Source = Dframe
      )

      # Every time you click "Add Data", it will add the user-input to Data$Source
      observeEvent(
        input$btn_add_to_data,
        {
          new_row <- data.frame(income = input$Income,
                                debt = input$Debt,
                                bonus = input$Bonus)

          Data$Source <- rbind(Data$Source,
                               new_row)
        }
      )

      output$fwbi <- renderText({
        income2<- input$Income
        debt2<- input$Debt
        bonus2<- input$Bonus
        paste("Your fwbi is: ", 
              scale(Data$Source$income) + 
              scale(Data$Source$debt) + 
              scale(Data$Source$bonus))
      })

      output$data <- renderTable({
        Data$Source
      })
    })
)

如果您希望将用户限制为仅添加一行,则可以使用eventReactive

Dframe <- data.frame(income = rnorm(5),
                     debt = rnorm(5),
                     bonus = rnorm(5))

library(shiny)

shinyApp(
  ui = 
    shinyUI(
      fluidPage(
        headerPanel("Calculating fwbs"),
        sidebarPanel(
          sliderInput("Income", "Please Select Income: ", min=0, max=5000, 
                      value=2500, step=100),
          sliderInput("Debt", "Please Select Debt: ", min = 0, max=2500, 
                      value=1250, step = 100),
          sliderInput("Bonus", "Have you received any bonus: ", min=0, max= 1000, 
                      value=500, step =100),
          actionButton(inputId = "btn_add_to_data",
                       label = "Add To Data")
        ),
        mainPanel(
          textOutput("fwbi"),
          tableOutput("data")
        )
      )
    ),

  server = 
    shinyServer(function(input, output, session){

      NewData <- eventReactive(
        input$btn_add_to_data,
        {
          new_row <- data.frame(income = input$Income,
                                debt = input$Debt,
                                bonus = input$Bonus)

          rbind(Dframe,
                new_row)
        }
      )

      output$fwbi <- renderText({
        income2<- input$Income
        debt2<- input$Debt
        bonus2<- input$Bonus
        paste("Your fwbi is: ", 
              scale(NewData()$income) + 
              scale(NewData()$debt) + 
              scale(NewData()$bonus))
      })

      output$data <- renderTable({
        NewData()
      })
    })
)