2016-12-27 51 views
0

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

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) 

回答

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) 
+0

哦,太感谢你了。对于愚蠢的错误感到抱歉。 – magicmykh

相关问题