闪亮:textInput在R中不响应

用户330

我写了一些代码,使情节变得光彩照人。正如我在代码中看到的那样,当我使用plot(c(12,11))时,我得到了情节,但是当我使用plot(c(input $ vec))或plot(input $ vec)时,我却没有改变情节。

library(shiny)
library(ggplot2)
library(dplyr)
library(purrr)
ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      textInput('vec', 'Enter a vector (comma delimited)', "0,1")
    ),
    
    mainPanel(
      plotOutput(outputId = "distPlot"),
    )
  )
)
           
      server <- function(input, output) {
        output$distPlot <- renderPlot({
          qo <- function(x,m) { 
            p<-x^3+m^3
            return(p)
          }
          
          plot <- function(m) {
            ggplot(tibble(x = c(-10, 20)), aes(x)) +
              map(m, 
                  ~stat_function(fun = qo, aes(color = paste0("heat ", .)), args=list(m = .)))
            
          }
          plot(c(12,11))
          
        
      })
        }
shinyApp(ui,server)

斯蒂芬

问题是这input$vec是一个字符串。要在函数中使用输入,首先必须将字符串拆分为单个数字并转换为数字,例如使用vec <- as.numeric(trimws(unlist(strsplit(input$vec, split = ","))))在这里,我首先使用分割字符串,,使用strsplit将结果列表转换为向量unlist,通过删除空格trimws,最后转换为数字。

之后,您可以应用您的功能:

library(shiny)
library(ggplot2)
library(dplyr)
library(purrr)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      textInput('vec', 'Enter a vector (comma delimited)', "0,1")
    ),
    
    mainPanel(
      plotOutput(outputId = "distPlot"),
    )
  )
)

server <- function(input, output) {
  output$distPlot <- renderPlot({
    qo <- function(x,m) { 
      p<-x^3+m^3
      return(p)
    }
    
    plot <- function(m) {
      ggplot(tibble(x = c(-10, 20)), aes(x)) +
        map(m, 
            ~stat_function(fun = qo, aes(color = paste0("heat ", .)), args=list(m = .)))
      
    }
    vec <- as.numeric(trimws(unlist(strsplit(input$vec, split = ","))))
    
    plot(vec)
    
  })
}
shinyApp(ui,server)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章