你好闪亮的应用程序在反应性表达中给了我错误
Posted
技术标签:
【中文标题】你好闪亮的应用程序在反应性表达中给了我错误【英文标题】:Hello shiny app gives me error in reactive expression 【发布时间】:2021-03-20 09:07:31 【问题描述】:您好,我正在尝试在此链接中重现 RStudio 闪亮应用程序的示例: https://shiny.rstudio.com/articles/tabsets.html shinyapp 给出了 d() 反应表达式的错误,我尝试更改表达式但给了我错误: 代码是:
图书馆(闪亮)
为随机分发应用定义 UI ----
ui
# App title ----
titlePanel("Tabsets"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# Input: Select the random distribution type ----
radioButtons("dist", "Distribution type:",
c("Normal" = "norm",
"Uniform" = "unif",
"Log-normal" = "lnorm",
"Exponential" = "exp")),
# br() element to introduce extra vertical spacing ----
br(),
# Input: Slider for the number of observations to generate ----
sliderInput("n",
"Number of observations:",
value = 500,
min = 1,
max = 1000)
),
# Main panel for displaying outputs ----
mainPanel(
# Output: Tabset w/ plot, summary, and table ----
tabsetPanel(type = "tabs",
tabPanel("Plot", plotOutput("plot")),
tabPanel("Summary", verbatimTextOutput("summary")),
tabPanel("Table", tableOutput("table"))
)
)
)
)
为随机分发应用定义服务器逻辑----
服务器
# Reactive expression to generate the requested distribution ----
# This is called whenever the inputs change. The output functions
# defined below then use the value computed from this expression
d <- reactive(
dist <- switch(input$dist,
norm = rnorm,
unif = runif,
lnorm = rlnorm,
exp = rexp,
rnorm)
dist(input$n)
)
# Generate a plot of the data ----
# Also uses the inputs to build the plot label. Note that the
# dependencies on the inputs and the data reactive expression are
# both tracked, and all expressions are called in the sequence
# implied by the dependency graph.
output$plot <- renderPlot(
dist <- input$dist
n <- input$n
hist(d(),
main = paste("r", dist, "(", n, ")", sep = ""),
col = "#75AADB", border = "white")
)
# Generate a summary of the data ----
output$summary <- renderPrint(
summary(d())
)
# Generate an HTML table view of the data ----
output$table <- renderTable(
d()
)
创建闪亮的应用程序----
shinyApp(用户界面,服务器)
我运行应用程序时的错误是: 警告:dist 中的错误:无效参数 反应式:d [C:\Users\pruebas\Documents\Shiny_examples\tabs/app.R#63]
有人可以帮助我在 RStudio 的文章中尝试学习我的第一个应用程序,但是这个是错误的,它应该用来教像我这样的人。我也很喜欢 github 但我找到了相同的代码。谢谢。
【问题讨论】:
您的代码运行良好。请重启 RStudio 后再试。 谢谢 YBS 和 Ash。我已经重新启动了 RStudio,应用程序运行良好。!! 【参考方案1】:您的应用运行良好。
您是将其保存为两个单独的文件,即ui.r
和server.r
,还是一个文件,即app.r
?
您的代码已设置为将其保存为两个单独的文件,但错误看起来像是您可能已将其保存为单个文件。
尝试将其保存为两个文件:
ui.r
ui <- fluidPage(
# App title ----
titlePanel("Tabsets"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# Input: Select the random distribution type ----
radioButtons("dist", "Distribution type:",
c("Normal" = "norm",
"Uniform" = "unif",
"Log-normal" = "lnorm",
"Exponential" = "exp")),
# br() element to introduce extra vertical spacing ----
br(),
# Input: Slider for the number of observations to generate ----
sliderInput("n",
"Number of observations:",
value = 500,
min = 1,
max = 1000)
),
# Main panel for displaying outputs ----
mainPanel(
# Output: Tabset w/ plot, summary, and table ----
tabsetPanel(type = "tabs",
tabPanel("Plot", plotOutput("plot")),
tabPanel("Summary", verbatimTextOutput("summary")),
tabPanel("Table", tableOutput("table"))
)
)
)
)
server.r
server <- function(input, output)
# Reactive expression to generate the requested distribution ----
# This is called whenever the inputs change. The output functions
# defined below then use the value computed from this expression
d <- reactive(
dist <- switch(input$dist,
norm = rnorm,
unif = runif,
lnorm = rlnorm,
exp = rexp,
rnorm)
dist(input$n)
)
# Generate a plot of the data ----
# Also uses the inputs to build the plot label. Note that the
# dependencies on the inputs and the data reactive expression are
# both tracked, and all expressions are called in the sequence
# implied by the dependency graph.
output$plot <- renderPlot(
dist <- input$dist
n <- input$n
hist(d(),
main = paste("r", dist, "(", n, ")", sep = ""),
col = "#75AADB", border = "white")
)
# Generate a summary of the data ----
output$summary <- renderPrint(
summary(d())
)
# Generate an HTML table view of the data ----
output$table <- renderTable(
d()
)
【讨论】:
谢谢阿什。我试图将它保存在 2 个文件中,不仅一个但给了我错误:以上是关于你好闪亮的应用程序在反应性表达中给了我错误的主要内容,如果未能解决你的问题,请参考以下文章
尝试更改状态栏样式在 ios 中给了我错误 - React native
标头函数平均在 C 中给了我错误的结果。如果我调用我在标头中创建的“平均”函数,结果与预期不符
如何将数据从闪亮的应用程序写入exce / csv文件?恰好我想将股票价格值的值写入excel / csv文件