adding data input into a empty data frame in R using shiny

别等时光非礼了梦想. 提交于 2021-02-11 18:21:07

问题


I am fairly new to shiny and R I am trying to make a user interface for the amount of money used in different companies. I made a empty data frame, and was trying to add the input from the user to the data frame, but the app crashes. Any assistance would be great please!

library(shiny)

df <- data.frame(Company = character(), Goods = character(),GoodsType = character(), 
             Number = integer(), Cost = double(), stringsAsFactors = FALSE)

ui <- fluidPage(
     titlePanel("Cost tool "),

      sidebarLayout(
        sidebarPanel(
          selectInput(inputId = "Company", label = "Company",
                      choices = list("Company 1", "Company 2 " , " Company 3 "), selected = ""),

          radioButtons(inputId = "Goods", label = "Select bought goods",inline = TRUE,
               choices = list("Food", "Electronics","Books"), selected = ""),

          selectInput(inputId = "GoodsType", label = " Types of Goods",
              choices = list(" Milk", "Eggs" , "vegetables" ,"Games Consoles",
               "headphones", "phones", "mystery","fiction","horror"), selected = ""),

          textInput(inputId = "Number", label = " How many?: ",
                    value = ""),

           textInput(inputId = "Cost", label = "Cost",
                    value = ""),

          actionButton("submit","Submit")


  ),
    mainPanel(
     dataTableOutput("CostTable")
    )
  )
)


server <- function(input, output){

 observeEvent(input$submit, {

 df[1,] <- data.frame(input$Company,input$Goods,input$GoodsType, input$Number, input$Cost)


   })


}

shinyApp(ui = ui, server = server) 

回答1:


When constructing the dataframe, integers are numeric(). I've also added the reactiveValues so you can put your dataframe into. If you want the dataframe to reset on evey visit you can put v$df <- data.frame(Company =... into the server part of the app

library(shiny)
library(DT)
ui <- fluidPage(
    titlePanel("Cost tool "),
    sidebarLayout(
        sidebarPanel(
            selectInput(inputId = "Company", label = "Company",
                        choices = list("Company 1", "Company 2 " , " Company 3 "), selected = ""),
            radioButtons(inputId = "Goods", label = "Select bought goods",inline = TRUE,
                         choices = list("Food", "Electronics","Books"), selected = ""),
            selectInput(inputId = "GoodsType", label = " Types of Goods",
                        choices = list(" Milk", "Eggs" , "vegetables" ,"Games Consoles",
                                       "headphones", "phones", "mystery","fiction","horror"), selected = ""),
            textInput(inputId = "Number", label = " How many?: ",value = ""),
            textInput(inputId = "Cost", label = "Cost",value = ""),
            actionButton("submit","Submit")
        ),
        mainPanel(
            DT::dataTableOutput("CostTable")
        )
    )
)


v <- reactiveValues()
v$df <- data.frame(Company = character(), Goods = character(),GoodsType = character(), 
                   Number = numeric(), Cost = numeric(), stringsAsFactors = FALSE)

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

    observeEvent(input$submit,{
        req(input$Company,input$Goods,input$GoodsType,input$Number,input$Cost)
        tmp <- data.frame(Company = input$Company,Goods = input$Goods,GoodsType = input$GoodsType, Number = input$Number,Cost = input$Cost)
        v$df <- rbind(v$df,tmp)
    })
    output$CostTable <- DT::renderDataTable({
        v$df
    })
}

shinyApp(ui = ui, server = server) 



来源:https://stackoverflow.com/questions/60733099/adding-data-input-into-a-empty-data-frame-in-r-using-shiny

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!