R Shiny非数字参数

时间:2016-01-27 17:35:31

标签: r shiny

我刚开始使用闪亮,运行此代码时出现了第一个问题:

Shiny Server.R

library(shiny)

shinyServer(
function(input, output){


asset <- reactive(input$asset)
weight1 <- 0.3
weight2 <- 1-weight1

sum1 <- asset*weight1

Shiny UI.R:

library(shiny)

shinyUI(fluidPage(

titlePanel(title = "Programm"),
sidebarLayout(
sidebarPanel(("Asset 1:"),
           numericInput("asset:", "asset1:", 0, min=0, max=1000000)),
mainPanel(("xyz"),



         )

   )

))

错误说明:二元运算符的非数字参数。是否有可能从反应函数中获取数值? 谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

旁注:遗憾的是,您的代码无法真正重现。更好的代码将使您更有可能得到答案

问题是asset是一个反应对象。因此,你不能在行sum1 <- asset*weight1中乘以它。 这应该是

sum1 <- reactive({asset()*weight1})

总代码看起来像

shiny::shinyApp(shinyUI(fluidPage(

    titlePanel(title = "Programm"),
    sidebarLayout(
        sidebarPanel(numericInput("asset", "asset1:", 0, min=0, max=1000000)),
        mainPanel(  textOutput("text1"))

    )

  )),
  shinyServer(
    function(input, output){
        asset <- reactive(input$asset)
        weight1 <- 0.3
        weight2 <- 1-weight1
        output$text1 <- reactive({asset()*weight1})
     }))