【发布时间】:2015-01-15 17:29:35
【问题描述】:
我有一个包含三列的表:销售额、价格和收入。我想创建一个应用程序,它会要求用户输入有关价格的信息,但是当没有输入时,即在按下操作按钮之前,图表将根据代码中已经存在的一些基本价格计算收入。我无法产生这种反应。我提供了下面的代码。任何帮助将不胜感激。
ui.R
library(shiny)
library(googleVis)
shinyUI(fluidPage(
titlePanel("Test"),
sidebarLayout(
sidebarPanel(
numericInput(inputId = "A", label = "A",value=0),
numericInput(inputId = "B", label = "B",value=0),
numericInput(inputId = "C", label = "C",value=0),
numericInput(inputId = "D", label = "D",value=0),
actionButton("action","Submit")
),
mainPanel(
htmlOutput("mytable")
)
)
))
服务器.R
library(data.table)
library(googleVis)
library(shiny)
shinyServer(function(input, output) {
Sales<-c(2,6,9,10)
Prices<-c(34,43,76,89)
Prices_user<-reactive({
c(input$A,input$B,input$C,input$D)
})
Rev<-Sales*Prices
Rev_user<-reactive({
Sales*Prices_user()
})
combined<-data.frame(Sales,Prices,Rev)
combined_user<-reactive({
data.frame(Sales,Prices_user(),Rev_user())
})
output$mytable<-renderGvis({
if(input$action ==0){
gvisTable(combined)
}else{
gvisTable(combined_user())
}
})
})
【问题讨论】:
-
你需要在你的 server.R 中有一行写着
output$mytable <- renderGvis [etc.]。否则,您在 ui.R 中的htmlOutput("mytable")行不会指向任何内容。 -
我已经进行了编辑。但即使生成了表格,它也采用默认价格,即 0
-
代替
Prices<-c(34,43,76,89)(排除它),我认为可以将A、B、C和D的默认值设置为34、43、76和89。例如,@987654326 @ -
是的。我曾考虑过该方法,但反过来又将用户输入显示为开始时的默认值。我想要的是从服务器中的预先固定价格执行此操作,而默认输入显示 0 表示必须更改它。简而言之,我想让 Price 向量成为一个变量,当按下提交时它成为反应向量。
-
我明白了。您可以使用
ifelse()语句创建两个单独的表。它会去:output$mytable <- ifelse(input$action, [code for table with inputs], [code for default table])