如何在R Shiny中使用两个数字输入

时间:2016-12-27 23:35:43

标签: r shiny

下面我有一个R代码,它接受来自numericInput对象的输入值并将其存储在Excel电子表格中。我现在想要有两个numericInput,但我不知道该怎么做。在下面的代码中,我试图复制对象作为最后的努力,但它给了我一个错误。 (我并不感到惊讶)有什么建议吗?

library(shiny)
library(xlsxjars)
library(rJava)
library(xlsx)

ui <- fluidPage(
  numericInput(inputId = "num", 
               label = "Choose a number",
               min = 0, max = 1000000, value = 1),
  actionButton(inputId = "submit", 
               label = "Submit"),
  numericInput(inputId = "num2", 
               label = "Choose a number2", min = 0, max = 1000000, value = 1),
)

server <- function(input, output) {
  options(java.parameters = "- Xmx1024m")
  wb <- loadWorkbook(file = "F:\\RProject-Rough\\DirectEffect.xlsx")
  sheet<-getSheets(wb)

  observeEvent(input$submit, {
          addDataFrame(c(input$num,input$num2), sheet$'Direct Effects',
          col.names = FALSE, row.names = FALSE, startRow = 3,startColumn = 5)
          saveWorkbook(wb,"F:\\RProject-Rough\\DirectEffect.xlsx")
  })
}

shinyApp(ui = ui, server = server)

1 个答案:

答案 0 :(得分:0)

首先,在你的第二个numericInput之后,有一个不属于那里的逗号。

其次,addDataFrame将data.frame作为参数而不是向量。

以下适用于我:

library(shiny)
library(xlsxjars)
library(rJava)
library(xlsx)

ui <- fluidPage(
  numericInput(inputId = "num", 
               label = "Choose a number",
               min = 0, max = 1000000, value = 1),
  actionButton(inputId = "submit", 
               label = "Submit"),
  numericInput(inputId = "num2", 
               label = "Choose a number2", min = 0, max = 1000000, value = 1)
)

server <- function(input, output) {

  options(java.parameters = "- Xmx1024m")
  wb <- loadWorkbook(file = "/PATH/TO/test.xlsx")
  sheet <- getSheets(wb)

  observeEvent(input$submit, {
    addDataFrame(data.frame(c(input$num,input$num2)), sheet$`Direct Effects`, 
    col.names = FALSE, row.names = FALSE, startRow = 3, startColumn = 5)
    saveWorkbook(wb, "/PATH/TO/test.xlsx")
  })
}

shinyApp(ui = ui, server = server)