【问题标题】:Shiny: convert character string into numeric vector within eventReactiveShiny:在 eventReactive 中将字符串转换为数字向量
【发布时间】:2019-03-16 06:08:19
【问题描述】:

我刚刚开始尝试使用 Shiny。我想做的事情看起来很容易,但我做不到。

我只想得到两个用逗号分隔的值字符串,然后,当按下操作按钮“go”时,将这些字符串转换为数字向量,并绘制一个对另一个。

问题似乎是字符串到数字向量的转换。在 R 中,我通常使用以下行并且可以正常工作

x<-"2,4,6,98"
y<-as.numeric(unlist(strsplit(x,",")))
y
[1]  2  4  6 98

但是当“eventReactive”中包含相同的内容时,我得到了

as.double 中的错误:无法将“闭包”类型强制转换为“double”类型的向量

这是我试过的代码:

library(shiny)
ui <- fluidPage(
  titlePanel("Plot data"),
  sidebarPanel(
    textInput(inputId = "Conc",label="measured concentrations",value="",
          placeholder ="Enter a numeric vector, comma separated"),
    textInput(inputId = "Time",label="Time", value="",
          placeholder ="Enter a numeric vector, comma separated"),
actionButton("go","Go!")
  ),

  mainPanel(
    plotOutput(outputId = "plot1")
  )
)


server<-function(input, output){

  t<-eventReactive(input$go,{as.numeric(unlist(strsplit(input$Time,",")))})
  parent<-eventReactive(input$go,{as.numeric(unlist(strsplit(input$Conc,",")))})
  pp<-reactive({plot(t,parent)})
  output$plot1 <- renderPlot({pp()})
}
shinyApp(ui=ui,server=server)

感谢任何帮助!

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    不明白你想要什么,但试试这个:

    library(shiny)
    ui <- fluidPage(
      titlePanel("Plot data"),
      sidebarPanel(
        textInput(inputId = "Conc",label="measured concentrations",value="",
                  placeholder ="Enter a numeric vector, comma separated"),
        textInput(inputId = "Time",label="Time", value="",
                  placeholder ="Enter a numeric vector, comma separated"),
        actionButton("go","Go!")
      ),
    
      mainPanel(
        plotOutput(outputId = "plot1")
      )
    )
    
    
    server<-function(input, output){
      val<-reactiveValues()
    
      observeEvent(input$go,{
        val$t<-as.numeric(unlist(strsplit(input$Time,",")))
        val$parent<-as.numeric(unlist(strsplit(input$Conc,",")))
    
      })
    
      output$plot1 <- renderPlot({
        validate(need(val$t,""))
        validate(need(val$parent,""))
        plot(val$t,val$parent)
      })
    
    }
    shinyApp(ui=ui,server=server)
    

    【讨论】:

    • 完美运行!谢谢!
    猜你喜欢
    • 2020-02-27
    • 1970-01-01
    • 1970-01-01
    • 2017-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-19
    相关资源
    最近更新 更多