Warning: 出错,无法将类型 ‘closure’ 强制转换为类型 ‘character’

huangapple go评论72阅读模式
英文:

Warning: Error in as.character: cannot coerce type 'closure' to vector of type 'character'

问题

这是从书中复制的代码,似乎在运行应用程序时遇到了错误。以下是错误信息:

警告:Error in as.character: 无法将类型 'closure' 强制转换为类型 'character'

你已经尝试在Stack Overflow上查找解决方案但没有成功。如果你有特定的问题或需要帮助解决这个错误,请告诉我,我将尽力提供帮助。

英文:

Doing a small project for homework. We had to copy beginner project code from a book and then edit the slider into the sidebar. I copied the work in the book but get an error every time I try to run the app.

>Warning: Error in as.character: cannot coerce type 'closure' to vector of type 'character'

This is the code I copied from Schmuller, Joseph. R Projects for Dummies, John Wiley & Sons, Incorporated, 2018. ProQuest Ebook Central, http://ebookcentral.proquest.com/lib/erau/detail.action?docID=5231273.

I have looked up answers on SO and have not found any successful fixes.

library(shiny)
library(shinydashboard)

ui <- dashboardPage( 
  dashboardHeader(title = "Sampling"),
  dashboardSidebar(
    sidebarMenu(
      menuItem("Uniform Distibution", 
               tabName = "uniform",
               icon = icon("circle")),
      menuItem("Normal Distribution",
               tabName = "normal",
               icon = icon("circle"))
    )),
  
  dashboardBody(
    tabItems(
     
       tabItem(
        tabName = "uniform",
        fluidRow(
          box(title = "Select a Number",
              solidHeader = TRUE, 
              background = "yellow", 
              status="warning", 
              height = 312, 
              sliderInput(inputId = "number",
                          label = "",
                          value = 500,
                          min = 25,
                          max = 1000)), 
          box(title = "Histogram",
              solidHeader=TRUE, 
              background = "light-blue", 
              status="primary", 
              plotOutput("hist", height = 250)),
          
          valueBoxOutput("meanBox"),
          valueBoxOutput("mediumBox"),
          valueBoxOutput("sdBox")
        )
      ),
      
      tabItem(
        tabName = "normal",
          fluidRow(
              box(title = "Select a Number",
                    solidHeader = TRUE,
                    collapsible = TRUE,
                    status = "warning",
                    sliderInput(inputId = "normnumber",
                                label = "",
                                value = 500,
                                min = 25,
                                max = 1000)),
                box(title = "Density Plot",
                    solidHeader = TRUE,
                    background = "light-blue",
                    status = "primary",
                    plotOutput("density", 
                               height = 250)),
                
                infoBoxOutput("meanInfoBox"),
                infoBoxOutput("medianInfoBox"),
                infoBoxOutput("sdInfoBox")
              )
      )
    ),
    
  ),
  
  
  server <- function(input, output) { 
    histdata <- {runif(input$number,min=0,max=1)}
    densitydata <- {rnorm(input$normnumber)}
    
    output$hist <- renderPlot({ 
      hist(histdata(),
           xlab="Value", 
           main=paste(input$number,
                      "random values between 0 and 1")) 
    }) 
    
    output$density <- renderPlot({
      hist(densitydata(),
           xlab="Value",
           main=paste("standard normal distribution \n",
                      input$normnumber,
                      "random values"),
           probability = TRUE)
      lines(density(densitydata()))
      
    output$meanBox <- renderValueBox({
        valueBox(
          round(mean(histdata()),3),
          "Mean",
          color = "navy"
        )
      })
      
      output$medianBox <- renderValueBox({
        valueBox(
          round(median(histdata()),3),
          "Median",
          color = "aqua"
        )
      })
      
      output$sdBox <- renderValueBox({
        valueBox(
          round(sd(histdata()),3),
          "Standard Deviation",
          color = "blue"
        ) 
      })
      
      output$meanInfoBox <- renderInfoBox({
        infoBox("Mean",
                round(mean(densitydata()),3),
                icon = icon("align-center"),
                color = "navy")
      })
      
      output$medianInfoBox <- renderInfoBox({
        infoBox("Median",
                round(median(densitydata()),3),
                icon = icon("area-chart"),
                color = "aqua")
      })
      
      output$sdInfoBox <- renderInfoBox({
        infoBox("Standard Deviation",
                round(sd(densitydata()),3),
                icon = icon("scribd"),
                fill = TRUE,
                color = "blue")
      })
    })
  }
)
shinyApp(ui, server)

答案1

得分: 0

我对这段代码进行了一些修改以使其工作。请查看下面的修改后的代码。通常,UI和服务器是两个分开的函数,所以我去掉了服务器函数前面的逗号。

其次,我使数据能够从滑块中“响应”,通过将histdatadensitydata的构建包装在一个响应函数中(请查看服务器函数中的前两行)。这样做的原因是数据会随着滑块的移动而变化(或重新生成),因此您必须指示shiny以相同的方式动态地进行操作(在控制台中运行?shiny::reactive以获取帮助)。

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title = "Sampling"),
  dashboardSidebar(
    sidebarMenu(
      menuItem("Uniform Distibution",
               tabName = "uniform",
               icon = icon("circle")
               ),
      menuItem("Normal Distribution",
               tabName = "normal",
               icon = icon("circle")
               )
      ))
  ,

  dashboardBody(
    tabItems(

      tabItem(
        tabName = "uniform",
        fluidRow(
          box(title = "Select a Number",
              solidHeader = TRUE,
              background = "yellow",
              status = "warning",
              height = 312,
              sliderInput(inputId = "number",
                          label = "",
                          value = 500,
                          min = 25,
                          max = 1000)
              ),
          shinydashboard::box(title = "Histogram",
              solidHeader=TRUE,
              background = "light-blue",
              status="primary",
              plotOutput("hist", height = 250)
              )
        )
      ),

      tabItem(
        tabName = "normal",
        fluidRow(
          box(title = "Select a Number",
              solidHeader = TRUE,
              collapsible = TRUE,
              status = "warning",
              sliderInput(inputId = "normnumber",
                          label = "",
                          value = 500,
                          min = 25,
                          max = 1000
                          )
              ),
          box(title = "Density Plot",
              solidHeader = TRUE,
              background = "light-blue",
              status = "primary",
              plotOutput("density",
                         height = 250
                         )
              ),

          infoBoxOutput("meanInfoBox"),
          infoBoxOutput("medianInfoBox"),
          infoBoxOutput("sdInfoBox")
        )
      )
  )

  ))


  server <- function(input, output) {
    histdata <- reactive({runif(input$number,min=0,max=1)})
    densitydata <- reactive({rnorm(input$normnumber)})

    output$hist <- renderPlot({
      hist(histdata(),
           xlab="Value",
           main=paste(input$number,
                      "random values between 0 and 1")
           )
    })

    output$density <- renderPlot({
      hist(densitydata(),
           xlab="Value",
           main=paste("standard normal distribution \n",
                      input$normnumber,
                      "random values"),
           probability = TRUE)
      lines(density(densitydata()))

      output$meanBox <- renderValueBox({
        valueBox(
          round(mean(histdata()),3),
          "Mean",
          color = "navy"
        )
      })

      output$medianBox <- renderValueBox({
        valueBox(
          round(median(histdata()),3),
          "Median",
          color = "aqua"
        )
      })

      output$sdBox <- renderValueBox({
        valueBox(
          round(sd(histdata()),3),
          "Standard Deviation",
          color = "blue"
        )
      })

      output$meanInfoBox <- renderInfoBox({
        infoBox("Mean",
                round(mean(densitydata()),3),
                icon = icon("align-center"),
                color = "navy")
      })

      output$medianInfoBox <- renderInfoBox({
        infoBox("Median",
                round(median(densitydata()),3),
                icon = icon("area-chart"),
                color = "aqua")
      })

      output$sdInfoBox <- renderInfoBox({
        infoBox("Standard Deviation",
                round(sd(densitydata()),3),
                icon = icon("scribd"),
                fill = TRUE,
                color = "blue")
      })
    })
  }

shinyApp(ui, server)

英文:

I made a few changes to get this to work. See the modified code below. Usually, the UI and server are two separate functions, so I removed the comma before the server function.

Secondly, I made the data to be captured from the sliders "reactive", by wrapping the construction of histdata and densitydata in a reactive function (See first 2 lines in the server function). The reason here is that the data changes (or is regenerated) as you move the slider, so you have to instruct shiny to be dynamic in the same way (Run ?shiny::reactive in your console for help on this).

library(shiny)
library(shinydashboard)
ui &lt;- dashboardPage(
dashboardHeader(title = &quot;Sampling&quot;),
dashboardSidebar(
sidebarMenu(
menuItem(&quot;Uniform Distibution&quot;,
tabName = &quot;uniform&quot;,
icon = icon(&quot;circle&quot;)
),
menuItem(&quot;Normal Distribution&quot;,
tabName = &quot;normal&quot;,
icon = icon(&quot;circle&quot;)
)
))
,
dashboardBody(
tabItems(
tabItem(
tabName = &quot;uniform&quot;,
fluidRow(
box(title = &quot;Select a Number&quot;,
solidHeader = TRUE,
background = &quot;yellow&quot;,
status = &quot;warning&quot;,
height = 312,
sliderInput(inputId = &quot;number&quot;,
label = &quot;&quot;,
value = 500,
min = 25,
max = 1000)
),
shinydashboard::box(title = &quot;Histogram&quot;,
solidHeader=TRUE,
background = &quot;light-blue&quot;,
status=&quot;primary&quot;,
plotOutput(&quot;hist&quot;, height = 250)
)
)
),
tabItem(
tabName = &quot;normal&quot;,
fluidRow(
box(title = &quot;Select a Number&quot;,
solidHeader = TRUE,
collapsible = TRUE,
status = &quot;warning&quot;,
sliderInput(inputId = &quot;normnumber&quot;,
label = &quot;&quot;,
value = 500,
min = 25,
max = 1000
)
),
box(title = &quot;Density Plot&quot;,
solidHeader = TRUE,
background = &quot;light-blue&quot;,
status = &quot;primary&quot;,
plotOutput(&quot;density&quot;,
height = 250
)
),
infoBoxOutput(&quot;meanInfoBox&quot;),
infoBoxOutput(&quot;medianInfoBox&quot;),
infoBoxOutput(&quot;sdInfoBox&quot;)
)
)
)
))
server &lt;- function(input, output) {
histdata &lt;- reactive({runif(input$number,min=0,max=1)})
densitydata &lt;- reactive({rnorm(input$normnumber)})
output$hist &lt;- renderPlot({
hist(histdata(),
xlab=&quot;Value&quot;,
main=paste(input$number,
&quot;random values between 0 and 1&quot;)
)
})
output$density &lt;- renderPlot({
hist(densitydata(),
xlab=&quot;Value&quot;,
main=paste(&quot;standard normal distribution \n&quot;,
input$normnumber,
&quot;random values&quot;),
probability = TRUE)
lines(density(densitydata()))
output$meanBox &lt;- renderValueBox({
valueBox(
round(mean(histdata()),3),
&quot;Mean&quot;,
color = &quot;navy&quot;
)
})
output$medianBox &lt;- renderValueBox({
valueBox(
round(median(histdata()),3),
&quot;Median&quot;,
color = &quot;aqua&quot;
)
})
output$sdBox &lt;- renderValueBox({
valueBox(
round(sd(histdata()),3),
&quot;Standard Deviation&quot;,
color = &quot;blue&quot;
)
})
output$meanInfoBox &lt;- renderInfoBox({
infoBox(&quot;Mean&quot;,
round(mean(densitydata()),3),
icon = icon(&quot;align-center&quot;),
color = &quot;navy&quot;)
})
output$medianInfoBox &lt;- renderInfoBox({
infoBox(&quot;Median&quot;,
round(median(densitydata()),3),
icon = icon(&quot;area-chart&quot;),
color = &quot;aqua&quot;)
})
output$sdInfoBox &lt;- renderInfoBox({
infoBox(&quot;Standard Deviation&quot;,
round(sd(densitydata()),3),
icon = icon(&quot;scribd&quot;),
fill = TRUE,
color = &quot;blue&quot;)
})
})
}
shinyApp(ui, server)

huangapple
  • 本文由 发表于 2023年5月15日 05:11:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76249701.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定