刚开始学习闪亮。我试着构建一个简单的、无反应的应用程序,用户点击一个按钮,屏幕上就会打印出一个随机矢量。但是,我不能让提交按钮工作。
# Load required files
lapply(c("data.table", "shiny"), require, character.only=T)
#=================================================================
# Define UI for application that draws a histogram
ui <- shinyUI(fluidPage(
# Application title
titlePanel("App-4"),
# Sidebar
sidebarLayout(
sidebarPanel(
submitButton("Submit")
),
# Print the data
mainPanel(
textOutput("myTable")
)
)
))
#=================================================================
# Define server logic
server <- shinyServer(function(input, output) {
output$myTable <- renderPrint({
sample(10)
})
})
#=================================================================
# Run the application
shinyApp(ui = ui, server = server)我做错了什么?我能够让它在actionButton上工作,但我想知道为什么上面的代码不能工作。谢谢。
发布于 2016-04-07 07:54:05
这是一个非常简单的演示。当您单击该按钮时,它将生成一个包含100个随机数的新直方图。
submitButton用于输入表单,不适用于您的要求。例如,如果您有四个不同的输入,并且希望输出仅在单击“提交”按钮时更改,而不是在单个输入更改时更改。
在Shiny中,输出更改是由事件链引起的。您的输出需要依赖于一个或多个输入才能更改。现在,您的输出(服务器代码)不依赖于任何输入,因此不会发生任何事情。阅读这里可以获得非常详细的解释。http://shiny.rstudio.com/articles/reactivity-overview.html
library(shiny)
# Define UI for application that draws a histogram
ui <- shinyUI(fluidPage(
# Application title
titlePanel("Button demo"),
# Sidebar with a button
sidebarLayout(
sidebarPanel(
actionButton("button", "Click me to get a new histogram")
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
))
# Define server logic required to draw a histogram
server <- shinyServer(function(input, output) {
observeEvent(input$button, {
output$distPlot <- renderPlot({
hist(rnorm(100))
})
})
})
# Run the application
shinyApp(ui = ui, server = server)https://stackoverflow.com/questions/36461657
复制相似问题