首页 > 解决方案 > R/Shiny 直方图类作为反应性

问题描述

R 允许将直方图保存为“直方图”类对象:

h <- hist(x,plot=FALSE)

并且可以从“h”恢复几个值。例如“h$mids”、“h$counts”等...

现在,我需要在 Shiny 脚本中使用它,但我找不到如何将“h”保存为反应变量。你知道怎么做吗?

我试过: h <- reactive({hist(x,breaks=y,plot=FALSE)})

但它不起作用。

以下是我的脚本。

谢谢

胡安


library(shiny)

InputParameters <- function(u) {

sidebarPanel("Input parameters"
,numericInput("N","Number",1000,min=100,max=10000,step=100,width=NULL)
,numericInput("mc","Mean",0.22,min=0.02,max=0.5,step=0.02,width=NULL)
,numericInput("sc","Sttdev",0.57,min=0.4,max=0.8,step=0.01,width=NULL)
,numericInput("logmstep","Bin",0.2,min=0.1,max=0.5,step=0.1,width=NULL)
,width=2)

}

ui <- fluidPage(

sidebarLayout(

        InputParameters("A"),            

        mainPanel(                    
                navbarPage("IMF2CMD"
                ,tabPanel("System-IMF"
                ,plotOutput("plot1",width="100%",height="700px",click=NULL,
                        dblclick=NULL,hover=NULL,hoverDelay=NULL,
                        hoverDelayType=NULL,brush=NULL,clickId=NULL,hoverId=NULL,
                        inline=FALSE)
         )

      ) 
    ) 
  ) 
) 


server <- function(input,output,session) {

logmc        <- reactive( { log(input$mc,10) } )
logm         <- reactive( { rnorm(input$N,logmc(),input$sc) } )
breakslogM   <- reactive( { breakslogM <- seq(-3,2,input$logmstep) } )

output$plot1 <- renderPlot( { hist(logm(),breaks=breakslogM(),plot=TRUE) })

h           <- reactive( { hist(logm(),breaks=breakslogM(),plot=FALSE) } )

}

shinyApp(ui=ui,server=server)

标签: rshiny

解决方案


它适用于您的h <- reactive( { hist(logm(),breaks=breakslogM(),plot=FALSE) } ), 然后output$plot1 <- renderPlot( { plot(h()) })


推荐阅读