【问题标题】:adding data input into a empty data frame in R using shiny使用闪亮将数据输入添加到R中的空数据框中
【发布时间】:2020-03-18 03:26:28
【问题描述】:

我对 Shiny 还很陌生,我正在尝试为不同公司使用的金额制作一个用户界面。我制作了一个空数据框,并试图将用户的输入添加到数据框中,但应用程序崩溃了。请提供任何帮助!

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) 

【问题讨论】:

    标签: r dataframe shiny


    【解决方案1】:

    构造数据框时,整数为numeric()。我还添加了reactiveValues,以便您可以将数据框放入其中。如果您希望在每次访问时重置数据框,您可以将 v$df &lt;- data.frame(Company =... 放入应用程序的 server 部分

    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) 
    

    【讨论】:

    • 你的回答破坏了表格的显示,每列一个字母
    • 还是不行,单词被分列了
    • 我认为你在用什么版本的 DT,一切都适合我
    • 我忘了在应用程序的开头加载 DT,你应该在你的答案中添加一个 library 调用,它现在可以工作了
    • 我有另一台机器可以重现该问题,添加 DT:: 解决了它
    猜你喜欢
    • 2021-10-31
    • 2021-06-21
    • 1970-01-01
    • 2016-02-25
    • 2021-12-01
    • 2014-04-11
    • 2017-11-15
    • 2014-12-28
    • 2019-01-27
    相关资源
    最近更新 更多