我被要求做一个简单的闪亮应用程序。我有以下代码:
library(shiny)
ui <- fluidPage(
tabsetPanel(
tabPanel(title = "Central Limit Theorem",
sliderInput(inputId = "num", label = "Choose a sample size",
value = 5, min = 1, max = 50),
plotOutput(outputId = "hist")
)
,
tabPanel(title = "BMI prediction",
pageWithSidebar(
# Application title
headerPanel("BMI prediction"),
sidebarPanel(
numericInput('wt', 'Weight in Pounds', 90, min = 50, max = 200),
submitButton('Submit'),
numericInput('ht', 'Height in Inches', 60, min = 50, max = 85),
submitButton('Submit')
),
mainPanel(
h3('Results of prediction'),
h4('Your weight is'),
verbatimTextOutput("inputValue1"),
h4('Your height is'),
verbatimTextOutput("inputValue2"),
h4('Which resulted in a BMI of '),
verbatimTextOutput("prediction")
)
)
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$hist <- renderPlot({
par(mfrow = c(1,2))
r <- 10000
samp <- matrix(rexp(n=(input$num)*r, rate = 2), nrow = r)
sample.means <- apply(samp,1,mean)
hist(sample.means,col="purple", main="Sampling Distribution of the Sample Mean",
xlab = "Sample Mean")
qqnorm(sample.means, col = "purple")
})
bmi_calc <- function(weight, height) (weight/height^2)*703
output$inputValue1 <- renderPrint({input$wt})
output$inputValue2 <- renderPrint({input$ht})
output$prediction <- renderPrint({bmi_calc(input$wt, input$ht)})
}
shinyApp(ui = ui, server = server)
由于某些原因,第一个选项卡中的直方图不响应滑块。如果我不包括第二个选项卡,它将正常工作。
这是两个submitButton
S; 如果单击第二个选项卡上的任何一个按钮并返回第一个选项卡,则它将更新。不幸的是,我认为没有一种方法可以指定submitButton
更新的输入(当前两个输入均适用于所有输入,因此第二个选项卡上的两个都是多余的)。但是,有一些替代方法:
submitButton
在每个选项卡上放一个,它将无害地更新(除非一个确实是资源密集型的)更新所有输入。submitButton
外部tabsetPanel
,因此一个按钮可以更新所有三个输入。actionButton
来显式更新您喜欢的任何值。这种方法可以使您对何时更新的内容进行更精确的控制,但是却要付出更多的代码编写工作。如果您的小部件需要大量资源,而又不想过度更新,则这是最佳选择。本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句