UI代码:如何计算Shiny中textInput框中的数据?
===
library(shiny)
shinyUI(
# Use a fluid Bootstrap layout
fluidPage(
# Generate a row with a sidebar
sidebarLayout(
# Define the sidebar with one input
sidebarPanel(
sliderInput("capacity", "Current Capacity:",
min=0, max=100, value=10),
c(list(
textInput("service", "Service Component Name", ""),
actionButton("addbtn", "Add Component"))),
#lapply(seq(10), function(i) uiOutput(paste0("ui", i)))
br(),
br(),
br(),
br(),
br(),
actionButton("calcbtn", "Calculate Projection")
),
# Create a spot for the barplot
mainPanel(
textInput("inputWork","Volume", width="200px"),
textInput("inputGrowth","Growth Rate", width="100px"),
lapply(seq(10), function(i) uiOutput(paste0("ui", i)))
#tags$p("Web"),
#verbatimTextOutput("input_type_text")
)
)
)
)
服务器代码:
server <- function(input, output)
{
observeEvent(input$addbtn, {
n <- isolate(input$addbtn)
if (n == 0) return()
# create n-th pair of text input and output
output[[paste0("ui", n)]] <- renderUI(
list(textInput(paste0("textin", n), isolate(input$service)),
textOutput(paste0("textout", n))))
# display something in the output
output[[paste0("textout", n)]] <- renderText({
work<-as.numeric(input$inputWork)
growth<-as.numeric(input$inputGrowth)
print(growth)
#paste("projection", (100+growth)*as.numeric(input[[paste0("textin", n)]]))
})
})
observeEvent(input$calcbtn, {
n <- isolate(input$calcbtn)
if (n == 0) return()
output[[paste0("textout", n)]] <- renderText({
work<-as.numeric(input$inputWork)
growth<-as.numeric(input$inputGrowth)
project<-growth+as.numeric(input$service)
print(growth)
print(project)
paste("projection", ((1+growth/100)*as.numeric(input[[paste0("textin", n)]])))
})
})
}
这就是我要做的。此代码将有一个初始文本框和提交按钮。用户在第一个输入文本中放置文本,单击submitbutton,在主面板中生成一个新文本。用户可以多次执行此操作以在主面板中创建多个textInput框。
我在主面板的顶部还有一个静态的另一个inputText框,标签为Workload。
所以,这就是我想要做的事:
- 用户将插入工作量textIntut数据(它需要的数字)。
- 用户将数据插入到其他动态生成的textInput框(全部需要是数字)。
- 我将从工作负载和所有其他文本框中获取值,执行一些计算和预测,并在每个动态生成的textInput框旁边显示数据,如果我可以在旁边插入文本框生成以显示我的输出。
例如,我在我的工作负载中有数据,我已经生成了Web_server,App_server textInput框。我将从工作负载中提取数据,并根据web_server中的数据进行分类,并将其显示在web_server textInput框(在文本框中显示数据)旁边,对app_server textInput框执行相同操作。
任何想法,我怎么可以做到这一点闪亮?这是我想要完成的图像。考虑到从用户输入的工作负载增长率以及来自用户输入部分的其他输入,我将不得不计算并填充各个文本框。下面
如果您所说的数据是单个数字,则应该使用'numericInput'。否则,您需要在服务器代码中进行一些验证和转换(将字符串转换为数字) –
有两个问题。 (1)你知道动态创建的输入框的最大数量吗?或者您需要接受用户想要的多少? (2)您能否显示一个示例UI来显示计算结果?这将是textOutput关联的textInput? –
1)没有最大数量的文本框,它可以与用户输入一样多。 2)是,与textInput关联的textOutput。他们可以并排吗? – user1471980