Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.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
如何使ui.R和server.R从ShinyApp中的其他文件和绘图导入函数和数据_R_Shiny_Rstudio - Fatal编程技术网

如何使ui.R和server.R从ShinyApp中的其他文件和绘图导入函数和数据

如何使ui.R和server.R从ShinyApp中的其他文件和绘图导入函数和数据,r,shiny,rstudio,R,Shiny,Rstudio,我有一个三部分的文件,我想用它来制作闪亮的应用程序 酷图 这是一个存储打印功能的代码 library(ggplot2) library(tidyverse) library(ggrepel) #------------------------- # Function #------------------------- plotit <- function (dat, x_thres, y_thres) { dat["Significant"] <- ifelse((dat$

我有一个三部分的文件,我想用它来制作闪亮的应用程序

酷图 这是一个存储打印功能的代码

library(ggplot2)
library(tidyverse)
library(ggrepel)

#-------------------------
# Function 
#-------------------------
plotit <- function (dat, x_thres, y_thres) {
  dat["Significant"] <- ifelse((dat$wt > x_thres | 
                                  dat$mpg > y_thres ), 'NotSignif','Signif')
  p <- ggplot(dat, aes(wt, mpg)) + 
       geom_point(alpha=0.8,size=2.75, aes(color=Significant)) +
       scale_color_manual(values=c("#B94024","#7D8D87")) +
       geom_vline(xintercept= x_thres, colour = '#B94024') + 
       geom_hline(yintercept=y_thres, colour = '#B94024') +
       geom_text_repel(data=subset(dat, wt > x_thres | mpg > y_thres),
                       aes(wt,mpg,label=model),
                       box.padding = unit(0.35, "lines"),
                       point.padding = unit(0.3, "lines") 
                       )  + 
       theme(legend.position="none")  

  return(p)

}

#-------------------------
# Begin main code
#-------------------------
# I literally want to use file as input not 
# default mtcars variable
infile <- "https://gist.githubusercontent.com/seankross/a412dfbd88b3db70b74b/raw/5f23f993cd87c283ce766e7ac6b329ee7cc2e1d1/mtcars.csv"
dat <- read_delim(infile,delim=",", col_types = cols())  
y_thres <- 25
x_thres <- 3
plotit(dat, x_thres, y_thres)
用户界面 我的问题是如何从
plot.R
和 显示绘图

目前,在RStudio中,它看起来像这样(减去我的评论)


这是你想要的吗?请确保您的滑块具有唯一的名称

library(ggplot2)
library(tidyverse)
library(ggrepel)
library(shiny)
#-------------------------
# Function 
#-------------------------
plotit <- function (dat, x_thres, y_thres) {
  dat["Significant"] <- ifelse((dat$wt > x_thres | 
                                  dat$mpg > y_thres ), 'NotSignif','Signif')
  p <- ggplot(dat, aes(wt, mpg)) + 
    geom_point(alpha=0.8,size=2.75, aes(color=Significant)) +
    scale_color_manual(values=c("#B94024","#7D8D87")) +
    geom_vline(xintercept= x_thres, colour = '#B94024') + 
    geom_hline(yintercept=y_thres, colour = '#B94024') +
    geom_text_repel(data=subset(dat, wt > x_thres | mpg > y_thres),
                    aes(wt,mpg,label=model),
                    box.padding = unit(0.35, "lines"),
                    point.padding = unit(0.3, "lines") 
    )  + 
    theme(legend.position="none")  

  return(p)

}

#-------------------------
# Begin main code
#-------------------------
# I literally want to use file as input not 
# default mtcars variable
infile <- "https://gist.githubusercontent.com/seankross/a412dfbd88b3db70b74b/raw/5f23f993cd87c283ce766e7ac6b329ee7cc2e1d1/mtcars.csv"
dat <- read_delim(infile,delim=",", col_types = cols())  
y_thres <- 25
x_thres <- 3
plotit(dat, x_thres, y_thres)



ui <- shinyUI(
  fluidPage(

    #  Application title
    titlePanel("Sliders"),

    # Sidebar with sliders that demonstrate various available
    # options
    sidebarLayout(
      sidebarPanel(
        # Simple integer interval
        sliderInput("integer", "X-threshold",
                    min=3, max=10, value=1),

        # Simple integer interval
        sliderInput("integer2", "Y-threshold",
                    min=10, max=35, value=1)

      ),

      # Show a table summarizing the values entered
      mainPanel(
        tableOutput("values"),
        plotOutput("myplot")
        # How can I output the plot from coolplot.R here????
      )
    )
  )
)

server <- shinyServer(function(input, output, session) {
  # Reactive expression to compose a data frame containing all of
  # the values

  sliderValues <- reactive({

    # Compose data frame
    data.frame(Name = c("X-threshold", "Y-threshold"),
      Value = as.character(c(input$integer, input$integer2)), 
      stringsAsFactors=FALSE)
  }) 

  # Show the values using an HTML table
  output$values <- renderTable({
    sliderValues()
  })

  output$myplot <- renderPlot({

    plotit(dat, input$integer, input$integer2)
  })

})

shinyApp(ui = ui, server = server)
库(ggplot2)
图书馆(tidyverse)
图书馆(ggrepel)
图书馆(闪亮)
#-------------------------
#作用
#-------------------------
情节、不签名、签名)
p x|thres | mpg>y|thres),
aes(重量、mpg、标签=型号),
box.padding=单位(0.35,“线”),
点填充=单位(0.3,“线”)
)  + 
主题(legend.position=“无”)
返回(p)
}
#-------------------------
#开始主代码
#-------------------------
#我确实想使用文件作为输入,而不是
#默认mtcars变量

你想要什么?请确保您的滑块具有唯一的名称

library(ggplot2)
library(tidyverse)
library(ggrepel)
library(shiny)
#-------------------------
# Function 
#-------------------------
plotit <- function (dat, x_thres, y_thres) {
  dat["Significant"] <- ifelse((dat$wt > x_thres | 
                                  dat$mpg > y_thres ), 'NotSignif','Signif')
  p <- ggplot(dat, aes(wt, mpg)) + 
    geom_point(alpha=0.8,size=2.75, aes(color=Significant)) +
    scale_color_manual(values=c("#B94024","#7D8D87")) +
    geom_vline(xintercept= x_thres, colour = '#B94024') + 
    geom_hline(yintercept=y_thres, colour = '#B94024') +
    geom_text_repel(data=subset(dat, wt > x_thres | mpg > y_thres),
                    aes(wt,mpg,label=model),
                    box.padding = unit(0.35, "lines"),
                    point.padding = unit(0.3, "lines") 
    )  + 
    theme(legend.position="none")  

  return(p)

}

#-------------------------
# Begin main code
#-------------------------
# I literally want to use file as input not 
# default mtcars variable
infile <- "https://gist.githubusercontent.com/seankross/a412dfbd88b3db70b74b/raw/5f23f993cd87c283ce766e7ac6b329ee7cc2e1d1/mtcars.csv"
dat <- read_delim(infile,delim=",", col_types = cols())  
y_thres <- 25
x_thres <- 3
plotit(dat, x_thres, y_thres)



ui <- shinyUI(
  fluidPage(

    #  Application title
    titlePanel("Sliders"),

    # Sidebar with sliders that demonstrate various available
    # options
    sidebarLayout(
      sidebarPanel(
        # Simple integer interval
        sliderInput("integer", "X-threshold",
                    min=3, max=10, value=1),

        # Simple integer interval
        sliderInput("integer2", "Y-threshold",
                    min=10, max=35, value=1)

      ),

      # Show a table summarizing the values entered
      mainPanel(
        tableOutput("values"),
        plotOutput("myplot")
        # How can I output the plot from coolplot.R here????
      )
    )
  )
)

server <- shinyServer(function(input, output, session) {
  # Reactive expression to compose a data frame containing all of
  # the values

  sliderValues <- reactive({

    # Compose data frame
    data.frame(Name = c("X-threshold", "Y-threshold"),
      Value = as.character(c(input$integer, input$integer2)), 
      stringsAsFactors=FALSE)
  }) 

  # Show the values using an HTML table
  output$values <- renderTable({
    sliderValues()
  })

  output$myplot <- renderPlot({

    plotit(dat, input$integer, input$integer2)
  })

})

shinyApp(ui = ui, server = server)
库(ggplot2)
图书馆(tidyverse)
图书馆(ggrepel)
图书馆(闪亮)
#-------------------------
#作用
#-------------------------
情节、不签名、签名)
p x|thres | mpg>y|thres),
aes(重量、mpg、标签=型号),
box.padding=单位(0.35,“线”),
点填充=单位(0.3,“线”)
)  + 
主题(legend.position=“无”)
返回(p)
}
#-------------------------
#开始主代码
#-------------------------
#我确实想使用文件作为输入,而不是
#默认mtcars变量

谢谢。差不多了。我需要将
coolplot.R
ui.R
server.R
分开,我该怎么做?你可以通过
source
添加这个,比如
source('coolplot.R',local=TRUE)
。但是您需要指定
源文件中的文件目录
callI在
服务器.r中添加了一行,我认为应该在那里尝试。我得到了这个错误
参数意味着不同的行数:2,0对象'xintercept'未找到1
。如果您能在单独的代码
ui.R
server.R
coolplot.R
中给出示例,我将不胜感激。否,请仅将其放入服务器。请确保文件已加载…谢谢。差不多了。我需要将
coolplot.R
ui.R
server.R
分开,我该怎么做?你可以通过
source
添加这个,比如
source('coolplot.R',local=TRUE)
。但是您需要指定
源文件中的文件目录
callI在
服务器.r中添加了一行,我认为应该在那里尝试。我得到了这个错误
参数意味着不同的行数:2,0对象'xintercept'未找到1
。如果您能在单独的代码
ui.R
server.R
coolplot.R
中给出示例,我将不胜感激。否,请仅将其放入服务器。只要确保为它加载文件。。。
library(ggplot2)
library(tidyverse)
library(ggrepel)
library(shiny)
#-------------------------
# Function 
#-------------------------
plotit <- function (dat, x_thres, y_thres) {
  dat["Significant"] <- ifelse((dat$wt > x_thres | 
                                  dat$mpg > y_thres ), 'NotSignif','Signif')
  p <- ggplot(dat, aes(wt, mpg)) + 
    geom_point(alpha=0.8,size=2.75, aes(color=Significant)) +
    scale_color_manual(values=c("#B94024","#7D8D87")) +
    geom_vline(xintercept= x_thres, colour = '#B94024') + 
    geom_hline(yintercept=y_thres, colour = '#B94024') +
    geom_text_repel(data=subset(dat, wt > x_thres | mpg > y_thres),
                    aes(wt,mpg,label=model),
                    box.padding = unit(0.35, "lines"),
                    point.padding = unit(0.3, "lines") 
    )  + 
    theme(legend.position="none")  

  return(p)

}

#-------------------------
# Begin main code
#-------------------------
# I literally want to use file as input not 
# default mtcars variable
infile <- "https://gist.githubusercontent.com/seankross/a412dfbd88b3db70b74b/raw/5f23f993cd87c283ce766e7ac6b329ee7cc2e1d1/mtcars.csv"
dat <- read_delim(infile,delim=",", col_types = cols())  
y_thres <- 25
x_thres <- 3
plotit(dat, x_thres, y_thres)



ui <- shinyUI(
  fluidPage(

    #  Application title
    titlePanel("Sliders"),

    # Sidebar with sliders that demonstrate various available
    # options
    sidebarLayout(
      sidebarPanel(
        # Simple integer interval
        sliderInput("integer", "X-threshold",
                    min=3, max=10, value=1),

        # Simple integer interval
        sliderInput("integer2", "Y-threshold",
                    min=10, max=35, value=1)

      ),

      # Show a table summarizing the values entered
      mainPanel(
        tableOutput("values"),
        plotOutput("myplot")
        # How can I output the plot from coolplot.R here????
      )
    )
  )
)

server <- shinyServer(function(input, output, session) {
  # Reactive expression to compose a data frame containing all of
  # the values

  sliderValues <- reactive({

    # Compose data frame
    data.frame(Name = c("X-threshold", "Y-threshold"),
      Value = as.character(c(input$integer, input$integer2)), 
      stringsAsFactors=FALSE)
  }) 

  # Show the values using an HTML table
  output$values <- renderTable({
    sliderValues()
  })

  output$myplot <- renderPlot({

    plotit(dat, input$integer, input$integer2)
  })

})

shinyApp(ui = ui, server = server)
library(ggplot2)
library(tidyverse)
library(ggrepel)

#-------------------------
# Function 
#-------------------------
#-------------------------
# Begin main code
#-------------------------
# I literally want to use file as input not 
# default mtcars variable
infile <- "https://gist.githubusercontent.com/seankross/a412dfbd88b3db70b74b/raw/5f23f993cd87c283ce766e7ac6b329ee7cc2e1d1/mtcars.csv"
dat <- read_delim(infile,delim=",", col_types = cols())  

plotit <- function (dat, x_thres, y_thres) {
  dat["Significant"] <- ifelse((dat$wt > x_thres | 
                                  dat$mpg > y_thres ), 'NotSignif','Signif')
  p <- ggplot(dat, aes(wt, mpg)) + 
    geom_point(alpha=0.8,size=2.75, aes(color=Significant)) +
    scale_color_manual(values=c("#B94024","#7D8D87")) +
    geom_vline(xintercept= x_thres, colour = '#B94024') + 
    geom_hline(yintercept=y_thres, colour = '#B94024') +
    geom_text_repel(data=subset(dat, wt > x_thres | mpg > y_thres),
                    aes(wt,mpg,label=model),
                    box.padding = unit(0.35, "lines"),
                    point.padding = unit(0.3, "lines") 
    )  + 
    theme(legend.position="none")  

  return(p)
}
   library(shiny)
    source("coolplot.R",local = TRUE)$value

    ui <- shinyUI(
      fluidPage(

        #  Application title
        titlePanel("Sliders"),

        # Sidebar with sliders that demonstrate various available
        # options
        sidebarLayout(
          sidebarPanel(
            # Simple integer interval
            sliderInput("integer", "X-threshold",
                        min=3, max=10, value=1),

            # Simple integer interval
            sliderInput("integer2", "Y-threshold",
                        min=10, max=35, value=1)

          ),

          # Show a table summarizing the values entered
          mainPanel(
            tableOutput("values"),
            plotOutput("myplot")
            # How can I output the plot from coolplot.R here????
          )
        )
      )
    )

    server <- shinyServer(function(input, output, session) {
      # Reactive expression to compose a data frame containing all of
      # the values

      # Add the source file of the plot if necessary

      sliderValues <- reactive({

        # Compose data frame
        data.frame(Name = c("X-threshold", "Y-threshold"),
          Value = as.character(c(input$integer, input$integer2)), 
          stringsAsFactors=FALSE)
      }) 

      # Show the values using an HTML table
      output$values <- renderTable({
        sliderValues()
      })

      output$myplot <- renderPlot({

        plotit(dat, input$integer, input$integer2)
      })

    })

    shinyApp(ui = ui, server = server)