Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 在Shining中调用其他函数_R_Shiny_Lattice - Fatal编程技术网

R 在Shining中调用其他函数

R 在Shining中调用其他函数,r,shiny,lattice,R,Shiny,Lattice,我开发了一个简单的闪亮应用程序,将平均值my_mean和标准偏差my_sd分布的分数my_x作为输入。作为输出,应用程序返回一个正态标准分布的格点图,对应于my_x的z分数。请在上查找应用程序的代码 现在,我想为应用程序添加第二个功能: 通过检查checkboxInput,我将计算输入的pnorm,并对图形的相对区域进行着色 library(lattice) e4a <- seq(60, 170, length = 10000) e4b <- dnorm(e4a, 110, 15)

我开发了一个简单的闪亮应用程序,将平均值
my_mean
和标准偏差
my_sd
分布的分数
my_x
作为输入。作为输出,应用程序返回一个正态标准分布的格点图,对应于
my_x
z分数。请在上查找应用程序的代码

现在,我想为应用程序添加第二个功能:

通过检查
checkboxInput
,我将计算输入的
pnorm
,并对图形的相对区域进行着色

library(lattice)
e4a <- seq(60, 170, length = 10000)
e4b <- dnorm(e4a, 110, 15)
#z-score is calculated with the inputs listed above:

z_score <- (my_x - my_mean)/my_sd

plot_e4d <- xyplot(e4b ~ e4a,
               type = "l",
               main = "Plot 4",
               scales = list(x = list(at = seq(60, 170, 10)), rot = 45),
               panel = function(x,y, ...){
                   panel.xyplot(x,y, ...)
                   panel.abline(v = c(z_score, 110), lty = 2)

                   xx <- c(60, x[x>=60 & x<=z_score], z_score) 
                   yy <- c(0, y[x>=60 & x<=z_score], 0) 
                   panel.polygon(xx,yy, ..., col='red')
               })
print(plot_e4d)
我为图形编写了代码(这里是预期结果的一个示例),但我不知道如何使它在Shiny中工作。特别是,我无法想象如何使复选框激活的函数在绘制图形的第一个函数中正常工作

library(lattice)
e4a <- seq(60, 170, length = 10000)
e4b <- dnorm(e4a, 110, 15)
#z-score is calculated with the inputs listed above:

z_score <- (my_x - my_mean)/my_sd

plot_e4d <- xyplot(e4b ~ e4a,
               type = "l",
               main = "Plot 4",
               scales = list(x = list(at = seq(60, 170, 10)), rot = 45),
               panel = function(x,y, ...){
                   panel.xyplot(x,y, ...)
                   panel.abline(v = c(z_score, 110), lty = 2)

                   xx <- c(60, x[x>=60 & x<=z_score], z_score) 
                   yy <- c(0, y[x>=60 & x<=z_score], 0) 
                   panel.polygon(xx,yy, ..., col='red')
               })
print(plot_e4d)
库(晶格)
e4a这应该有效:

library(shiny)
library(lattice)

shinyApp(
  ui = {
    pageWithSidebar(
      headerPanel("Standard Normal"),
      sidebarPanel(
        numericInput('mean', 'Your mean', 80),
        numericInput('sd', 'Your standard deviation', 2),
        numericInput('x', 'Your score', 250),
        checkboxInput("zScoreArea", label = "Area under z-score", value = TRUE)
      ),
      mainPanel(
        h3('Standard Normal'),
        plotOutput('sdNorm'),
        h4('Your z-score is:'),
        verbatimTextOutput('z_score')
      ))
  },
  server = {
    function(input, output){

      #data
      dt1 <- seq(60, 170, length = 10000)
      dt2 <- dnorm(dt1, 110, 15)

      #xyplot panel= function()
      myfunc <- reactive({
        if(input$zScoreArea){
          function(x,y, ...){
            panel.xyplot(x,y, ...)
            panel.abline( v = c(z_score(), 110), lty = 2)

            xx <- c(60, x[x >= 60 & x <= z_score()], z_score())
            yy <- c(0,  y[x >= 60 & x <= z_score()], 0)
            panel.polygon(xx,yy, ..., col='red')
          }
        }else{
          function(x, ...){
            panel.xyplot(x, ...)
            panel.abline(v = c(z_score(), 110), lty = 2)}

        }
      })

      #reactive z_score for plotting
      z_score <- reactive({
        my_mean <- input$mean
        my_sd <- input$sd
        my_x <- input$x

        #return z score
        (my_x - my_mean)/my_sd
      })

      output$sdNorm <- renderPlot({
        xyplot(dt2 ~ dt1,
               type = "l",
               main = "Plot 4",
               scales = list(x = list(at = seq(60, 170, 10)), rot = 45),
               panel = myfunc()
        )
      })

      output$z_score = renderPrint({ z_score() })
    }
  }
)
库(闪亮)
图书馆(格子)
shinyApp(
用户界面={
带边框的页面(
headerPanel(“标准正常”),
侧栏面板(
数值输入(“平均值”,“你的平均值”,80),
数值输入('sd','您的标准偏差',2),
数值输入('x','你的分数',250),
checkboxInput(“zScoreArea”,label=“z分数下的区域”,值=真)
),
主面板(
h3(“标准正常值”),
plotOutput('sdNorm'),
h4('你的z分数是:'),
逐字输出(“z_分数”)
))
},
服务器={
功能(输入、输出){
#资料

dt1我找到了一个有效的解决方案。我很确定它不是最有效的,但它是有效的。它由调用绘图的服务器函数中的
if
/
else
语句组成。我要感谢@zx8754的启发

以下是
ui.r
文件:

library(shiny)

shinyUI(pageWithSidebar(
headerPanel("Standard Normal"),
sidebarPanel(
    numericInput('mean', 'Your mean', 0),
    numericInput('sd', 'Your standard deviation', 0),
    numericInput('x', 'Your score', 0),
    checkboxInput('p1', label = 'Probability of getting a score smaller than x or z', value = FALSE)
),
mainPanel(
    h3('Standard Normal'),
    plotOutput('sdNorm'),
    h4('Your z-score is:'),
    verbatimTextOutput('z'),
    h4('Your lower tail probability is:'),
    verbatimTextOutput('p1')    
    ))
library(lattice)

shinyServer(
function(input, output){
    output$sdNorm <- renderPlot({
        dt1 <- seq(-3, 3, length = 1000)
        dt2 <- dnorm(dt1, 0, 1)
        my_mean <- input$mean
        my_sd <- input$sd
        my_x <- input$x
        z <- (my_x - my_mean)/my_sd
        if(input$p1){

            xyplot(dt2 ~ dt1,
                   type = "l",
                   main = "Lower tail probability",
                   panel = function(x,y, ...){
                       panel.xyplot(x,y, ...)
                       panel.abline(v = c(z, 0), lty = 2)
                       xx <- c(-3, x[x>=-3 & x<=z], z) 
                       yy <- c(0, y[x>=-3 & x<=z], 0) 
                       panel.polygon(xx,yy, ..., col='red')
                   })

        }else{
            xyplot(dt2 ~ dt1,
                   type = "l",
                   main = "Standard Normal Distribution",
                   panel = function(x, ...){
                       panel.xyplot(x, ...)
                       panel.abline(v = c(z, 0), lty = 2)
                   })
        }

        })
    output$z = renderPrint({
        my_mean <- input$mean
        my_sd <- input$sd
        my_x <- input$x
        z <- (my_x - my_mean)/my_sd
        z
    })
    output$p1 <- renderPrint({
        if(input$p1){
            my_mean <- input$mean
            my_sd <- input$sd
            my_x <- input$x
            p1 <- 1- pnorm(my_x, my_mean, my_sd)
            p1
        } else {
            p1 <- NULL
        }

    })

}
)

以及
server.R
文件:

library(shiny)

shinyUI(pageWithSidebar(
headerPanel("Standard Normal"),
sidebarPanel(
    numericInput('mean', 'Your mean', 0),
    numericInput('sd', 'Your standard deviation', 0),
    numericInput('x', 'Your score', 0),
    checkboxInput('p1', label = 'Probability of getting a score smaller than x or z', value = FALSE)
),
mainPanel(
    h3('Standard Normal'),
    plotOutput('sdNorm'),
    h4('Your z-score is:'),
    verbatimTextOutput('z'),
    h4('Your lower tail probability is:'),
    verbatimTextOutput('p1')    
    ))
library(lattice)

shinyServer(
function(input, output){
    output$sdNorm <- renderPlot({
        dt1 <- seq(-3, 3, length = 1000)
        dt2 <- dnorm(dt1, 0, 1)
        my_mean <- input$mean
        my_sd <- input$sd
        my_x <- input$x
        z <- (my_x - my_mean)/my_sd
        if(input$p1){

            xyplot(dt2 ~ dt1,
                   type = "l",
                   main = "Lower tail probability",
                   panel = function(x,y, ...){
                       panel.xyplot(x,y, ...)
                       panel.abline(v = c(z, 0), lty = 2)
                       xx <- c(-3, x[x>=-3 & x<=z], z) 
                       yy <- c(0, y[x>=-3 & x<=z], 0) 
                       panel.polygon(xx,yy, ..., col='red')
                   })

        }else{
            xyplot(dt2 ~ dt1,
                   type = "l",
                   main = "Standard Normal Distribution",
                   panel = function(x, ...){
                       panel.xyplot(x, ...)
                       panel.abline(v = c(z, 0), lty = 2)
                   })
        }

        })
    output$z = renderPrint({
        my_mean <- input$mean
        my_sd <- input$sd
        my_x <- input$x
        z <- (my_x - my_mean)/my_sd
        z
    })
    output$p1 <- renderPrint({
        if(input$p1){
            my_mean <- input$mean
            my_sd <- input$sd
            my_x <- input$x
            p1 <- 1- pnorm(my_x, my_mean, my_sd)
            p1
        } else {
            p1 <- NULL
        }

    })

}
库(晶格)
shinyServer(
功能(输入、输出){

输出$sdNorm当复选框被“选中”时,是否希望调用函数?此向量中的每个值代表什么:
v=c(80、95、110)
?我想这些应该是反应值。是的,这些是创建示例图的数字。我将编辑示例图的代码。你能将值添加到my_x,my_意思是,my_sd以生成绘图吗?@zx8754即使答案没有直接解决问题,它也启发了我,所以我找到了解决方案。我会在几分钟内向你展示!我是sorry@zx8754,我无法使其工作。我试图通过单击复选框来为z分数(根据输入计算)下的曲线区域着色,但我无法修改您的代码。@我明白了,请尝试将您的数据和z分数作为反应对象保留在renderPlot之外。